-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7feca97
commit 7f23ee7
Showing
6 changed files
with
312 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Linq; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace WireMockInspector.ViewModels; | ||
|
||
public class AlphabeticallySortedJsonDiffFormatter | ||
{ | ||
public static readonly AlphabeticallySortedJsonDiffFormatter Instance = new AlphabeticallySortedJsonDiffFormatter(); | ||
|
||
public string Format(JToken? diff) | ||
{ | ||
if (diff == null) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
if (diff is JObject jObject) | ||
{ | ||
if(jObject.DeepClone() is JObject sorted) | ||
{ | ||
Sort(sorted); | ||
return sorted.ToString(); | ||
} | ||
|
||
return jObject.ToString(); | ||
} | ||
return diff.ToString(); | ||
} | ||
|
||
private static void Sort(JObject jObj) | ||
{ | ||
var props = jObj.Properties().ToList(); | ||
foreach (var prop in props) | ||
{ | ||
prop.Remove(); | ||
} | ||
|
||
foreach (var prop in props.OrderBy(p => p.Name)) | ||
{ | ||
jObj.Add(prop); | ||
if (prop.Value is JObject) | ||
Sort((JObject)prop.Value); | ||
} | ||
} | ||
} | ||
|
||
public static class JsonHelper | ||
{ | ||
public static string ToComparableForm(string json) | ||
{ | ||
try | ||
{ | ||
return AlphabeticallySortedJsonDiffFormatter.Instance.Format(JToken.Parse(json)); | ||
} | ||
catch (Exception e) | ||
{ | ||
return json; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.