1

I have an object which I use NewtonSoft JSON to deserialize to string

Jsonstring = JsonConvert.SerializeObject(InfoObject)

Is there a way I can convert to Canonicalized JSON string. I couldn't find anyway with NewtonSoft JSON.

The sorting needs to be case sensitive. So “orgExtra” should appear before “organization” because a capital “E” is before a lower case “a”. Also, it needs to flatten the JSON structure, so

{ “organization” : “exampleorg”,
“fName” : “name of person”,
“orgExtra” : {
“orgLocator” : “Bldg1”,
“desg” : “Mgr”
}

should be

“fName:name of person” + “\n” + “orgExtra.desg:Mgr” + “\n” + “orgExtra.orgLocator:Bldg1” + “\n” + “organization:exampleorg” + “\n”

Can this Canonicalization be done with NewtonSoft?

  • `IContractResolver` provides the method to create the Properties, which is (of course) `CreateProperties()` :) You can implement it and generate a specific order yourself (I assume you mean to order the properties by their name, since Json.Net already produces a canonical form - except the order). In practice, you can create a custom ContractResolver deriving from `DefaultContractResolver` and override `CreateProperties()`. – Jimi Apr 19 '20 at 17:37
  • You can serialize properties in alphabetical order using a custom contract resolver as shown in [this github comment](https://github.com/JamesNK/Newtonsoft.Json/issues/1688#issuecomment-387624939) and also [How to sort properties alphabetically when serializing JSON using Netwonsoft library?](https://stackoverflow.com/q/56933494). You will still need to sort dictionary and dynamic properties alphabetically though, which would require a custom converter. – dbc Apr 19 '20 at 19:31
  • Jimi and dbc, can you please check the question again because I added some more information. I tried with JObject but it doesn't sort on CASE sensitive names. Same with default contract resolver. Somehow Python has it implemented out of box. – Shweta Mahesh Apr 19 '20 at 21:43
  • When you override `CreateProperties()` (in a custom class derived from `DefaultContractResolver`), you have to return (IIRC) an `IList`. You can OrderBy() the list of properties, returning `base.CreateProperties(..., ...).OrderBy(prop => prop.PropertyName).ToList()` (or similar, I can't test it now). – Jimi Apr 19 '20 at 22:11
  • Yes that's right. `JObject` represents an *already serialized JSON literal* and so Json.NET writes it to a string unchanged. You need to apply the contract resolver when you create the `JObject` not when you output it. You would need to write your own code to sort the properties of a `JObject`. What type of object is `InfoObject` by the way? – dbc Apr 19 '20 at 22:57
  • Your sample JSON is malformed by the way, it uses left and right double quotation marks ` “` and `”` rather than the neutral double quotation mark `"`. – dbc Apr 19 '20 at 23:01
  • dbc - thanks for your response. InfoObject is the C# class object which is converted to JSON. Also, the " was because I copied the large JSON string and edited it to just give idea of structure. – Shweta Mahesh Apr 19 '20 at 23:23

0 Answers0