Skip to content

Commit

Permalink
Merge pull request #49 from bielu/fix/nested-serialization-correction
Browse files Browse the repository at this point in the history
Fix/nested serialization correction
  • Loading branch information
rickbutterfield authored Mar 25, 2024
2 parents 9ff8168 + 78af597 commit 89d1ddf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,5 @@ FodyWeavers.xsd
*.msp

# JetBrains Rider
*.sln.iml
*.sln.iml
.idea
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Globalization;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Extensions;

namespace Umbraco.Community.BlockPreview.Extensions;

Expand All @@ -13,4 +17,23 @@ public static string ToPascalCase(this string value)

return $"{char.ToUpper(value[0], CultureInfo.CurrentCulture)}{value[1..]}";
}
public static bool TryConvertToGridItem(this object? rawPropValue, out BlockValue value)
{
if (!rawPropValue.ToString()?.DetectIsJson() == true || rawPropValue is not JObject jObject)
{
value = default(BlockValue);
return false;
}
var keys = jObject.Properties().Select(x => x.Name);
if (keys.Contains(nameof(BlockValue.Layout), StringComparer.InvariantCultureIgnoreCase)
|| keys.Contains(nameof(BlockValue.ContentData), StringComparer.InvariantCultureIgnoreCase)
|| keys.Contains(nameof(BlockValue.SettingsData), StringComparer.InvariantCultureIgnoreCase))
{

value = JsonConvert.DeserializeObject<BlockValue>(rawPropValue.ToString());
return true;
}
value = default(BlockValue);
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Encodings.Web;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Composing;
Expand Down Expand Up @@ -59,7 +61,15 @@ public virtual void ConvertNestedValuesToString(BlockItemData? contentData)

foreach (var rawPropValue in contentData.RawPropertyValues.Where(x => x.Value != null))
{
contentData.RawPropertyValues[rawPropValue.Key] = rawPropValue.Value?.ToString();
var originalValue = rawPropValue.Value;
if (originalValue.TryConvertToGridItem(out var blockValue))
{
blockValue.ContentData.ForEach(ConvertNestedValuesToString);
blockValue.SettingsData.ForEach(ConvertNestedValuesToString);
contentData.RawPropertyValues[rawPropValue.Key] = JsonConvert.SerializeObject(blockValue);
continue;
}
contentData.RawPropertyValues[rawPropValue.Key] = originalValue.ToString();
}
}
public virtual IPublishedElement? ConvertToElement(BlockItemData data, bool throwOnError)
Expand All @@ -77,8 +87,7 @@ public virtual void ConvertNestedValuesToString(BlockItemData? contentData)

public virtual ViewDataDictionary CreateViewData(object? typedBlockInstance)
{
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) {
Model = typedBlockInstance
};

Expand Down

0 comments on commit 89d1ddf

Please sign in to comment.