Skip to content

Commit

Permalink
JSON Formatter was not sorting array values. (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
veler committed Jul 23, 2022
1 parent 2a2211b commit baedeaf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/dev/impl/DevToys/Helpers/JsonYaml/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ private static void SortJsonPropertiesAlphabetically(JObject jObject)
{
SortJsonPropertiesAlphabetically(obj);
}
else if (property.Value is JArray array)
{
foreach (JToken? arrayItem in array)
{
if (arrayItem is JObject arrayObj)
{
SortJsonPropertiesAlphabetically(arrayObj);
}
}
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/tests/DevToys.Tests/Helpers/JsonHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public void FormatDoesNotAlterateDateTimes(string input, string expectedResult)
}

[DataTestMethod]
[DataRow("{\"Email\":\"john@mail.com\",\"Website\":\"http://samplewebsite.com\",\"Name\":\"John Smith\",\"Phone\":\"(50)345872\"}", "{\"Email\":\"john@mail.com\",\"Name\":\"John Smith\",\"Phone\":\"(50)345872\",\"Website\":\"http://samplewebsite.com\"}")]
[DataRow(
"{\"a\": \"asdf\", \"c\" : 545, \"b\": 33, \"array\": [{\"a\": \"asdf\", \"c\" : 545, \"b\": 33, \"array\": []}]}",
"{\"a\":\"asdf\",\"array\":[{\"a\":\"asdf\",\"array\":[],\"b\":33,\"c\":545}],\"b\":33,\"c\":545}")]
public void FormatSortPropertiesAlphabetically(string input, string expectedResult)
{
Assert.AreEqual(expectedResult, JsonHelper.Format(input, Indentation.Minified, sortProperties: true));
Expand Down

0 comments on commit baedeaf

Please sign in to comment.