-More documentation (I know!)
-Fixed decimals to always be written with a decimal place -Replaced IMappingResolver with IContractResolver -Renamed JsonMemberMapping to JsonProperty -Added JsonContract, JsonObjectContract, JsonArrayContract, JsonDictionaryContract -Added support for OnSerializing, OnSerialized, OnDeserializing, OnDeserialized callback methods -Major refactoring of serialization internals
This commit is contained in:
@@ -17,16 +17,135 @@
|
||||
|
||||
<p>The Json.NET serializer has a number of advanced options to alter how JSON is serialized and deserialized.</p>
|
||||
|
||||
<h3>IMappingResolver</h3>
|
||||
<h3>Serialization Callbacks</h3>
|
||||
|
||||
<p>The <a href="./html/T_Newtonsoft_Json_Serialization_IMappingResolver.htm">IMappingResolver</a> interface provides a way to globally customize how the JsonSerializer maps JSON properties to .NET object members.</p>
|
||||
<p>Implementing the IMappingResolver interface and then assigning an instance to a JsonSerializer lets you control what object members should be serialized, how they are serialized and what they are called.</p>
|
||||
<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>
|
||||
|
||||
|
||||
<h3>IContractResolver</h3>
|
||||
|
||||
<p>The <a href="./html/T_Newtonsoft_Json_Serialization_IContractResolver.htm">IContractResolver</a> interface provides a way to customize how the JsonSerializer serializes and deserializes .NET objects to JSON.</p>
|
||||
<p>Implementing the IContractResolver interface and then assigning an instance to a JsonSerializer lets you control
|
||||
whether the object is serialized as a JSON object or JSON array, what object members should be serialized, how they are serialized and what they are called.</p>
|
||||
|
||||
<h4>DefaultMappingResolver</h4>
|
||||
<p>The <a href="./html/T_Newtonsoft_Json_Serialization_DefaultMappingResolver.htm">DefaultMappingResolver</a> is the default resolver used by the serializer. It provides many avenues of extensibility in the form of virtual methods that can be overriden.</p>
|
||||
<h4>DefaultContractResolver</h4>
|
||||
<p>The <a href="./html/T_Newtonsoft_Json_Serialization_DefaultContractResolver.htm">DefaultContractResolver</a> is the default resolver used by the serializer. It provides many avenues of extensibility in the form of virtual methods that can be overriden.</p>
|
||||
|
||||
<h4>CamelCaseMappingResolver</h4>
|
||||
<p><a href="./html/T_Newtonsoft_Json_Serialization_CamelCaseMappingResolver.htm">CamelCaseMappingResolver</a> inherits from DefaultMappingResolver and simply overrides the JSON property name to be written in <a href="http://en.wikipedia.org/wiki/CamelCase" target="_blank">camelcase</a>.</p>
|
||||
<h4>CamelCasePropertyNamesContractResolver</h4>
|
||||
<p><a href="./html/T_Newtonsoft_Json_Serialization_CamelCasePropertyNamesContractResolver.htm">CamelCasePropertyNamesContractResolver</a> inherits from DefaultContractResolver and simply overrides the JSON property name to be written in <a href="http://en.wikipedia.org/wiki/CamelCase" target="_blank">camelcase</a>.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code"> <div style="font-family: courier new; color: black; font-size: 10pt;"> <pre style="margin: 0px;"><span style="color: rgb(43, 145, 175);">Product</span> product = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Product</span></pre>
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@
|
||||
<p>
|
||||
Json.NET is a free open source project that I have developed in my personal time.</p>
|
||||
<p>
|
||||
You can show your support for Json.NET by donating. I really appreciate your feedback and support.</p>
|
||||
I really appreciate your feedback and support for Json.NET and its future development.</p>
|
||||
<p><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=james%40newtonking%2ecom&item_name=Supporting%20Json%2eNET&no_shipping=0&no_note=1&tax=0¤cy_code=USD&lc=US&bn=PP%2dDonationsBF&charset=UTF%2d8" target="_blank">
|
||||
<img src="donate.gif" alt="Donate" style="border-width:0;" />
|
||||
</a></p>
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serializing and deserializing JSON</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 PreserveObjectRefences</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>The quickest method of converting between JSON text and a .NET object is using
|
||||
the <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a>. The JsonSerializer converts .NET objects into their JSON
|
||||
equivalent and back again.</p>
|
||||
|
||||
|
||||
<p>For simple scenarios 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
|
||||
JsonSerializer.</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;">Product</span> product = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span>();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;">product.Name = <span style="color: #a31515;">"Apple"</span>;</pre>
|
||||
<pre style="margin: 0px;">product.Expiry = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2008, 12, 28);</pre>
|
||||
<pre style="margin: 0px;">product.Price = 3.99M;</pre>
|
||||
<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>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> output = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(product);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Name": "Apple",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Expiry": "\/Date(1230375600000+1300)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Price": 3.99,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Sizes": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Small",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Medium",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Large"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</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;">Product</span> deserializedProduct = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">Product</span>>(output);</pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<h3>JsonSerializer</h3>
|
||||
|
||||
|
||||
<p>For more control over how an object is serialized the JsonSerializer can be used
|
||||
directly. The JsonSerializer has a number of properties to control its behaviour
|
||||
when serializing and deserializing.</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;">Product</span> product = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span>();</pre>
|
||||
<pre style="margin: 0px;">product.Expiry = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2008, 12, 28);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializer</span>();</pre>
|
||||
<pre style="margin: 0px;">serializer.Converters.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">JavaScriptDateTimeConverter</span>());</pre>
|
||||
<pre style="margin: 0px;">serializer.NullValueHandling = <span style="color: #2b91af;">NullValueHandling</span>.Ignore;</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<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>
|
||||
<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>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> serializer.Serialize(writer, product);</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// {"Expiry":new Date(1230375600000),"Price":0}</span></pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<h5>ReferenceLoopHandling</h5>
|
||||
<p>Controls how circular referencing objects are serialized. Error, ignore or
|
||||
serialize.</p>
|
||||
<h5>MissingMemberHandling</h5>
|
||||
<p>Controls how missing members (e.g. JSON contains a property that isn't a member
|
||||
on the object) are handled during deserialization. Ignore or error.</p>
|
||||
<h5>NullValueHandling</h5>
|
||||
<p>Controls how null values are handled during serialization and deserialization.
|
||||
Include or ignore.</p>
|
||||
<h5>DefaultValueHandling</h5>
|
||||
<p>Controls whether a value will be written to JSON or not if it matches the value specified in
|
||||
the member's DefaultValueAttribute. Include or ignore.</p>
|
||||
<h5>ObjectCreationHandling</h5>
|
||||
<p>Controls how objects are created during deserialization. Auto, reuse, replace.</p>
|
||||
<h5>Converters</h5>
|
||||
<p>A collection of JsonConverters that will be used during serialization and
|
||||
deserialization.</p>
|
||||
<h5>TypeNameHandling</h5>
|
||||
<p>Controls how .NET type names are used when serializing and deserializing JSON. If
|
||||
type names are SDFSDFSDF</p>
|
||||
<h3>Serialization Attributes</h3>
|
||||
<p>There are a number of attributes that can be placed on a class to control how it is serialized.</p>
|
||||
<p>Note that as well as using the built-in Json.NET attributes, Json.NET also looks for the
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx" target="_blank">DataContract</a> and
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx" target="_blank">DataMember</a> attributes when determining how JSON is to be serialized and deserialized. If both are present the Json.NET serialization attributes take precedence.</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>(<span style="color: #2b91af;">MemberSerialization</span>.OptIn)]</pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Person</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// "John Smith"</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// "2000-12-15T22:11:03"</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> BirthDate { <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;">// new Date(976918263055)</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">JavaScriptDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> LastModified { <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;">// not serialized</span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Department { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<h5>JsonObjectAttribute</h5>
|
||||
<p>The MemberSerialization flag on this attribute specifies whether member serialization is opt-in (a member must have the JsonProperty or DataMember attribute to be serialized) or opt-out (everything is serialized by default but can be ignored with the JsonIgnoreAttribute).</p>
|
||||
<p>.NET classes that implement IEnumerable are automatically serialized as an array, populated with the IEnumerable values. Placing the JsonPropertyAttribute will override this and force the serializer to serialize the class's fields and properties.</p>
|
||||
<h5>JsonPropertyAttribute</h5>
|
||||
<p>Allows the name of the serialized member to be customized. The attribute also indicates that a member should be serialized when member serialization is set to opt-in.</p>
|
||||
<h5>JsonIgnoreAttribute</h5>
|
||||
<p>Excludes a field or property from serialization.</p>
|
||||
<h5>JsonConverterAttribute</h5>
|
||||
<p>The JsonConverterAttribute specifies which JsonSerializer is used to convert an object.</p>
|
||||
<p>The attribute can be placed on a class or a member. When placed on a class the JsonConverter
|
||||
specified by the attribute will be the default way of serializing that class. When the attribute is on a field
|
||||
or property then the specified JsonConverter will always be used to serialize that value.</p>
|
||||
<p>The priority of which JsonConverter is used is member attribute then class attribute and
|
||||
finally any converters passed to the JsonSerializer.</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;">MemberConverterClass</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> DefaultConverter { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> MemberConverter { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
</div></div>
|
||||
<p>This example shows the JsonConverterAttribute being applied to a property.</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;">DateTime</span> date = <span style="color: #2b91af;">Convert</span>.ToDateTime(<span style="color: #a31515;">"1970-01-01T00:00:00Z"</span>).ToUniversalTime();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">MemberConverterClass</span> c = <span style="color: blue;">new</span> <span style="color: #2b91af;">MemberConverterClass</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> DefaultConverter = date,</pre>
|
||||
<pre style="margin: 0px;"> MemberConverter = date</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(c, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(json);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "DefaultConverter": "\/Date(0)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "MemberConverter": "1970-01-01T00:00:00Z"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<h3>JsonConverters</h3>
|
||||
<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>
|
||||
<p>To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:</p>
|
||||
|
||||
|
||||
|
||||
<h5>DateTime JSON Converters</h5>
|
||||
<p>Json.NET comes with a number of JsonConverters for serializing and deserializing DateTimes. Read
|
||||
more about dates and Json.NET <a href="DatesInJSON.html">here</a>.</p>
|
||||
|
||||
<h5>XmlNodeConverter</h5>
|
||||
<p>Converts an XmlNode to and from JSON. Note that to convert a JSON object it must have only a single property
|
||||
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
|
||||
well formed XML can only have one root node.</p>
|
||||
|
||||
|
||||
<h5>BinaryConverter</h5>
|
||||
<p>Converts binary data like a byte array or SqlBinary object to JSON. The binary
|
||||
data is written as a string in JSON and is encoded in Base64.</p>
|
||||
|
||||
<h5>CustomCreationConverter</h5>
|
||||
<p>An abstract JsonConverter for customizing how an object is create during deserialization.
|
||||
Inherit from this class and implement the Create method with your own code to create and return an object.
|
||||
The object will then be populated with JSON values by the serializer.</p>
|
||||
<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>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -20,7 +20,7 @@
|
||||
equivalent and back again.</p>
|
||||
|
||||
|
||||
<p>For simple scenarios the <a href="./html/Overload_Newtonsoft_Json_JsonConvert_SerializeObject.htm">SerializeObject</a> and <a href="./html/Overload_Newtonsoft_Json_JavaScriptConvert_DeserializeObject.htm">DeserializeObject</a> methods on <a href="./html/T_Newtonsoft_Json_JavaScriptConvert.htm">JavaScriptConvert</a> provide an easy to use wrapper over
|
||||
<p>For simple scenarios 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
|
||||
JsonSerializer.</p>
|
||||
|
||||
|
||||
@@ -132,7 +132,8 @@ the member's DefaultValueAttribute. Include or ignore.</p>
|
||||
</div></div>
|
||||
|
||||
<h5>JsonObjectAttribute</h5>
|
||||
<p>The MemberSerialization flag on this attribute specifies whether member serialization is opt-in (a member must have the JsonPropertyAttribute to be serialized) or opt-out (everything is serialized by default but can be ignored with the JsonIgnoreAttribute).</p>
|
||||
<p>The MemberSerialization flag on this attribute specifies whether member serialization is opt-in (a member must have the JsonProperty or DataMember attribute to be serialized) or opt-out (everything is serialized by default but can be ignored with the JsonIgnoreAttribute).</p>
|
||||
<p>.NET classes that implement IEnumerable are automatically serialized as an array, populated with the IEnumerable values. Placing the JsonPropertyAttribute will override this and force the serializer to serialize the class's fields and properties.</p>
|
||||
<h5>JsonPropertyAttribute</h5>
|
||||
<p>Allows the name of the serialized member to be customized. The attribute also indicates that a member should be serialized when member serialization is set to opt-in.</p>
|
||||
<h5>JsonIgnoreAttribute</h5>
|
||||
@@ -144,8 +145,43 @@ the member's DefaultValueAttribute. Include or ignore.</p>
|
||||
or property then the specified JsonConverter will always be used to serialize that value.</p>
|
||||
<p>The priority of which JsonConverter is used is member attribute then class attribute and
|
||||
finally any converters passed to the JsonSerializer.</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;">MemberConverterClass</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> DefaultConverter { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> MemberConverter { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
</div></div>
|
||||
<p>This example shows the JsonConverterAttribute being applied to a property.</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;">DateTime</span> date = <span style="color: #2b91af;">Convert</span>.ToDateTime(<span style="color: #a31515;">"1970-01-01T00:00:00Z"</span>).ToUniversalTime();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">MemberConverterClass</span> c = <span style="color: blue;">new</span> <span style="color: #2b91af;">MemberConverterClass</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> DefaultConverter = date,</pre>
|
||||
<pre style="margin: 0px;"> MemberConverter = date</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(c, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(json);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "DefaultConverter": "\/Date(0)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "MemberConverter": "1970-01-01T00:00:00Z"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<h3>JsonConverters</h3>
|
||||
<p>JsonConverters allows JSON to be manually be 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>
|
||||
<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>
|
||||
<p>To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:</p>
|
||||
|
||||
|
||||
@@ -162,13 +198,13 @@ finally any converters passed to the JsonSerializer.</p>
|
||||
|
||||
<h5>BinaryConverter</h5>
|
||||
<p>Converts binary data like a byte array or SqlBinary object to JSON. The binary
|
||||
data is encoded in Base64.</p>
|
||||
data is written as a string in JSON and is encoded in Base64.</p>
|
||||
|
||||
<h5>CustomCreationConverter</h5>
|
||||
<p>An abstract JsonConverter for customizing how an object is create during deserialization.
|
||||
Inherit from this class and implement the Create method with your own code to create and return an object.
|
||||
The object will then be populated with JSON values by the serializer.</p>
|
||||
<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>
|
||||
<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>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
+3
-1
@@ -7,7 +7,9 @@
|
||||
<namespaceSummaryItem name="Newtonsoft.Json" isDocumented="True">The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework.</namespaceSummaryItem>
|
||||
<namespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True">The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>.</namespaceSummaryItem>
|
||||
<namespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON.</namespaceSummaryItem>
|
||||
<namespaceSummaryItem name="Newtonsoft.Json.Utilities" isDocumented="True" />
|
||||
<namespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True">The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema.</namespaceSummaryItem>
|
||||
<namespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON.</namespaceSummaryItem>
|
||||
<namespaceSummaryItem name="Newtonsoft.Json.Utilities" isDocumented="False" />
|
||||
</namespaceSummaries>
|
||||
<additionalContent contentSiteMap=".\doc.sitemap" topicFileTransform="">
|
||||
<contentItem sourcePath=".\SerializingJSON.html" destPath="" excludeItems="False" />
|
||||
|
||||
Reference in New Issue
Block a user