Short and sweet:
Is there a way to specify the order of fields in a serialized JSON object using JSON.NET?
It would be sufficient to specify that a single field always appear first.
Short and sweet:
Is there a way to specify the order of fields in a serialized JSON object using JSON.NET?
It would be sufficient to specify that a single field always appear first.
There's no order of fields in the JSON format so defining an order doesn't make sense.
{ id: 1, name: 'John' }
is equivalent to { name: 'John', id: 1 }
(both represent a strictly equivalent object instance)
I followed the JsonConvert.SerializeObject(key)
method call via reflection (where key was an IList) and found that JsonSerializerInternalWriter.SerializeList gets called. It takes a list and loops through via
for (int i = 0; i < values.Count; i++) { ...
where values is the IList parameter brought in.
Short answer is...No, there's no built in way to set the order the fields are listed in the JSON string.