The order in which a type's members are serialized to JSON is based on the order in which the .NET runtime returns them. With the current C# 5.0 compiler and the current 4.5 version of the CLR this order is deterministic, but there is guarantee that this
will change in the future, as Eric Lippert
For the correctness of the generated JSON, the order in which those members are presented does not matter, but if users depend on a certain order (as some unit tests of the JSON.NET codebase already do) their application might fail in the future.
Since JSON.NET is an amazingly powerful tool that can serialize about anything without a problem, it becomes rather interesting to use JSON.NET for creating keys. For instance, consider a DTO message that holds the data for a query to be executed. This message
can be serialized to JSON and the resulting string can be used as key in a cache. If this key is hashed it could even be used as a file name where the content of the file is the serialized result of the query. This would effectively enable the use of those
DTO messages as key in a cache, without the need for each of them to override equality and even allows the cache to outlive the application instance.
With the current version of the C# compiler and the framework this works out rather well, but this will break in future versions of the platform, where the order in which members are returned might change every time the application is restarted.
What I'd like is that either:
- JSON.NET guarantees the order of the serialized member (for instance by sorting them by name in the DefaultContractResolver.GetSerializableMembers method)
- Allow users to extend the framework in a way that they can force a particular order in which the members are serialized.
- Let JSON.NET explicitly state that the order of the members is undeterministic and can change every time the application is recompiled or even restarted. (this is of course the least attractive option).