Preserving Object References
www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm
By default Json.NET will serialize all objects it encounters by value. If a list contains two Person references and both references point to the same object, then the JsonSerializer will write out all the names and values for each reference.
In most cases this is the desired result, but in certain scenarios writing the second item in the list as a reference to the first is a better solution. If the above JSON was deserialized now, then the returned list would contain two completely separate Person objects with the same values. Writing references by value will also cause problems on objects where a circular reference occurs.
PreserveReferencesHandling
Setting PreserveReferencesHandling will track object references when serializing and deserializing JSON.
By default Json.NET will serialize all objects it encounters by value. If a list contains two Person references and both references point to the same object, then the JsonSerializer will write out all the names and values for each reference.
Person p = new Person
2{
3 BirthDate = new DateTime(1980, 12, 23, 0, 0, 0, DateTimeKind.Utc),
4 LastModified = new DateTime(2009, 2, 20, 12, 59, 21, DateTimeKind.Utc),
5 Name = "James"
6};
7
8List<Person> people = new List<Person>();
9people.Add(p);
10people.Add(p);
11
12string json = JsonConvert.SerializeObject(people, Formatting.Indented);
13//[
14// {
15// "Name": "James",
16// "BirthDate": "1980-12-23T00:00:00Z",
17// "LastModified": "2009-02-20T12:59:21Z"
18// },
19// {
20// "Name": "James",
21// "BirthDate": "1980-12-23T00:00:00Z",
22// "LastModified": "2009-02-20T12:59:21Z"
23// }
24//]
In most cases this is the desired result, but in certain scenarios writing the second item in the list as a reference to the first is a better solution. If the above JSON was deserialized now, then the returned list would contain two completely separate Person objects with the same values. Writing references by value will also cause problems on objects where a circular reference occurs.
PreserveReferencesHandling
Setting PreserveReferencesHandling will track object references when serializing and deserializing JSON.
0 комментариев