Git Product home page Git Product logo

jsorter's Introduction

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
  }
]

jsorter's People

Contributors

robg-test avatar

Stargazers

Joss Sparkes avatar

Watchers

 avatar

jsorter's Issues

Add ability to not sort JSON Array values at all

Reason: I believe order can matter for Arrays no matter what the object type is.
This means that ordering an array may be incorrect if the user wants to solely just sort property keys.

Proposal Add a configuration flag to disable all array sorting then disable array sorting by reading from that flag

Notes: You still want to sort any arrays objects properties in this scenario!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.