From 6f7c58732ac9532f521693b11bc35fce48db5b6d Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Tue, 12 Sep 2023 15:18:24 +0100 Subject: [PATCH] V12/block grid independence (#539) * BlockLists, not escaping JSON in their own properties. * Format nicely on the way out (mangle it on the way in) --- uSync.Core/Extensions/JsonExtensions.cs | 5 +-- uSync.Core/Mapping/Mappers/BlockListMapper.cs | 26 +++++++++++++- .../Mapping/SyncNestedValueMapperBase.cs | 34 +++++++++++++------ 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/uSync.Core/Extensions/JsonExtensions.cs b/uSync.Core/Extensions/JsonExtensions.cs index 0038027b..754c31a2 100644 --- a/uSync.Core/Extensions/JsonExtensions.cs +++ b/uSync.Core/Extensions/JsonExtensions.cs @@ -115,7 +115,8 @@ private static JToken ExpandStringValue(string stringValue) public static JToken GetJTokenFromObject(this object value) { var stringValue = value.GetValueAs(); - if (string.IsNullOrWhiteSpace(stringValue) || !stringValue.DetectIsJson()) return null; + if (string.IsNullOrWhiteSpace(stringValue) || !stringValue.DetectIsJson()) + return stringValue; try { @@ -128,7 +129,7 @@ public static JToken GetJTokenFromObject(this object value) } } - private static TObject GetValueAs(this object value) + public static TObject GetValueAs(this object value) { if (value == null) return default; var attempt = value.TryConvertTo(); diff --git a/uSync.Core/Mapping/Mappers/BlockListMapper.cs b/uSync.Core/Mapping/Mappers/BlockListMapper.cs index e8e0dccb..8cd597e6 100644 --- a/uSync.Core/Mapping/Mappers/BlockListMapper.cs +++ b/uSync.Core/Mapping/Mappers/BlockListMapper.cs @@ -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; @@ -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(); + 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 GetPropertiesMethod) { if (jsonValue is JObject jObjectValue) diff --git a/uSync.Core/Mapping/SyncNestedValueMapperBase.cs b/uSync.Core/Mapping/SyncNestedValueMapperBase.cs index bbb2eeb2..18f796ae 100644 --- a/uSync.Core/Mapping/SyncNestedValueMapperBase.cs +++ b/uSync.Core/Mapping/SyncNestedValueMapperBase.cs @@ -39,6 +39,19 @@ public SyncNestedValueMapperBase(IEntityService entityService, this.dataTypeService = dataTypeService; } + /// + /// Gets the import property representation as a JToken + /// + /// + /// this usually is a bit of nested json, but sometimes + /// some properties want it to be json serialized into a string. + /// + protected virtual JToken GetImportProperty(object value) + => value?.ToString().GetJsonTokenValue() ?? null; + + protected virtual JToken GetExportProperty(string value) + => value; + /// /// Get the import value for properties used in the this JObject /// @@ -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); } } } @@ -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); } } } @@ -108,14 +120,14 @@ protected IEnumerable GetPropertyDependencies(JObject value, protected IEnumerable GetPropertyDependencies( IDictionary properties, DependencyFlags flags) { - var dependencies = new List(); - if (properties.Any()) + if (!properties.Any()) + return Enumerable.Empty(); + + var dependencies = new List(); + 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; @@ -183,7 +195,7 @@ protected IContentType GetDocTypeByKey(JObject json, string keyAlias) var attempt = json[keyAlias].TryConvertTo(); if (attempt.Success) { - return mapperCollection.Value?.EntityCache.GetContentType(attempt.Result) + return mapperCollection.Value?.EntityCache.GetContentType(attempt.Result) ?? contentTypeService.Get(attempt.Result); } } @@ -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); }