Subversion Repository Public Repository

ChrisCompleteCodeTrunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?xml version="1.0" encoding="utf-8"?>
<topic id="PreserveObjectReferences" revisionNumber="1">
  <developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
    <!--
    <summary>
      <para>Optional summary abstract</para>
    </summary>
    -->
    <introduction>
      <para>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.</para>
      
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="PreservingObjectReferencesOff" title="Preserve Object References Off" />
      
      <para>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.</para>
    </introduction>
    <!-- Add one or more top-level section elements.  These are collapsible.
         If using <autoOutline />, add an address attribute to identify it
         and specify a title so that it can be jumped to with a hyperlink. -->
    <section>
      <title>PreserveReferencesHandling</title>
      <content>
        <para>Setting <codeEntityReference>T:Newtonsoft.Json.PreserveReferencesHandling</codeEntityReference> will track object references
        when serializing and deserializing JSON.</para>
        
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="PreservingObjectReferencesOn" title="Preserve Object References On" />       
        
        <para>The first Person in the list is serialized with the addition of an
        object ID. The second Person in JSON is now only a reference to the first.</para>
        <para>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.</para>
<alert class="note">
  <para>References cannot be preserved when a value is set via a non-default constructor.
        With a non-default constructor, child values must be created before the parent value so they can be passed into
        the constructor, making tracking reference impossible.
        <codeEntityReference>T:System.Runtime.Serialization.ISerializable</codeEntityReference> types are an example
        of a class whose values are populated with a non-default constructor and won't work with PreserveReferencesHandling.</para>
</alert>
      </content>
    </section>
    
    <section>
      <title>IsReference</title>
      <content>
        <para>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.</para>
        
        <para>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.</para>

<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="PreservingObjectReferencesAttribute" title="IsReference" />
      </content>
    </section>
    
    <section>
      <title>IReferenceResolver</title>
      <content>
        <para>To customize how references are generated and resolved the
        <codeEntityReference>T:Newtonsoft.Json.Serialization.IReferenceResolver</codeEntityReference>
        interface is available to inherit from and use with
        the JsonSerializer.</para>
      </content>
    </section>
    <relatedTopics>
      <codeEntityReference>T:Newtonsoft.Json.PreserveReferencesHandling</codeEntityReference>
    </relatedTopics>
  </developerConceptualDocument>
</topic>

Commits for ChrisCompleteCodeTrunk/M3Workflow/Libraries/Json90r1/Source/Doc/PreserveObjectReferences.aml

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000