-Updated documentation

This commit is contained in:
JamesNK
2011-07-28 12:03:50 +00:00
parent 8f01fa44f5
commit d664eee995
4 changed files with 74 additions and 65 deletions
+50 -50
View File
@@ -1,7 +1,7 @@
<html>
<head>
<title>Customizing JSON serialization with attributes</title>
<title>Serialization Attributes</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<link href="custom.css" rel="stylesheet" type="text/css" />
</head>
@@ -10,7 +10,7 @@
<div id="control">
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
<span class="topicTitle">Customizing JSON serialization with attributes</span></div>
<span class="topicTitle">Serialization Attributes</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
@@ -28,28 +28,28 @@
<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;">&nbsp; <span style="color: green;">// "John Smith"</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<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>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// "2000-12-15T22:11:03"</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <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;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// new Date(976918263055)</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">JavaScriptDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <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;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// not serialized</span></pre>
<pre style="margin: 0px;">&nbsp; <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 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;">&nbsp; <span style="color: green;">// "John Smith"</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<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>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// "2000-12-15T22:11:03"</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <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;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// new Date(976918263055)</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">JavaScriptDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <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;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// not serialized</span></pre>
<pre style="margin: 0px;">&nbsp; <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>
@@ -77,38 +77,38 @@ or property then the specified JsonConverter will always be used to serialize th
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;">&nbsp; <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;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <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 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;">&nbsp; <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;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <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;">&nbsp;</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;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; DefaultConverter = date,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; MemberConverter = date</pre>
<pre style="margin: 0px;">&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</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;">&nbsp;</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;">//&nbsp; "DefaultConverter": "\/Date(0)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "MemberConverter": "1970-01-01T00:00:00Z"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
</div>
<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;">&nbsp;</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;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; DefaultConverter = date,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; MemberConverter = date</pre>
<pre style="margin: 0px;">&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</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;">&nbsp;</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;">//&nbsp; "DefaultConverter": "\/Date(0)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "MemberConverter": "1970-01-01T00:00:00Z"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
</div>
</div></div>
</div></div>
<div id="footer">
+16 -11
View File
@@ -18,6 +18,16 @@
<p>JsonSerializer has a number of properties on it to customize how it serializes
JSON. These can also be used with the methods on JsonConvert via the
JsonSerializerSettings overloads.</p>
<h3>MissingMemberHandling</h3>
<p>MissingMemberHandling controls how missing members, e.g. JSON contains a property
that isn&#39;t a member on the object, are handled during deserialization.</p>
<ul>
<li><strong>Ignore</strong> - By default Json.NET ignores JSON if there is no field
or property for its value to be set to during deserialization.</li>
<li><strong>Error</strong> - Json.NET errors when there is a missing member during
deserialization.</li>
</ul>
<h3>ReferenceLoopHandling</h3>
<p>ReferenceLoopHandling controls how circular referencing objects, e.g. a Person
object referencing itself via a Manager property, are serialized.</p>
@@ -32,16 +42,7 @@
reference loops. This is useful if objects are nested but not indefinitely.
</li>
</ul>
<h3>MissingMemberHandling</h3>
<p>MissingMemberHandling controls how missing members, e.g. JSON contains a property
that isn&#39;t a member on the object, are handled during deserialization.</p>
<ul>
<li><strong>Ignore</strong> - By default Json.NET ignores JSON if there is no field
or property for its value to be set to during deserialization.</li>
<li><strong>Error</strong> - Json.NET errors when there is a missing member during
deserialization </li>
</ul>
<p>ReferenceLoopHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
<h3>NullValueHandling</h3>
<p>NullValueHandling controls how null values on .NET objects are handled during
serialization and how null values in JSON are handled during deserialization.</p>
@@ -52,10 +53,11 @@
value is null when serializing and will skip setting fields/properties if the
JSON property is null when deserializing. </li>
</ul>
<p>NullValueHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
<h3>
DefaultValueHandling</h3>
<p>
&nbsp;DefaultValueHandling controls how Json.NET uses default values set using the .NET
DefaultValueHandling controls how Json.NET uses default values set using the .NET
DefaultValueAttribute when serializing and deserializing.</p>
<ul>
<li><strong>Include</strong> - By default Json.NET will write a field/property value
@@ -67,6 +69,7 @@
Json.NET deserializer will skip setting a .NET object’s field/property if the
JSON value is the same as the default value. </li>
</ul>
<p>DefaultValueHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
<h3>ObjectCreationHandling</h3>
<p>ObjectCreationHandling controls how objects are created and deserialized to
during deserialization.</p>
@@ -78,6 +81,7 @@
<li><strong>Replace</strong> - Json.NET will always recreate objects and collections
before setting values to them during deserialization. </li>
</ul>
<p>ObjectCreationHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
<h3>TypeNameHandling</h3>
<p>TypeNameHandling controls whether Json.NET includes .NET type names during
serialization with a $type property and reads .NET type names from that property
@@ -97,6 +101,7 @@
<li><strong>All</strong> - Json.NET will write and use type names for objects and
collections. </li>
</ul>
<p>TypeNameHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
<h3>TypeNameAssemblyFormat</h3>
<p>TypeNameAssemblyFormat controls how type names are written during serialization.</p>
<ul>
+2
View File
@@ -66,6 +66,8 @@
<Content Include="ContractResolver.html" />
<Content Include="custom.css" />
<Content Include="ConvertingJSONandXML.html" />
<Content Include="SerializationSettings.html" />
<Content Include="SerializationGuide.html" />
<Content Include="SerializingJSONFragments.html" />
<Content Include="SelectToken.html" />
<Content Include="ReducingSerializedJSONSize.html" />
+6 -4
View File
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode title="Introduction" url="Introduction.html" isDefault="true" />
<siteMapNode title="Serializing and deserializing JSON" url="SerializingJSON.html">
<siteMapNode title="Customizing JSON serialization with attributes" url="SerializationAttributes.html" />
<siteMapNode title="Serializing Dates in JSON" url="DatesInJSON.html" />
<siteMapNode title="Serializing Collections" url="SerializingCollections.html" />
<siteMapNode title="Serializing and Deserializing JSON" url="SerializingJSON.html">
<siteMapNode title="Serialization Settings" url="SerializationSettings.html" />
<siteMapNode title="Serialization Guide" url="SerializationGuide.html" />
<siteMapNode title="Serialization Attributes" url="SerializationAttributes.html" />
<siteMapNode title="Serialization Callbacks" url="SerializationCallbacks.html" />
<siteMapNode title="Serialization Error Handling" url="SerializationErrorHandling.html" />
<siteMapNode title="Serialization and Preserving Object References" url="PreserveObjectReferences.html" />
<siteMapNode title="CustomCreationConverter" url="CustomCreationConverter.html" />
<siteMapNode title="Contract Resolvers" url="ContractResolver.html" />
<siteMapNode title="Serializing Collections" url="SerializingCollections.html" />
<siteMapNode title="Serializing Dates in JSON" url="DatesInJSON.html" />
<siteMapNode title="Reducing Serialized JSON Size" url="ReducingSerializedJSONSize.html" />
<siteMapNode title="Serializing Partial JSON Fragments" url="SerializingJSONFragments.html" />
</siteMapNode>