-Documentation

-Added Path to JsonWriterException
This commit is contained in:
James Newton-King
2012-05-07 23:16:24 +12:00
parent a8f4be59ab
commit d815dc7749
22 changed files with 299 additions and 124 deletions
+10 -9
View File
@@ -16,13 +16,16 @@
<span style="color: DarkGray"> </span>
<p>DateTimes in JSON are hard.</p>
<p>
The problem comes from the <a href="http://www.ietf.org/rfc/rfc4627.txt" target="_blank">JSON spec</a> itself, there is no literal syntax for dates in JSON. The spec has objects, arrays, strings, integers and floats, but it defines no standard for what a date looks like.</p>
The problem comes from the <a href="http://www.ietf.org/rfc/rfc4627.txt" target="_blank">JSON spec</a> itself: there is no literal syntax for dates in JSON. The spec has objects, arrays, strings, integers and floats, but it defines no standard for what a date looks like.</p>
<p>The default format used by <a href="http://james.newtonking.com/projects/json-net.aspx" target="_blank">Json.NET</a>
is the ISO 8601 standard: 2012-03-19T07:22Z. You can read more about it <a href="http://en.wikipedia.org/wiki/ISO_8601" target="_blank">here</a>.</p>
<p>Prior to Json.NET 4.5 dates were written using the Microsoft format:
&quot;\/Date(1198908717056)\/&quot;. If you want to use this format, or you want to
maintain compatibility with Microsoft JSON serializers or older versions of
Json.NET then change the DateFormatHandling setting to MicrosoftDateFormat.</p>
Json.NET then change the <a href="html/T_Newtonsoft_Json_DateFormatHandling.htm">DateFormatHandling</a> setting to MicrosoftDateFormat.</p>
<p>Json.NET also has the <a href="html/T_Newtonsoft_Json_DateTimeZoneHandling.htm">DateTimeZoneHandling</a> setting. This can be used to convert
DateTime&#39;s kind when serializing, e.g. set DateTimeZoneHandling to Utc to
serialize all DateTimes as UTC dates.</p>
<h3>DateTime JsonConverters</h3>
<p>With no standard for dates in JSON, the number of possible different formats when interoping with other systems is endless. Fortunately Json.NET has a solution to deal with reading and writing custom dates: JsonConverters. A JsonConverter is used to override how a type is serialized.</p>
@@ -49,16 +52,16 @@
<pre style="margin: 0px;">&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// Default as of Json.NET 4.5</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// default as of Json.NET 4.5</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">string</span> isoJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry);</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> settingsWithMicrosoftDateFormat = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> microsoftDateFormatSettings = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span></pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; DateFormatHandling = <span style="color: rgb(43, 145, 175);">DateFormatHandling</span>.MicrosoftDateFormat</pre>
<pre style="margin: 0px;">&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">string</span> microsoftJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, settingsWithMicrosoftDateFormat);</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">string</span> microsoftJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, microsoftDateFormatSettings);</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
@@ -71,7 +74,7 @@
</div>
<p>Simply pass the JsonConverter you wish to use to the Json.NET serializer.</p>
<h4>JavaScriptDateTimeConverter</h4>
<p>The JavaScriptDateTimeConverter class is one of the two DateTime JsonConverters that come with Json.NET. This converter serializes a DateTime as a <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date" target="_blank">JavaScript Date object</a>.</p>
<p>The JavaScriptDateTimeConverter class is one of the two DateTime JsonConverters that come with Json.NET. This converter serializes a DateTime as a <a href="http://msdn.microsoft.com/en-us/library/cd9w2te4.aspx" target="_blank">JavaScript Date object</a>.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-size: 10pt; color: black; font-family: courier new;">
@@ -80,7 +83,7 @@
</div>
</div>
</div>
<p>Technically this is invalid JSON according to the spec but all browsers, and some JSON frameworks including Json.NET, support it. </p>
<p>Technically this is invalid JSON according to the spec but all browsers, and some JSON frameworks, including Json.NET, support it. </p>
<h4>IsoDateTimeConverter</h4>
<p>IsoDateTimeConverter seralizes a DateTime to an <a href="http://en.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a> formatted string.
Note that since Json.NET 4.5 dates are written using the ISO 8601 format by
@@ -94,8 +97,6 @@
</div>
</div>
<p>The IsoDateTimeConverter class has a property, DateTimeFormat, to further customize the formatted string.</p>
<p>&nbsp;</p>
<p>One final thing to note is all date values returned by Json.NET are in <a href="http://en.wikipedia.org/wiki/Utc" target="_blank">UTC time</a>.</p>
<div id="footer"></div>