| PreserveReferencesHandling Enumeration |
Specifies reference handling options for the
JsonSerializer.
Note that references cannot be preserved when a value is set via a non-default constructor such as types that implement
ISerializable.
Namespace:
Newtonsoft.Json
Assembly:
Newtonsoft.Json (in Newtonsoft.Json.dll) Version: 12.0.1+509643a8952ce731e0207710c429ad6e67dc43db
Syntax [FlagsAttribute]
public enum PreserveReferencesHandling
Members
| Member name | Value | Description |
---|
| None | 0 |
Do not preserve references when serializing types.
|
| Objects | 1 |
Preserve references when serializing into a JSON object structure.
|
| Arrays | 2 |
Preserve references when serializing into a JSON array structure.
|
| All | 3 |
Preserve references when serializing.
|
Examples Preserve Object References
string json = JsonConvert.SerializeObject(people, Formatting.Indented,
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
List<Person> deserializedPeople = JsonConvert.DeserializeObject<List<Person>>(json,
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
Console.WriteLine(deserializedPeople.Count);
Person p1 = deserializedPeople[0];
Person p2 = deserializedPeople[1];
Console.WriteLine(p1.Name);
Console.WriteLine(p2.Name);
bool equal = Object.ReferenceEquals(p1, p2);
See Also