-Fixes to XML documentation

-Sandcastle Help File Builder project added
-Quickstart guides added
This commit is contained in:
JamesNK
2008-05-04 01:26:44 +00:00
parent 3209f09f0f
commit d88e2cf2ca
22 changed files with 1045 additions and 25 deletions
+193
View File
@@ -0,0 +1,193 @@
<html>
<head>
<title>Reading and Writing 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">Reading and Writing JSON</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>LINQ to JSON is a programming API for working with JSON objects. The API has been designed with LINQ in mind to enable to quick querying and creation of JSON objects. LINQ to JSON sits under the <a href="./html/N_Newtonsoft_Json_Linq.htm">Newtonsoft.Json.Linq</a> namespace.</p>
<h3>Creating JSON</h3>
<p>There are a number of different options when it comes to creating JSON using LINQ to JSON. The first to create objects imperatively. You have total control but it is more verbose than other options.</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;">JArray</span> array = <span style="color: blue;">new</span> <span style="color: #2b91af;">JArray</span>();</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">JValue</span> text = <span style="color: blue;">new</span> <span style="color: #2b91af;">JValue</span>(<span style="color: #a31515;">"Manual text"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">JValue</span> date = <span style="color: blue;">new</span> <span style="color: #2b91af;">JValue</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2000, 5, 23));</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">array.Add(text);</pre>
<pre style="margin: 0px;">array.Add(date);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = array.ToString();</pre>
<pre style="margin: 0px;"><span style="color: green;">// [</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Manual text",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "\/Date(958996800000+1200)\/"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
</div>
</div></div>
<p>Another option is to create JSON objects declaratively.</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;">List</span>&lt;<span style="color: #2b91af;">Post</span>&gt; posts = GetPosts();</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> rss =</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"channel"</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"title"</span>, <span style="color: #a31515;">"James Newton-King"</span>),</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"link"</span>, <span style="color: #a31515;">"http://james.newtonking.com"</span>),</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"description"</span>, <span style="color: #a31515;">"James Newton-King's blog."</span>),</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"item"</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JArray</span>(</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">from</span> p <span style="color: blue;">in</span> posts</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">orderby</span> p.Title</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">select</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"title"</span>, p.Title),</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"description"</span>, p.Description),</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"link"</span>, p.Link),</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"category"</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JArray</span>(</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">from</span> c <span style="color: blue;">in</span> p.Categories</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">select</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">JValue</span>(c)))))))));</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(rss.ToString());</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "channel": {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "title": "James Newton-King",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "link": "http://james.newtonking.com",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "description": "James Newton-King's blog.",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "item": [</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; "title": "Json.NET 1.3 + New license + Now on CodePlex",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; "description": "Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; "link": "http://james.newtonking.com/projects/json-net.aspx",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; "category": [</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Json.NET",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "CodePlex"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; "title": "LINQ to JSON beta",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; "description": "Annoucing LINQ to JSON",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; "link": "http://james.newtonking.com/projects/json-net.aspx",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; "category": [</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Json.NET",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "LINQ"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
</div>
</div></div>
<p>You can create a JSON object from a non-JSON type using the <a href="./html/M_Newtonsoft_Json_Linq_JObject_FromObject.htm">FromObject</a> method.</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;">JObject</span> o = <span style="color: #2b91af;">JObject</span>.FromObject(<span style="color: blue;">new</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; channel = <span style="color: blue;">new</span></pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; title = <span style="color: #a31515;">"James Newton-King"</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; link = <span style="color: #a31515;">"http://james.newtonking.com"</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; description = <span style="color: #a31515;">"James Newton-King's blog."</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; item =</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">from</span> p <span style="color: blue;">in</span> posts</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">orderby</span> p.Title</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">select</span> <span style="color: blue;">new</span></pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title = p.Title,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description = p.Description,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link = p.Link,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; category = p.Categories</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">});</pre>
</div>
</div></div>
<p>Finally JSON objects can be created from a string use the <a href="./html/M_Newtonsoft_Json_Linq_JObject_Parse.htm">Parse</a> method.</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;">string</span> json = <span style="color: #a31515;">@"{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; CPU: 'Intel',</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; Drives: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; 'DVD read/writer',</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""500 gigabyte hard drive""</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> o = <span style="color: #2b91af;">JObject</span>.Parse(json);</pre>
</div>
</div></div>
<h3>Querying JSON</h3>
<p>The properties methods that are the most useful when querying JSON objects are <a href="./html/Overload_Newtonsoft_Json_Linq_JToken_Children.htm">Children</a>, <a href="./html/M_Newtonsoft_Json_Linq_JToken_Values__1.htm">Values</a> and the <a href="./html/P_Newtonsoft_Json_Linq_JToken_Item.htm">property index</a>.</p>
<p>Children returns all the children of that object. Values is similar to Children but it allows you to specify a type you would like the values to be converted into. Finally the property index is used to get a specific child, either by index position for JSON arrays or property name for JSON objects.</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;">var</span> postTitles =</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">from</span> p <span style="color: blue;">in</span> rss[<span style="color: #a31515;">"channel"</span>][<span style="color: #a31515;">"item"</span>].Children()</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">select</span> p.Value&lt;<span style="color: blue;">string</span>&gt;(<span style="color: #a31515;">"title"</span>);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> item <span style="color: blue;">in</span> postTitles)</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(item);</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">//LINQ to JSON beta</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//Json.NET 1.3 + New license + Now on CodePlex</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">var</span> categories =</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">from</span> c <span style="color: blue;">in</span> rss[<span style="color: #a31515;">"channel"</span>][<span style="color: #a31515;">"item"</span>].Children()[<span style="color: #a31515;">"category"</span>].Values&lt;<span style="color: blue;">string</span>&gt;()</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">group</span> c <span style="color: blue;">by</span> c <span style="color: blue;">into</span> g</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">orderby</span> g.Count() <span style="color: blue;">descending</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">select</span> <span style="color: blue;">new</span> { Category = g.Key, Count = g.Count() };</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> c <span style="color: blue;">in</span> categories)</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(c.Category + <span style="color: #a31515;">" - Count: "</span> + c.Count);</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">//Json.NET - Count: 2</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//LINQ - Count: 1</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//CodePlex - Count: 1</span></pre>
</div>
</div></div>
<div id="footer"></div>
</div>
</body>
</html>
+87
View File
@@ -0,0 +1,87 @@
<html>
<head>
<title>LINQ to 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">LINQ to JSON</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>To manually read and write JSON Json.NET provides the <a href="./html/T_Newtonsoft_Json_JsonReader.htm>JsonReader</a> and <a href="./html/T_Newtonsoft_Json_JsonWriter.htm">JsonWriter</a> classes.</p>
<h3>JsonTextReader and JsonTextWriter</h3>
<p><a href="./html/T_Newtonsoft_Json_JsonTextReader.htm">JsonTextReader</a> and <a href="./html/T_Newtonsoft_Json_JsonTextWriter.htm">JsonTextWriter</a> are used to read and write JSON text. The JsonTextWriter has a number of settings on it to control how JSON is formatted when it is written. These options include formatting, indention character, indent count and quote character.</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;">StringBuilder</span> sb = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringBuilder</span>();</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">StringWriter</span> sw = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringWriter</span>(sb);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> (<span style="color: #2b91af;">JsonWriter</span> jsonWriter = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonTextWriter</span>(sw))</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.Formatting = <span style="color: #2b91af;">Formatting</span>.Indented;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteStartObject();</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WritePropertyName(<span style="color: #a31515;">"CPU"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"Intel"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WritePropertyName(<span style="color: #a31515;">"PSU"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"500W"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WritePropertyName(<span style="color: #a31515;">"Drives"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteStartArray();</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"DVD read/writer"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteComment(<span style="color: #a31515;">"(broken)"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"500 gigabyte hard drive"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"200 gigabype hard drive"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteEnd();</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteEndObject();</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "CPU": "Intel",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "PSU": "500W",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Drives": [</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "DVD read/writer"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; /*(broken)*/,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "500 gigabyte hard drive",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "200 gigabype hard drive"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
</div>
</div></div>
<h3>JsonTokenReader and JsonTokenWriter</h3>
<p><a href="./html/T_Newtonsoft_Json_JsonTokenReader.htm">JsonTokenReader</a> and <a href="./html/T_Newtonsoft_Json_JsonTokenWriter.htm">JsonTokenWriter</a> read and write LINQ to JSON objects. They are located in the <a href="./html/N_Newtonsoft_Json_Linq.htm">Newtonsoft.Json.Linq</a> namespace. These objects allow you to use LINQ to JSON objects with objects that read and write JSON such as the JsonSerializer. For example you can deserialize from a LINQ to JSON object into a regular .NET object and vice versa.</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;">JObject</span> o = <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"Name"</span>, <span style="color: #a31515;">"John Smith"</span>),</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"BirthDate"</span>, <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(1983, 3, 20))</pre>
<pre style="margin: 0px;">&nbsp; );</pre>
<pre style="margin: 0px;">&nbsp;</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;"><span style="color: #2b91af;">Person</span> p = (<span style="color: #2b91af;">Person</span>)serializer.Deserialize(<span style="color: blue;">new</span> <span style="color: #2b91af;">JsonTokenReader</span>(o), <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Person</span>));</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// John Smith</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p.Name);</pre>
</div>
</div></div>
<div id="footer"></div>
</div>
</body>
</html>
+144
View File
@@ -0,0 +1,144 @@
<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">Serializing and deserializing JSON</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_JavaScriptConvert_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
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;">&nbsp;</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;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> output = <span style="color: #2b91af;">JavaScriptConvert</span>.SerializeObject(product);</pre>
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Name": "Apple",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Expiry": &quot;\/Date(1230375600000+1300)\/&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Price": 3.99,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Sizes": [</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Small",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Medium",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Large"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> deserializedProduct = (<span style="color: #2b91af;">Product</span>)<span style="color: #2b91af;">JavaScriptConvert</span>.DeserializeObject(output, <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Product</span>));</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;">&nbsp;</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;">&nbsp;</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;">&nbsp; serializer.Serialize(writer, product);</pre>
<pre style="margin: 0px;">&nbsp; <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&#39;t a member
on the object) are handled during deserialization. Error or ignore.</p>
<h5>NullValueHandling</h5>
<p>Controls how null values are handled during serialization and deserialization.
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>
<h3>Serialization Attributes</h3>
<p>There are a number of optional attributes that can be placed on a class to control how it is serialized.</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;">&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: #2b91af;">JsonProperty</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;">// 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>
<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>
<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>
<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>To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:</p>
<h5>IsoDateTimeConverter</h5>
<p>Converts a DateTime to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z).</p>
<h5>JavaScriptDateTimeConverter</h5>
<p>Converts a DateTime to and from a JavaScript date constructor (e.g. new Date(52231943)).</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. This is required because properties are converted into nodes and XML can only have one root node.</p>
<div id="footer"></div>
</div>
</body>
</html>
+19
View File
@@ -0,0 +1,19 @@
div#content {
padding: 0 10px 10px 10px;
}
.overflowpanel
{
width: 98%;
border: solid 1px #ccc;
overflow: auto;
overflow-y: hidden;
background-color: #fcfcfc;
margin-bottom: 1em;
}
.overflowpanel .code
{
padding: 10px 4px 20px 10px;
display: block;
}
+56
View File
@@ -0,0 +1,56 @@
<project schemaVersion="1.6.0.2">
<assemblies>
<assembly assemblyPath="D:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\bin\Release\Newtonsoft.Json.dll" xmlCommentsPath="D:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\bin\Release\Newtonsoft.Json.xml" commentsOnly="False" />
</assemblies>
<namespaceSummaries>
<namespaceSummaryItem name="" isDocumented="False" />
<namespaceSummaryItem name="Newtonsoft.Json" isDocumented="True">The &lt;b&gt;Newtonsoft.Json&lt;/b&gt; namespace provides classes that are used to implement the core services of the framework.</namespaceSummaryItem>
<namespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Converters&lt;/b&gt; namespace provides classes that inherit from &lt;a href="T_Newtonsoft_Json_JsonConverter.htm"&gt;JsonConverter&lt;/a&gt;.</namespaceSummaryItem>
<namespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Linq&lt;/b&gt; namespace provides classes that are used to implement LINQ to JSON.</namespaceSummaryItem>
<namespaceSummaryItem name="Newtonsoft.Json.Utilities" isDocumented="True" />
</namespaceSummaries>
<additionalContent>
<contentItem sourcePath=".\SerializingJSON.html" destPath="" excludeItems="False" />
<contentItem sourcePath=".\styles.css" destPath="" excludeItems="False" />
<contentItem sourcePath=".\custom.css" destPath="" excludeItems="False" />
<contentItem sourcePath=".\LINQtoJSON.html" destPath="" excludeItems="False" />
<contentItem sourcePath=".\ReadingWritingJSON.html" destPath="" excludeItems="False" />
</additionalContent>
<ProjectSummary />
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, Namespace</MissingTags>
<VisibleItems>InheritedMembers, InheritedFrameworkMembers, Protected, SealedProtected</VisibleItems>
<HtmlHelp1xCompilerPath path="" />
<HtmlHelp2xCompilerPath path="" />
<OutputPath>.\Output\</OutputPath>
<SandcastlePath path="" />
<WorkingPath path="" />
<CleanIntermediates>True</CleanIntermediates>
<KeepLogFile>True</KeepLogFile>
<HelpFileFormat>Help1xAndWebsite</HelpFileFormat>
<PurgeDuplicateTopics>True</PurgeDuplicateTopics>
<CppCommentsFixup>False</CppCommentsFixup>
<FrameworkVersion>3.5</FrameworkVersion>
<BinaryTOC>True</BinaryTOC>
<IncludeFavorites>False</IncludeFavorites>
<Preliminary>False</Preliminary>
<RootNamespaceContainer>False</RootNamespaceContainer>
<RootNamespaceTitle />
<HelpTitle>Json.NET - Quick Starts &amp; API Documentation</HelpTitle>
<HtmlHelpName>Documentation</HtmlHelpName>
<Language>en-US</Language>
<CopyrightHref />
<CopyrightText />
<FeedbackEMailAddress />
<HeaderText />
<FooterText />
<ProjectLinkType>Local</ProjectLinkType>
<SdkLinkType>Msdn</SdkLinkType>
<SdkLinkTarget>Blank</SdkLinkTarget>
<PresentationStyle>Prototype</PresentationStyle>
<NamingMethod>MemberName</NamingMethod>
<SyntaxFilters>Standard</SyntaxFilters>
<ShowFeedbackControl>False</ShowFeedbackControl>
<ContentPlacement>AboveNamespaces</ContentPlacement>
<ContentSiteMap path=".\doc.sitemap" />
<TopicFileTransform path="" />
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode title="Serializing and deserializing JSON" url=".\SerializingJSON.html" isDefault="true" />
<siteMapNode title="LINQ to JSON" url=".\LINQtoJSON.html" />
<siteMapNode title="Reading and Writing JSON" url=".\ReadingWritingJSON.html" />
</siteMap>
+439
View File
@@ -0,0 +1,439 @@
/* page style */
body {
margin: 0;
background-color: #FFFFFF;
padding: 0;
font-size: 8.5pt;
font-family: verdana, arial, sans-serif;
color: #000000;
}
html>body {
margin: 0;
background-color: #FFFFFF;
padding: 0;
font-size: 8.5pt;
font-family: verdana, arial, sans-serif;
color: #000000;
overflow: auto;
}
table {
/* this is a trick to force tables to inherit the body font size */
font-size: 100%;
}
/* non-scrolling (control) region style */
div#control {
margin: 0;
background-color: #D4DFFF;
padding: 4px;
width: 100%;
border-bottom-color: #C8CDDE;
border-bottom-style: solid;
border-bottom-width: 1px;
z-index: 2;
}
span.productTitle {
font-size: 80%;
}
span.topicTitle {
font-size: 140%;
font-weight: bold;
color: #003399;
}
span#chickenFeet {
float: left;
}
span#languageFilter {
float: right;
height: 1px;
max-height: 1px;
vertical-align: bottom;
overflow: visible;
}
/* scrolling (content) region style */
div#main
{
clear: both;
margin: 0;
padding: 1em;
width: 100%;
z-index: 1;
overflow: auto;
}
/* sections */
div#header {
font-size: 80%;
color: #666666;
margin-bottom: 0.5em;
}
div.summary {
margin-top: 0em;
margin-bottom: 1em;
}
div.section {
margin-bottom: 1em;
}
div.sectionTitle {
display: inline;
font-size: 120%;
font-weight: bold;
color: #003399;
}
div.sectionContent {
margin-top: 0.2em;
}
span.subsectionTitle {
font-weight: bold;
}
div#footer {
margin-top: 1em;
border-top: thin solid #003399;
padding-top: 0.5em;
}
div#footer p {
margin-top: 0.2em;
margin-bottom: 0.2em;
}
/* authored content (block) */
p {
margin-top: 1em;
margin-bottom: 1em;
}
dl, ul, ol {
margin-top: 0.5em;
margin-bottom: 0.5em;
}
pre {
margin: 0;
padding: 0;
font-family: "Andale Mono", "Courier New", Courier, monospace;
}
table.authoredTable {
table-layout: fixed;
width: 100%;
margin-bottom: 1em;
}
table.authoredTable th {
border-bottom-color: #C8CDDE;
border-bottom-style: solid;
border-bottom-width: 1px;
background: #EFEFF7;
padding: 0.2em;
text-align: left;
color: #000066;
font-weight: bold;
}
table.authoredTable td {
border-bottom-style: solid;
border-bottom-color: #C8CDDE;
border-bottom-width: 1px;
background: #F7F7FF;
padding: 0.2em;
vertical-align: top;
}
div.alert {
border: 1px solid #C8CDDE;
background: #F7F7FF;
}
div.media {
text-align: center;
margin-bottom: 1em;
}
/* authored content (inline) */
span.keyword {
font-weight: bold;
}
span.code {
font-family: "Andale Mono", "Courier New", Courier, monospace;
font-size: 110%;
color: #000066;
}
/* auto-generated controls */
div.langTabs {
/*width: 100%;*/
}
div.langTab {
float: left;
width: 15%;
border-top: 1px solid #C8CDDE;
border-left: 1px solid #C8CDDE;
border-right: 1px solid #C8CDDE;
background: #F7F7FF;
padding: 0.2em;
text-align: left;
color: #000066;
font-weight: normal;
}
div.activeLangTab {
float: left;
width: 15%;
border-top: 1px solid #C8CDDE;
border-left: 1px solid #C8CDDE;
border-right: 1px solid #C8CDDE;
background: #EFEFF7;
padding: 0.2em;
text-align: left;
color: #000066;
font-weight: bold;
}
table.members {
/* table-layout: fixed; */
width: 100%;
}
table.members th.iconColumn {
width: 60px;
}
table.members th.nameColumn {
width: 40%;
}
table.members th.descriptionColumn {
width: 60%;
}
table.members th {
border-bottom-color: #C8CDDE;
border-bottom-style: solid;
border-bottom-width: 1px;
background: #EFEFF7;
padding: 0.2em;
text-align: left;
color: #000066;
font-weight: bold;
}
table.members td {
border-bottom-style: solid;
border-bottom-color: #C8CDDE;
border-bottom-width: 1px;
background: #F7F7FF;
padding: 0.2em;
vertical-align: top;
overflow: hidden;
}
table.exceptions {
table-layout: fixed;
width: 100%;
}
table.exceptions th.exceptionNameColumn {
width: 40%;
}
table.exceptions th.exceptionConditionColumn {
width: 60%;
}
table.exceptions th {
border-bottom-color: #C8CDDE;
border-bottom-style: solid;
border-bottom-width: 1px;
background: #EFEFF7;
padding: 0.2em;
text-align: left;
color: #000066;
font-weight: bold;
}
table.exceptions td {
border-bottom-style: solid;
border-bottom-color: #C8CDDE;
border-bottom-width: 1px;
background: #F7F7FF;
padding: 0.2em;
vertical-align: top;
}
table.permissions {
table-layout: fixed;
width: 100%;
}
table.permissions th.permissionNameColumn {
width: 40%;
}
table.permissions th.permissionConditionColumn {
width: 60%;
}
table.permissions th {
border-bottom-color: #C8CDDE;
border-bottom-style: solid;
border-bottom-width: 1px;
background: #EFEFF7;
padding: 0.2em;
text-align: left;
color: #000066;
font-weight: bold;
}
table.permissions td {
border-bottom-style: solid;
border-bottom-color: #C8CDDE;
border-bottom-width: 1px;
background: #F7F7FF;
padding: 0.2em;
vertical-align: top;
}
span.obsolete {
color: red;
}
span.cs {
display: inline;
}
span.vb {
display: none;
}
span.cpp {
display: none;
}
span.nu
{
display: none;
}
/* syntax styling */
div.code span.identifier {
font-weight: bold;
}
div.code span.keyword {
color: green;
}
div.code span.parameter {
font-style: italic;
color: purple;
}
div.code span.literal {
color: purple;
}
div.code span.comment {
color: red;
}
span.foreignPhrase {
font-style: italic;
}
span.placeholder {
font-style: italic;
}
span.parameter
{
font-style: italic;
}
span.typeparameter
{
font-style: italic;
}
a {
color: blue;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
MSHelp\:link {
color: blue;
hoverColor: #3366ff;
}
span.nolink {
font-weight: bold;
}
span.selflink {
font-weight: bold;
}
table.filter {
table-layout: fixed;
}
tr.tabs td.tab {
width: 10em;
background: #F7F7FF;
padding: 0.2em;
text-align: left;
color: #000066;
font-weight: normal;
overflow: hidden;
cursor: pointer;
}
tr.tabs td.activeTab {
width: 10em;
background: #EFEFF7;
padding: 0.2em;
text-align: left;
color: #000066;
font-weight: bold;
overflow: hidden;
}
td.line {
background: #EFEFF7;
}
dt {
font-weight: bold;
margin-top: 10px;
margin-left: 10px;
}