JSON.NET (using the setting PreserveReferencesHandling = PreserveReferencesHandling.Objects
) serializes a reoccurring object inline on first occurrence and serialzes it as JSON reference on subsequent occurrences.
I'm guessing this is done to avoid forward references.
For example:
class Person
{
public string Name { get; set; }
public Person Mother { get; set; }
}
var joni = new Person { Name = "Joni" };
var james = new Person { Name = "James", Mother = joni };
var people = new List<Person> { james, joni };
var json = JsonConvert.SerializeObject(people, Formatting.Indented,
new JsonSerializerSettings {
PreserveReferencesHandling = PreserveReferencesHandling.Objects });
results in the following:
[
{
"$id": "1",
"Name": "James",
"Mother": {
"$id": "2",
"Name": "Joni",
"Mother": null
}
},
{
"$ref": "2"
}
]
Instead, I'd like to get this:
[
{
"$id": "1",
"Name": "James",
"Mother": {
"$ref": "2"
}
},
{
"$id": "2",
"Name": "Joni",
"Mother": null
}
]
Although both are effectively equal, I find the second much more sensible, especially when editing the JSON data by hand (which is a use case for me in this matter).
Is there a way of controlling which instance is serialized as a reference or am I stuck with this first-occurrence behavior?
EDIT
I've tried deserializing the desired JSON and found that JSON.NET doesn't do that properly because of the forward reference.