blob: 35635cf155039f62dd58545778bfc3ff1ea69575 (
plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
<html>
<head>
<title>Serialization Callbacks</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 Callbacks</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>Json.NET supports serialization callback methods. A callback can be used to
manipulate an object before and after its serialization and deserialization by
the JsonSerializer.</p>
<ul>
<li><b>OnSerializing</b></li>
<li><b>OnSerialized</b></li>
<li><b>OnDeserializing</b></li>
<li><b>OnDeserialized</b></li>
</ul>
<p>To tell the serializer which methods should be called during the object's serialization
lifecycle, decorate a method with the appropraite attribute (<a
href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.onserializingattribute.aspx" target="_blank">OnSerializingAttribute</a>,
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.onserializedattribute.aspx" target="_blank">OnSerializedAttribute</a>,
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializingattribute.aspx" target="_blank">OnDeserializingAttribute</a>,
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx" target="_blank">OnDeserializedAttribute</a>).</p>
<p>Example object with serialization callback methods:</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;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SerializationEventTestObject</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;"> <span style="color: green;">// This member is serialized and deserialized with no change.</span></pre>
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">int</span> Member1 { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"> <span style="color: green;">// The value of this field is set and reset during and </span></pre>
<pre style="margin: 0px;"> <span style="color: green;">// after serialization.</span></pre>
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member2 { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"> <span style="color: green;">// This field is not serialized. The OnDeserializedAttribute </span></pre>
<pre style="margin: 0px;"> <span style="color: green;">// is used to set the member value after serialization.</span></pre>
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonIgnore</span>]</pre>
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member3 { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"> <span style="color: green;">// This field is set to null, but populated after deserialization.</span></pre>
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member4 { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"> <span style="color: blue;">public</span> SerializationEventTestObject()</pre>
<pre style="margin: 0px;"> {</pre>
<pre style="margin: 0px;"> Member1 = 11;</pre>
<pre style="margin: 0px;"> Member2 = <span style="color: #a31515;">"Hello World!"</span>;</pre>
<pre style="margin: 0px;"> Member3 = <span style="color: #a31515;">"This is a nonserialized value"</span>;</pre>
<pre style="margin: 0px;"> Member4 = <span style="color: blue;">null</span>;</pre>
<pre style="margin: 0px;"> }</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnSerializing</span>]</pre>
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnSerializingMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
<pre style="margin: 0px;"> {</pre>
<pre style="margin: 0px;"> Member2 = <span style="color: #a31515;">"This value went into the data file during serialization."</span>;</pre>
<pre style="margin: 0px;"> }</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnSerialized</span>]</pre>
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnSerializedMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
<pre style="margin: 0px;"> {</pre>
<pre style="margin: 0px;"> Member2 = <span style="color: #a31515;">"This value was reset after serialization."</span>;</pre>
<pre style="margin: 0px;"> }</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnDeserializing</span>]</pre>
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnDeserializingMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
<pre style="margin: 0px;"> {</pre>
<pre style="margin: 0px;"> Member3 = <span style="color: #a31515;">"This value was set during deserialization"</span>;</pre>
<pre style="margin: 0px;"> }</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnDeserialized</span>]</pre>
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnDeserializedMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
<pre style="margin: 0px;"> {</pre>
<pre style="margin: 0px;"> Member4 = <span style="color: #a31515;">"This value was set after deserialization."</span>;</pre>
<pre style="margin: 0px;"> }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div></div>
<p>The example object being serialized and deserialized by Json.NET:</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;">SerializationEventTestObject</span> obj = <span style="color: blue;">new</span> <span style="color: #2b91af;">SerializationEventTestObject</span>();</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member1);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Hello World!</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This is a nonserialized value</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
<pre style="margin: 0px;"><span style="color: green;">// null</span></pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(obj, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// "Member1": 11,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// "Member2": "This value went into the data file during serialization.",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// "Member4": null</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member1);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This value was reset after serialization.</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This is a nonserialized value</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
<pre style="margin: 0px;"><span style="color: green;">// null</span></pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;">obj = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">SerializationEventTestObject</span>>(json);</pre>
<pre style="margin: 0px;"> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member1);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This value went into the data file during serialization.</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This value was set during deserialization</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This value was set after deserialization.</span></pre>
</div>
</div></div>
<div id="footer">
</div>
</div>
</body>
</html>
|