Skip to content

Commit

Permalink
V12/block grid independence (#539)
Browse files Browse the repository at this point in the history
* BlockLists, not escaping JSON in their own properties.

* Format nicely on the way out (mangle it on the way in)
  • Loading branch information
KevinJump committed Sep 22, 2023
1 parent d1d315d commit b712a9b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
5 changes: 3 additions & 2 deletions uSync.Core/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ private static JToken ExpandStringValue(string stringValue)
public static JToken GetJTokenFromObject(this object value)
{
var stringValue = value.GetValueAs<string>();
if (string.IsNullOrWhiteSpace(stringValue) || !stringValue.DetectIsJson()) return null;
if (string.IsNullOrWhiteSpace(stringValue) || !stringValue.DetectIsJson())
return stringValue;

try
{
Expand All @@ -128,7 +129,7 @@ public static JToken GetJTokenFromObject(this object value)
}
}

private static TObject GetValueAs<TObject>(this object value)
public static TObject GetValueAs<TObject>(this object value)
{
if (value == null) return default;
var attempt = value.TryConvertTo<TObject>();
Expand Down
26 changes: 26 additions & 0 deletions uSync.Core/Mapping/Mappers/BlockListMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;

using Umbraco.Extensions;

using uSync.Core.Dependency;

namespace uSync.Core.Mapping
Expand Down Expand Up @@ -37,6 +39,30 @@ public BlockListMapper(IEntityService entityService,
"Umbraco.BlockGrid"
};

protected override JToken GetImportProperty(object value)
{
if (value == null) return null;

var stringValue = value.GetValueAs<string>();
if (stringValue == null || !stringValue.DetectIsJson())
return stringValue;

// we have to get the json, the serialize the json,
// this is to make sure we don't serizlize any formatting
// (like indented formatting). because that would
// register changes that are not there.
var b = JsonConvert.SerializeObject(value.GetJTokenFromObject(), Formatting.None);

return b;
}


protected override JToken GetExportProperty(string value)
{
if (string.IsNullOrWhiteSpace(value) || !value.DetectIsJson()) return value;
return value.GetJsonTokenValue();
}

protected override string ProcessValues(JToken jsonValue, string editorAlias, Func<JObject, IContentType, JObject> GetPropertiesMethod)
{
if (jsonValue is JObject jObjectValue)
Expand Down
30 changes: 21 additions & 9 deletions uSync.Core/Mapping/SyncNestedValueMapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public SyncNestedValueMapperBase(IEntityService entityService,
this.dataTypeService = dataTypeService;
}

/// <summary>
/// Gets the import property representation as a JToken
/// </summary>
/// <remarks>
/// this usually is a bit of nested json, but sometimes
/// some properties want it to be json serialized into a string.
/// </remarks>
protected virtual JToken GetImportProperty(object value)
=> value?.ToString().GetJsonTokenValue() ?? null;

protected virtual JToken GetExportProperty(string value)
=> value;

/// <summary>
/// Get the import value for properties used in the this JObject
/// </summary>
Expand All @@ -52,8 +65,7 @@ protected JObject GetImportProperties(JObject item, IContentType docType)
if (value != null)
{
var mappedVal = mapperCollection.Value.GetImportValue(value.ToString(), property.PropertyEditorAlias);
item[property.Alias] = // mappedVal?.ToString() ?? null;
mappedVal?.ToString().GetJsonTokenValue() ?? null;
item[property.Alias] = GetImportProperty(mappedVal);
}
}
}
Expand All @@ -74,7 +86,7 @@ protected JObject GetExportProperties(JObject item, IContentType docType)
if (value != null)
{
var mappedVal = mapperCollection.Value.GetExportValue(value, property.PropertyEditorAlias);
item[property.Alias] = mappedVal; // .GetJsonTokenValue();
item[property.Alias] = GetExportProperty(mappedVal);
}
}
}
Expand Down Expand Up @@ -108,14 +120,14 @@ protected IEnumerable<uSyncDependency> GetPropertyDependencies(JObject value,
protected IEnumerable<uSyncDependency> GetPropertyDependencies(
IDictionary<string, object> properties, DependencyFlags flags)
{
var dependencies = new List<uSyncDependency>();

if (properties.Any())
if (!properties.Any())
return Enumerable.Empty<uSyncDependency>();

var dependencies = new List<uSyncDependency>();
foreach (var property in properties)
{
foreach (var property in properties)
{
dependencies.AddRange(mapperCollection.Value.GetDependencies(property.Value, property.Key, flags));
}
dependencies.AddRange(mapperCollection.Value.GetDependencies(property.Value, property.Key, flags));
}

return dependencies;
Expand Down
2 changes: 1 addition & 1 deletion uSync.Core/uSyncConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class Xml
// this is our format 'version' -
// it only changes when the format of the .config files change
// we use it to prompt people to do an uptodate export.
public const string FormatVersion = "10.1.0";
public const string FormatVersion = "10.7.0";

/// <summary>
/// names of the root xml elements that are seralized in/out
Expand Down

0 comments on commit b712a9b

Please sign in to comment.