All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Doc / DatesInJSON.html
1 <html>
2   
3   <head>
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" />
7   </head>
8   
9   <body>
10     
11     <div id="control">
12       <span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
13         <span class="topicTitle">Serializing Dates in JSON</span></div>
14
15     <div id="content">
16       <span style="color: DarkGray"> </span>
17         <p>DateTimes in JSON are hard.</p>
18         <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>
21
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>
24
25 <div class="overflowpanel">
26 <div class="code">
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;">&nbsp; <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>
31
32 <pre style="margin: 0px;">&nbsp; <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;">&nbsp;</pre>
35 <pre style="margin: 0px;">[<span style="color: rgb(43, 145, 175);">Test</span>]</pre>
36
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;">&nbsp; <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;">&nbsp; {</pre>
41
42 <pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; 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;">&nbsp;&nbsp;&nbsp; Details = <span style="color: rgb(163, 21, 21);">"Application started."</span></pre>
44 <pre style="margin: 0px;">&nbsp; };</pre>
45 <pre style="margin: 0px;">&nbsp;</pre>
46
47 <pre style="margin: 0px;">&nbsp; <span style="color: blue;">string</span> defaultJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry);</pre>
48 <pre style="margin: 0px;">&nbsp; <span style="color: green;">// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}</span></pre>
49 <pre style="margin: 0px;"></pre>
50 <pre style="margin: 0px;">&nbsp;</pre>
51 <pre style="margin: 0px;">&nbsp; <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>
52
53 <pre style="margin: 0px;">&nbsp; <span style="color: green;">// {"Details":"Application started.","LogDate":new Date(1234656000000)}</span></pre>
54 <pre style="margin: 0px;">&nbsp;</pre>
55 <pre style="margin: 0px;">&nbsp; <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;">&nbsp; <span style="color: green;">// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}</span></pre>
57
58 <pre style="margin: 0px;">}</pre>
59 </div>
60 </div>
61 </div>
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">
66 <div class="code">
67 <div style="font-size: 10pt; color: black; font-family: courier new;">
68 <pre style="margin: 0px;">new Date(1234656000000)</pre>
69
70 </div>
71 </div>
72 </div>
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">
77 <div class="code">
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>
80
81 </div>
82 </div>
83 </div>
84 <p>The IsoDateTimeConverter class has a property, DateTimeFormat, to further customize the formatted string.</p>
85 <p>&nbsp;</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>
87
88
89       <div id="footer"></div>      
90     </div>
91
92   </body>
93
94 </html>