4 <title>Serializing and deserializing JSON</title>
5 <link href="styles.css" rel="stylesheet" type="text/css" />
6 <link href="custom.css" rel="stylesheet" type="text/css" />
12 <span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
13 <span class="topicTitle">Serializing and deserializing JSON</span></div>
16 <span style="color: DarkGray"> </span>
18 <p>The quickest method of converting between JSON text and a .NET object is using
19 the <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a>. The JsonSerializer converts .NET objects into their JSON
20 equivalent and back again.</p>
23 <p>For simple scenarios where you want to convert to and from a JSON string the <a href="./html/Overload_Newtonsoft_Json_JsonConvert_SerializeObject.htm">SerializeObject</a> and <a href="./html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm">DeserializeObject</a> methods on <a href="./html/T_Newtonsoft_Json_JsonConvert.htm">JsonConvert</a> provide an easy to use wrapper over
27 <div class="overflowpanel"> <div class="code">
29 <div style="font-family: Courier New; font-size: 10pt; color: black;">
30 <pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> product = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span>();</pre>
31 <pre style="margin: 0px;"> </pre>
32 <pre style="margin: 0px;">product.Name = <span style="color: #a31515;">"Apple"</span>;</pre>
33 <pre style="margin: 0px;">product.Expiry = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2008, 12, 28);</pre>
34 <pre style="margin: 0px;">product.Price = 3.99M;</pre>
35 <pre style="margin: 0px;">product.Sizes = <span style="color: blue;">new</span> <span style="color: blue;">string</span>[] { <span style="color: #a31515;">"Small"</span>, <span style="color: #a31515;">"Medium"</span>, <span style="color: #a31515;">"Large"</span> };</pre>
36 <pre style="margin: 0px;"> </pre>
37 <pre style="margin: 0px;"><span style="color: blue;">string</span> output = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(product);</pre>
38 <pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
39 <pre style="margin: 0px;"><span style="color: green;">// "Name": "Apple",</span></pre>
40 <pre style="margin: 0px;"><span style="color: green;">// "Expiry": "\/Date(1230375600000+1300)\/",</span></pre>
41 <pre style="margin: 0px;"><span style="color: green;">// "Price": 3.99,</span></pre>
42 <pre style="margin: 0px;"><span style="color: green;">// "Sizes": [</span></pre>
43 <pre style="margin: 0px;"><span style="color: green;">// "Small",</span></pre>
44 <pre style="margin: 0px;"><span style="color: green;">// "Medium",</span></pre>
45 <pre style="margin: 0px;"><span style="color: green;">// "Large"</span></pre>
46 <pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
47 <pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
48 <pre style="margin: 0px;"> </pre>
49 <pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> deserializedProduct = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">Product</span>>(output);</pre>
54 <h3>JsonSerializer</h3>
57 <p>For more control over how an object is serialized the JsonSerializer can be used
58 directly. The JsonSerializer is able to read and write JSON text directly to
59 a stream via <a href="./html/T_Newtonsoft_Json_JsonTextReader.htm">JsonTextReader</a> and <a href="./html/T_Newtonsoft_Json_JsonTextWriter.htm">JsonTextWriter</a>. Other kinds of JsonWriters can
60 also be used such as <a href="./html/T_Newtonsoft_Json_Linq_JTokenReader.htm">JTokenReader</a>/<a href="./html/T_Newtonsoft_Json_Linq_JTokenWriter.htm">JTokenWriter</a> to
61 convert your object to and
62 from LINQ to JSON objects or <a href="./html/T_Newtonsoft_Json_Bson_BsonReader.htm">BsonReader</a>/<a href="./html/T_Newtonsoft_Json_Bson_BsonWriter.htm">BsonWriter</a> to convert to and from BSON.</p>
64 <div class="overflowpanel"> <div class="code">
66 <div style="font-family: Courier New; font-size: 10pt; color: black;">
67 <pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> product = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span>();</pre>
68 <pre style="margin: 0px;">product.Expiry = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2008, 12, 28);</pre>
69 <pre style="margin: 0px;"> </pre>
70 <pre style="margin: 0px;"><span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializer</span>();</pre>
71 <pre style="margin: 0px;">serializer.Converters.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">JavaScriptDateTimeConverter</span>());</pre>
72 <pre style="margin: 0px;">serializer.NullValueHandling = <span style="color: #2b91af;">NullValueHandling</span>.Ignore;</pre>
73 <pre style="margin: 0px;"> </pre>
74 <pre style="margin: 0px;"><span style="color: blue;">using</span> (<span style="color: #2b91af;">StreamWriter</span> sw = <span style="color: blue;">new</span> <span style="color: #2b91af;">StreamWriter</span>(<span style="color: #a31515;">@"c:\json.txt"</span>))</pre>
75 <pre style="margin: 0px;"><span style="color: blue;">using</span> (<span style="color: #2b91af;">JsonWriter</span> writer = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonTextWriter</span>(sw))</pre>
76 <pre style="margin: 0px;">{</pre>
77 <pre style="margin: 0px;"> serializer.Serialize(writer, product);</pre>
78 <pre style="margin: 0px;"> <span style="color: green;">// {"Expiry":new Date(1230375600000),"Price":0}</span></pre>
79 <pre style="margin: 0px;">}</pre>
83 <p>JsonSerializer has a number of properties on it to customize how it serializes JSON.
84 These can also be used with the methods on JsonConvert via the
85 JsonSerializerSettings overloads.</p>
87 <h5>ReferenceLoopHandling</h5>
88 <p>Controls how circular referencing objects are serialized. Error, ignore or
90 <h5>MissingMemberHandling</h5>
91 <p>Controls how missing members (e.g. JSON contains a property that isn't a member
92 on the object) are handled during deserialization. Ignore or error.</p>
93 <h5>NullValueHandling</h5>
94 <p>Controls how null values are handled during serialization and deserialization.
95 Include or ignore.</p>
96 <h5>DefaultValueHandling</h5>
97 <p>Controls whether a value will be written to JSON or not if it matches the value specified in
98 the member's DefaultValueAttribute. Include or ignore.</p>
99 <h5>ObjectCreationHandling</h5>
100 <p>Controls how objects are created during deserialization. Auto, reuse, replace.</p>
101 <h5>TypeNameHandling</h5>
102 <p>Controls whether .NET type names are included in serialized JSON and read during deserialization when creating objects. None, Objects, Arrays or All.</p>
103 <h5>ConstructorHandling</h5>
104 <p>Controls how constructors are used when initializing objects during deserialization. Default or AllowNonPublicDefaultConstructor.</p>
106 <p>A collection of JsonConverters that will be used during serialization and
110 <h3>JsonConverters</h3>
111 <p>JsonConverters allows JSON to be manually written during serialization and read during deserialization. This is useful for particularly complex JSON structures or for when you want to change how a type is serialized.</p>
112 <p>To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:</p>
116 <h5>DateTime JSON Converters</h5>
117 <p>Json.NET comes with a number of JsonConverters for serializing and deserializing DateTimes. Read
118 more about dates and Json.NET <a href="DatesInJSON.html">here</a>.</p>
120 <h5>XmlNodeConverter</h5>
121 <p>Converts an XmlNode to and from JSON. Note that to convert a JSON object it must have only a single property
122 or you must define a root node name to be inserted when using this converter. This is required because properties are converted into nodes and
123 well formed XML can only have one root node. XmlNodeConverter has an option to
124 insert a root node for you.</p>
127 <h5>BinaryConverter</h5>
128 <p>Converts binary data like the SqlBinary object to JSON. The binary
129 data is written as a string in JSON and is encoded in Base64.</p>
131 <h5>CustomCreationConverter</h5>
132 <p>An abstract JsonConverter for customizing how an object is create during deserialization.
133 Inherit from this class and implement the Create method with your own code to create and return an object.
134 The object will then be populated with JSON values by the serializer.</p>
135 <p>A possible example of using this converter would be to call out to a dependency injection framework to resolve what object should be created.</p>