-Line endings
This commit is contained in:
@@ -1,27 +1,27 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization and Preserving Object References</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization and Preserving Object References</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>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.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization and Preserving Object References</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization and Preserving Object References</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>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.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p = <span style="color: blue;">new</span> <span style="color: #2b91af;">Person</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
@@ -49,16 +49,16 @@
|
||||
<pre style="margin: 0px;"><span style="color: green;">//]</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
<p>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.</p>
|
||||
|
||||
<h3>PreserveReferencesHandling</h3>
|
||||
<p>Settings PreserveReferencesHandling will track object references when serializing
|
||||
and deserializing JSON.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<p>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.</p>
|
||||
|
||||
<h3>PreserveReferencesHandling</h3>
|
||||
<p>Settings PreserveReferencesHandling will track object references when serializing
|
||||
and deserializing JSON.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(people, <span style="color: #2b91af;">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializerSettings</span> { PreserveReferencesHandling = <span style="color: #2b91af;">PreserveReferencesHandling</span>.Objects });</pre>
|
||||
@@ -92,27 +92,27 @@
|
||||
<pre style="margin: 0px;"><span style="color: green;">// true</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
<p>The first Person in the list is serizlied with the addition of an object Id. The
|
||||
second Person in JSON is now only a reference to the first.</p>
|
||||
<p>With PreserveReferencesHandling on now only one Person object is created on
|
||||
deserialization and the list contains two references to it, mirroring what we
|
||||
started with.</p>
|
||||
|
||||
<h3>IsReference on JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute</h3>
|
||||
|
||||
|
||||
<p>The PreserveReferencesHandling setting on the JsonSerializer will change
|
||||
how all objects are serialized and deserialized. For fine grain control over which
|
||||
objects and members should be serialized as a reference there is the IsReference property on
|
||||
the JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute.</p>
|
||||
<p>Setting IsReference on JsonObjectAttribute or JsonArrayAttribute to true will
|
||||
mean the JsonSerializer will always serialize the type the attribute is against
|
||||
as a reference. Setting IsReference on the JsonPropertyAttribute to true will
|
||||
serialize only that property as a reference.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
|
||||
<p>The first Person in the list is serizlied with the addition of an object Id. The
|
||||
second Person in JSON is now only a reference to the first.</p>
|
||||
<p>With PreserveReferencesHandling on now only one Person object is created on
|
||||
deserialization and the list contains two references to it, mirroring what we
|
||||
started with.</p>
|
||||
|
||||
<h3>IsReference on JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute</h3>
|
||||
|
||||
|
||||
<p>The PreserveReferencesHandling setting on the JsonSerializer will change
|
||||
how all objects are serialized and deserialized. For fine grain control over which
|
||||
objects and members should be serialized as a reference there is the IsReference property on
|
||||
the JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute.</p>
|
||||
<p>Setting IsReference on JsonObjectAttribute or JsonArrayAttribute to true will
|
||||
mean the JsonSerializer will always serialize the type the attribute is against
|
||||
as a reference. Setting IsReference on the JsonPropertyAttribute to true will
|
||||
serialize only that property as a reference.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;">[<span style="color: #2b91af;">JsonObject</span>(IsReference = <span style="color: blue;">true</span>)]</pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">EmployeeReference</span></pre>
|
||||
@@ -121,18 +121,18 @@
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">EmployeeReference</span> Manager { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
<h3>IReferenceResolver</h3>
|
||||
<p>To customize how references are generated and resolved the <a href="./html/T_Newtonsoft_Json_Serialization_IReferenceResolver.htm">IReferenceResolver</a> interface is available to inherit from and use with the JsonSerializer.</p>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
|
||||
</div></div>
|
||||
<h3>IReferenceResolver</h3>
|
||||
<p>To customize how references are generated and resolved the <a href="./html/T_Newtonsoft_Json_Serialization_IReferenceResolver.htm">IReferenceResolver</a> interface is available to inherit from and use with the JsonSerializer.</p>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user