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 authored Sep 12, 2023
1 parent d6908a8 commit 6f7c587
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 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: 25 additions & 1 deletion uSync.Core/Mapping/Mappers/BlockListMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;

using uSync.Core.Dependency;

Expand Down Expand Up @@ -37,6 +37,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
34 changes: 23 additions & 11 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 Expand Up @@ -183,7 +195,7 @@ protected IContentType GetDocTypeByKey(JObject json, string keyAlias)
var attempt = json[keyAlias].TryConvertTo<Guid>();
if (attempt.Success)
{
return mapperCollection.Value?.EntityCache.GetContentType(attempt.Result)
return mapperCollection.Value?.EntityCache.GetContentType(attempt.Result)
?? contentTypeService.Get(attempt.Result);
}
}
Expand All @@ -194,7 +206,7 @@ protected IContentType GetDocTypeByKey(JObject json, string keyAlias)
protected IContentType GetDocType(string alias)
{
if (string.IsNullOrWhiteSpace(alias)) return default;
return mapperCollection.Value?.EntityCache.GetContentType(alias)
return mapperCollection.Value?.EntityCache.GetContentType(alias)
?? contentTypeService.Get(alias);
}

Expand Down

0 comments on commit 6f7c587

Please sign in to comment.