PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Json45r7/Source/Doc/PreserveObjectReferences.html

https://bitbucket.org/wantstudios/bitbucketclient
HTML | 138 lines | 116 code | 22 blank | 0 comment | 0 complexity | a4ea1731b8530682ef772bf37e75a7bb MD5 | raw file
  1. <html>
  2. <head>
  3. <title>Serialization and Preserving Object References</title>
  4. <link href="styles.css" rel="stylesheet" type="text/css" />
  5. <link href="custom.css" rel="stylesheet" type="text/css" />
  6. </head>
  7. <body>
  8. <div id="control">
  9. <span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
  10. <span class="topicTitle">Serialization and Preserving Object References</span></div>
  11. <div id="content">
  12. <span style="color: DarkGray"> </span>
  13. <p>By default Json.NET will serialize all objects it encounters by value. If a list
  14. contains two Person references, and both references point to the same object
  15. then the JsonSerializer will write out all the names and values for each
  16. reference.</p>
  17. <div class="overflowpanel"> <div class="code">
  18. <div style="font-family: Courier New; font-size: 10pt; color: black;">
  19. <pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p = <span style="color: blue;">new</span> <span style="color: #2b91af;">Person</span></pre>
  20. <pre style="margin: 0px;">&nbsp; {</pre>
  21. <pre style="margin: 0px;">&nbsp; &nbsp; BirthDate = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(1980, 12, 23, 0, 0, 0, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
  22. <pre style="margin: 0px;">&nbsp; &nbsp; LastModified = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2009, 2, 20, 12, 59, 21, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
  23. <pre style="margin: 0px;">&nbsp; &nbsp; Name = <span style="color: #a31515;">"James"</span></pre>
  24. <pre style="margin: 0px;">&nbsp; };</pre>
  25. <pre style="margin: 0px;">&nbsp;</pre>
  26. <pre style="margin: 0px;"><span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Person</span>&gt; people = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Person</span>&gt;();</pre>
  27. <pre style="margin: 0px;">people.Add(p);</pre>
  28. <pre style="margin: 0px;">people.Add(p);</pre>
  29. <pre style="margin: 0px;">&nbsp;</pre>
  30. <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>
  31. <pre style="margin: 0px;"><span style="color: green;">//[</span></pre>
  32. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; {</span></pre>
  33. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Name": "James",</span></pre>
  34. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "BirthDate": "\/Date(346377600000)\/",</span></pre>
  35. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "LastModified": "\/Date(1235134761000)\/"</span></pre>
  36. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; },</span></pre>
  37. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; {</span></pre>
  38. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Name": "James",</span></pre>
  39. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "BirthDate": "\/Date(346377600000)\/",</span></pre>
  40. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "LastModified": "\/Date(1235134761000)\/"</span></pre>
  41. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; }</span></pre>
  42. <pre style="margin: 0px;"><span style="color: green;">//]</span></pre>
  43. </div>
  44. </div></div>
  45. <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.
  46. If the above JSON was deserialized now then the returned list would contain two
  47. completely separate Person objects with the same values. Writing references by value will also cause problems on objects where a
  48. circular reference occurs.</p>
  49. <h3>PreserveReferencesHandling</h3>
  50. <p>Settings PreserveReferencesHandling will track object references when serializing
  51. and deserializing JSON.</p>
  52. <div class="overflowpanel"> <div class="code">
  53. <div style="font-family: Courier New; font-size: 10pt; color: black;">
  54. <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>
  55. <pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializerSettings</span> { PreserveReferencesHandling = <span style="color: #2b91af;">PreserveReferencesHandling</span>.Objects });</pre>
  56. <pre style="margin: 0px;"><span style="color: green;">//[</span></pre>
  57. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; {</span></pre>
  58. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "$id": "1",</span></pre>
  59. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Name": "James",</span></pre>
  60. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "BirthDate": "\/Date(346377600000)\/",</span></pre>
  61. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "LastModified": "\/Date(1235134761000)\/"</span></pre>
  62. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; },</span></pre>
  63. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; {</span></pre>
  64. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "$ref": "1"</span></pre>
  65. <pre style="margin: 0px;"><span style="color: green;">//&nbsp; }</span></pre>
  66. <pre style="margin: 0px;"><span style="color: green;">//]</span></pre>
  67. <pre style="margin: 0px;">&nbsp;</pre>
  68. <pre style="margin: 0px;"><span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Person</span>&gt; deserializedPeople = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Person</span>&gt;&gt;(json,</pre>
  69. <pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializerSettings</span> { PreserveReferencesHandling = <span style="color: #2b91af;">PreserveReferencesHandling</span>.Objects });</pre>
  70. <pre style="margin: 0px;">&nbsp;</pre>
  71. <pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(deserializedPeople.Count);</pre>
  72. <pre style="margin: 0px;"><span style="color: green;">// 2</span></pre>
  73. <pre style="margin: 0px;">&nbsp;</pre>
  74. <pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p1 = deserializedPeople[0];</pre>
  75. <pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p2 = deserializedPeople[1];</pre>
  76. <pre style="margin: 0px;">&nbsp;</pre>
  77. <pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p1.Name);</pre>
  78. <pre style="margin: 0px;"><span style="color: green;">// James</span></pre>
  79. <pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p2.Name);</pre>
  80. <pre style="margin: 0px;"><span style="color: green;">// James</span></pre>
  81. <pre style="margin: 0px;">&nbsp;</pre>
  82. <pre style="margin: 0px;"><span style="color: blue;">bool</span> equal = <span style="color: #2b91af;">Object</span>.ReferenceEquals(p1, p2);</pre>
  83. <pre style="margin: 0px;"><span style="color: green;">// true</span></pre>
  84. </div>
  85. </div></div>
  86. <p>The first Person in the list is serizlied with the addition of an object Id. The
  87. second Person in JSON is now only a reference to the first.</p>
  88. <p>With PreserveReferencesHandling on now only one Person object is created on
  89. deserialization and the list contains two references to it, mirroring what we
  90. started with.</p>
  91. <h3>IsReference on JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute</h3>
  92. <p>The PreserveReferencesHandling setting on the JsonSerializer will change
  93. how all objects are serialized and deserialized. For fine grain control over which
  94. objects and members should be serialized as a reference there is the IsReference property on
  95. the JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute.</p>
  96. <p>Setting IsReference on JsonObjectAttribute or JsonArrayAttribute to true will
  97. mean the JsonSerializer will always serialize the type the attribute is against
  98. as a reference. Setting IsReference on the JsonPropertyAttribute to true will
  99. serialize only that property as a reference.</p>
  100. <div class="overflowpanel"> <div class="code">
  101. <div style="font-family: Courier New; font-size: 10pt; color: black;">
  102. <pre style="margin: 0px;">[<span style="color: #2b91af;">JsonObject</span>(IsReference = <span style="color: blue;">true</span>)]</pre>
  103. <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">EmployeeReference</span></pre>
  104. <pre style="margin: 0px;">{</pre>
  105. <pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
  106. <pre style="margin: 0px;">&nbsp; <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>
  107. <pre style="margin: 0px;">}</pre>
  108. </div>
  109. </div></div>
  110. <h3>IReferenceResolver</h3>
  111. <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>
  112. <div id="footer">
  113. </div>
  114. </div>
  115. </body>
  116. </html>