Skip to content

Commit

Permalink
tidy up Json Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump committed Dec 10, 2024
1 parent b267b2f commit 023ccbb
Show file tree
Hide file tree
Showing 4 changed files with 29 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);
}
}


}
}
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 023ccbb

Please sign in to comment.