4 <title>Serializing Dates in 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 Dates in JSON</span></div>
16 <span style="color: DarkGray"> </span>
17 <p>DateTimes in JSON are hard.</p>
19 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>
20 <p>The default format used by <a href="http://james.newtonking.com/projects/json-net.aspx" target="_blank">Json.NET</a> for dates is the same one used by Microsoft: "\/Date(1198908717056)\/". You can read more about it <a href="http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx" target="_blank">here</a>.</p>
22 <h3>DateTime JsonConverters</h3>
23 <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>
25 <div class="overflowpanel">
27 <div style="font-size: 10pt; color: black; font-family: courier new;">
28 <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">LogEntry</span></pre>
29 <pre style="margin: 0px;">{</pre>
30 <pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Details { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
32 <pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span> LogDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
33 <pre style="margin: 0px;">}</pre>
34 <pre style="margin: 0px;"> </pre>
35 <pre style="margin: 0px;">[<span style="color: rgb(43, 145, 175);">Test</span>]</pre>
37 <pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">void</span> WriteJsonDates()</pre>
38 <pre style="margin: 0px;">{</pre>
39 <pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">LogEntry</span> entry = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">LogEntry</span></pre>
40 <pre style="margin: 0px;"> {</pre>
42 <pre style="margin: 0px;"> LogDate = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DateTime</span>(2009, 2, 15, 0, 0, 0, <span style="color: rgb(43, 145, 175);">DateTimeKind</span>.Utc),</pre>
43 <pre style="margin: 0px;"> Details = <span style="color: rgb(163, 21, 21);">"Application started."</span></pre>
44 <pre style="margin: 0px;"> };</pre>
45 <pre style="margin: 0px;"> </pre>
47 <pre style="margin: 0px;"> <span style="color: blue;">string</span> defaultJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry);</pre>
48 <pre style="margin: 0px;"> <span style="color: green;">// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}</span></pre>
49 <pre style="margin: 0px;"></pre>
50 <pre style="margin: 0px;"> </pre>
51 <pre style="margin: 0px;"> <span style="color: blue;">string</span> javascriptJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JavaScriptDateTimeConverter</span>());</pre>
53 <pre style="margin: 0px;"> <span style="color: green;">// {"Details":"Application started.","LogDate":new Date(1234656000000)}</span></pre>
54 <pre style="margin: 0px;"> </pre>
55 <pre style="margin: 0px;"> <span style="color: blue;">string</span> isoJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">IsoDateTimeConverter</span>());</pre>
56 <pre style="margin: 0px;"> <span style="color: green;">// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}</span></pre>
58 <pre style="margin: 0px;">}</pre>
62 <p>Simply pass the JsonConverter you wish to use to the Json.NET serializer.</p>
63 <h4>JavaScriptDateTimeConverter</h4>
64 <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>
65 <div class="overflowpanel">
67 <div style="font-size: 10pt; color: black; font-family: courier new;">
68 <pre style="margin: 0px;">new Date(1234656000000)</pre>
73 <p>Technically this is invalid JSON according to the spec but all browsers, and some JSON frameworks including Json.NET, support it. </p>
74 <h4>IsoDateTimeConverter</h4>
75 <p>IsoDateTimeConverter seralizes a DateTime to an <a href="http://en.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a> formatted string.</p>
76 <div class="overflowpanel">
78 <div style="font-size: 10pt; color: black; font-family: courier new;">
79 <pre style="margin: 0px;"><span style="color: rgb(163, 21, 21);">"2009-02-15T00:00:00Z"</span></pre>
84 <p>The IsoDateTimeConverter class has a property, DateTimeFormat, to further customize the formatted string.</p>
86 <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>
89 <div id="footer"></div>