0

I have a requirement to generate JSON that is always sorted in the same way.

Using JSON.NET I've implemented this by first serializing the object to a json string then deserealizing into a sorted dictionary, using StringComparer.Ordinal as the sort order (the order I require) and then serializing that to a json string. This works well but only for the set of properties that belong to the top level object.

Once there are sub-objects, sub-dictionaries, sub-lists, etc, this won't work.

I've looked in the documentation but I can't find anything to do with sorting, so my question is:

Is there a way to set JSON.NET to use a particular sort order when serializing to a string?

Paulo Pinto
  • 427
  • 3
  • 10
  • Try OrderedContractResolver: http://stackoverflow.com/a/11309106/298573 – VahidN Sep 14 '14 at 09:52
  • I'm afraid there is no built-in way to do it. The [answer](https://stackoverflow.com/a/3425631/102112) to a similar question confirms this. So the only way to do it is to do it manually (maybe via recursion) I think. – Oleks Mar 26 '12 at 23:35

0 Answers0