forked from fluentassertions/fluentassertions.json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJTokenExtensions.cs
More file actions
23 lines (22 loc) · 845 Bytes
/
JTokenExtensions.cs
File metadata and controls
23 lines (22 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Linq;
using Newtonsoft.Json.Linq;
namespace FluentAssertions.Json.Common
{
internal static class JTokenExtensions
{
/// <summary>
/// Recursively sorts the properties of JObject instances by name and
/// the elements of JArray instances by their string representation,
/// producing a normalized JToken for consistent comparison
/// </summary>
public static JToken Normalize(this JToken token)
{
return token switch
{
JObject obj => new JObject(obj.Properties().OrderBy(p => p.Name).Select(p => new JProperty(p.Name, Normalize(p.Value)))),
JArray array => new JArray(array.Select(Normalize).OrderBy(x => x.ToString(Newtonsoft.Json.Formatting.None))),
_ => token
};
}
}
}