From 023ccbb87c86778c68982c31bd31d1ac2efc783e Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Tue, 10 Dec 2024 10:51:30 +0000 Subject: [PATCH] tidy up Json Conversion --- .../DataTypes/ConfigurationSerializerBase.cs | 2 +- uSync.Core/Extensions/ChangeListExtensions.cs | 4 ++-- uSync.Core/Extensions/JsonExtensions.cs | 17 +++++++++++++++++ uSync.Core/uSyncConstants.cs | 10 +++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/uSync.Core/DataTypes/ConfigurationSerializerBase.cs b/uSync.Core/DataTypes/ConfigurationSerializerBase.cs index 1e314293..f168d91e 100644 --- a/uSync.Core/DataTypes/ConfigurationSerializerBase.cs +++ b/uSync.Core/DataTypes/ConfigurationSerializerBase.cs @@ -13,7 +13,7 @@ public virtual object DeserializeConfig(string config, Type configType) public virtual string SerializeConfig(object configuration) { - return JsonConvert.SerializeObject(configuration, Formatting.Indented); + return JsonConvert.SerializeObject(configuration, Formatting.Indented, uSyncConstants.uSyncJsonSettings); } } diff --git a/uSync.Core/Extensions/ChangeListExtensions.cs b/uSync.Core/Extensions/ChangeListExtensions.cs index c430578f..135502cb 100644 --- a/uSync.Core/Extensions/ChangeListExtensions.cs +++ b/uSync.Core/Extensions/ChangeListExtensions.cs @@ -37,8 +37,8 @@ public static void AddUpdateJson(this List changes, string name, ob public static void AddUpdateJson(this List changes, string name, object oldValue, object newValue, string path, bool success) { - var oldJson = JsonConvert.SerializeObject(oldValue, Formatting.Indented); - var newJson = JsonConvert.SerializeObject(newValue, Formatting.Indented); + var oldJson = JsonConvert.SerializeObject(oldValue, Formatting.Indented, uSyncConstants.uSyncJsonSettings); + var newJson = JsonConvert.SerializeObject(newValue, Formatting.Indented, uSyncConstants.uSyncJsonSettings); AddUpdate(changes, name, oldJson, newJson, path, success); } diff --git a/uSync.Core/Extensions/JsonExtensions.cs b/uSync.Core/Extensions/JsonExtensions.cs index 2c49d611..825203e8 100644 --- a/uSync.Core/Extensions/JsonExtensions.cs +++ b/uSync.Core/Extensions/JsonExtensions.cs @@ -1,6 +1,8 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using System.Linq; + using Umbraco.Extensions; namespace uSync.Core @@ -181,5 +183,20 @@ public static TObject GetValueAs(this object value) public static bool IsAngularExpression(this string value) => value.StartsWith("{{") && value.EndsWith("}}"); + + /// + /// performs an inplace sort of the properties in a JObject + /// + public static void SortByName(this JObject item) + { + var properties = item.Properties().OrderBy(x => x.Name).ToList(); + item.RemoveAll(); + foreach (var property in properties) + { + item.Add(property); + } + } + + } } \ No newline at end of file diff --git a/uSync.Core/uSyncConstants.cs b/uSync.Core/uSyncConstants.cs index 74a0ebf2..26293507 100644 --- a/uSync.Core/uSyncConstants.cs +++ b/uSync.Core/uSyncConstants.cs @@ -1,7 +1,15 @@ -namespace uSync.Core +using Newtonsoft.Json; +using uSync.Core.Json; + +namespace uSync.Core { public static partial class uSyncConstants { + public static JsonSerializerSettings uSyncJsonSettings = new JsonSerializerSettings() + { + ContractResolver = new uSyncContractResolver() + }; + public static class Xml { public const string Key = "Key";