RobTest.JSorter
0.1.0
dotnet add package RobTest.JSorter --version 0.1.0
NuGet\Install-Package RobTest.JSorter -Version 0.1.0
<PackageReference Include="RobTest.JSorter" Version="0.1.0" />
<PackageVersion Include="RobTest.JSorter" Version="0.1.0" />
<PackageReference Include="RobTest.JSorter" />
paket add RobTest.JSorter --version 0.1.0
#r "nuget: RobTest.JSorter, 0.1.0"
#addin nuget:?package=RobTest.JSorter&version=0.1.0
#tool nuget:?package=RobTest.JSorter&version=0.1.0
JSorter
Sort all the JSON
Disclaimer: This tool is designed in mind for checking JSON as part of Quality Assurance for the sake of the human viewing JSON. Your own code should not be reliant on how things are ordered. Also keep in mind that order can matter in JSON and it's up to you to decide how the tool will be used.
Downloads
The latest stable release of JSorter is available on NuGet or can be downloaded from GitHub.
Using JSorter
To order using default configuration is a simple to use extension method
using JSorter;
var a = JObject.Parse(@"{ ""b"" : ""b"", ""a"": ""a"" }");
a = a.Sort();
Console.Write(a.ToString());
{
"a": "a",
"b": "b"
}
Configuration
Sorting Primitive Values
By default sorting primitive values in Json is disabled, this is due to the fact that the sequence of primitives may matter
This can be enabled via configuration passed as an additional argument when using the Sort()
method
Example
using JSorter;
using JSorter.Configuration;
jTest = jTest.Sort(new JSorterConfiguration() {
SortPrimitiveValuesInArrays = true
}
var a = JArray.Parse(@"[2,1,3]");
a = a.Sort();
Console.Write(a.ToString());
Outputs
[ 1,2,3 ]
Sorting JSON Objects in an Array by a Key.
By default, objects belonging to a JSON array are sorted by there string representation. However this may cause problems if the first field changes You can use a selector to use as a sorting value for arrays.
Example
using JSorter;
using JSorter.Configuration;
jTest = jTest.Sort(new JSorterConfiguration() {
SortArrayObjectBy = new List<string>() { "id" }
}
var a = JArray.Parse(@"[{""a"": 1, ""id"":""2""},{""b"": 1, ""id"":""1""}]");
a = a.Sort();
Console.Write(a.ToString());
Outputs
[
{
"b" : 1,
"id": 1
},
{
"a": 1,
"id": 2
}
]
Disable Property Sorting
In some scenarios you may not want to sort the properties of an object.
The sorting of properties can be disabled using the SortJsonObjectProperties
flag.
using JSorter;
using JSorter.Configuration;
jTest = jTest.Sort(new JSorterConfiguration() {
SortArrayObjectBy = new List<string>() { "id" },
SortJsonObjectProperties = false
}
var a = JArray.Parse(@"[{""k"": 1, ""id"":""2""},{""l"": 1, ""id"":""1""}]");
a = a.Sort();
Console.Write(a.ToString());
Outputs
[
{
"l" : 1,
"id": 1
},
{
"k": 1,
"id": 2
}
]
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net6.0
- NewtonSoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.0 | 200 | 8/7/2023 |