Skip to content

Commit

Permalink
V13/j object sorting (#695)
Browse files Browse the repository at this point in the history
* Sort JObject properties in nested/blocks on export

* tidy up Json Conversion
  • Loading branch information
KevinJump authored Dec 19, 2024
1 parent 3e97c90 commit bc662f6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion uSync.Core/DataTypes/ConfigurationSerializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down
4 changes: 2 additions & 2 deletions uSync.Core/Extensions/ChangeListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static void AddUpdateJson(this List<uSyncChange> changes, string name, ob

public static void AddUpdateJson(this List<uSyncChange> 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);
}

Expand Down
17 changes: 17 additions & 0 deletions uSync.Core/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using System.Linq;

using Umbraco.Extensions;

namespace uSync.Core
Expand Down Expand Up @@ -181,5 +183,20 @@ public static TObject GetValueAs<TObject>(this object value)

public static bool IsAngularExpression(this string value)
=> value.StartsWith("{{") && value.EndsWith("}}");

/// <summary>
/// performs an inplace sort of the properties in a JObject
/// </summary>
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);
}
}


}
}
3 changes: 3 additions & 0 deletions uSync.Core/Mapping/SyncNestedValueMapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ protected JObject GetImportProperties(JObject item, IContentType docType)
/// </summary>
protected JObject GetExportProperties(JObject item, IContentType docType)
{
// we want JObjects to be sorted.
item.SortByName();

foreach (var property in docType.CompositionPropertyTypes)
{
if (item.ContainsKey(property.Alias))
Expand Down
10 changes: 9 additions & 1 deletion uSync.Core/uSyncConstants.cs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit bc662f6

Please sign in to comment.