-Added .NET 2.0 support

-Documentation
This commit is contained in:
JamesNK
2009-08-01 12:06:21 +00:00
parent 5611228772
commit ff5c48660d
30 changed files with 788 additions and 119 deletions
+12 -5
View File
@@ -17,13 +17,20 @@
<h3>
Features</h3>
<ul>
<li>LINQ to JSON</li>
<li>The JsonSerializer for quickly converting your .NET objects to JSON and back again</li>
<li>Json.NET can optionally produce well formatted, indented JSON for debugging or display</li>
<li>Attributes like JsonIgnore and JsonProperty can be added to a class to customize
how a class is serialized</li>
<li>Flexible JSON serializer to convert .NET objects to JSON and back again</li>
<li>LINQ to JSON for reading and writing JSON</li>
<li>Writes indented, easy to read JSON</li>
<li>Convert JSON to and from XML</li>
<li>Supports Silverlight and the Compact Framework</li>
</ul>
<p>The JSON serializer is a good choice when the JSON you are reading or writing maps closely
to a .NET class. The serializer automatically reads and writes JSON for the class.</p>
<p class="MsoNormal">
For situations where you are only interested in getting values from JSON, don&#39;t
have a class to serialize or deserialize to, or the JSON is radically different
from your class and you need to manually read and write from your objects then
LINQ to JSON is what you should use. LINQ to JSON allows you to easily read,
create and modify JSON in .NET.</p>
<h3>
History</h3>
<p>
+128
View File
@@ -0,0 +1,128 @@
<html>
<head>
<title>Serializing Collections</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 Collections</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>The <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a> has
great support for serializing and deserializing collections of objects.</p>
<h3>Serializing</h3>
<p>To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for.
Json.NET will serialize the collection and all of the values it contains.</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> p1 = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span></pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Name = <span style="color: #a31515;">"Product 1"</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Price = 99.95m,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; ExpiryDate = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2000, 12, 29, 0, 0, 0, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
<pre style="margin: 0px;">&nbsp; };</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> p2 = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; Name = <span style="color: #a31515;">"Product 2"</span>,</pre>
<pre style="margin: 0px;">&nbsp; Price = 12.50m,</pre>
<pre style="margin: 0px;">&nbsp; ExpiryDate = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2009, 7, 31, 0, 0, 0, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
<pre style="margin: 0px;">};</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Product</span>&gt; products = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Product</span>&gt;();</pre>
<pre style="margin: 0px;">products.Add(p1);</pre>
<pre style="margin: 0px;">products.Add(p2);</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(products, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
<pre style="margin: 0px;"><span style="color: green;">//[</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Name": "Product 1",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "ExpiryDate": "\/Date(978048000000)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Price": 99.95,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Sizes": null</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Name": "Product 2",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "ExpiryDate": "\/Date(1248998400000)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Price": 12.50,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Sizes": null</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>Deserializing</h3>
<p>To deserialize JSON into a .NET collection just specify the collection type you want to deserialize to. Json.NET supports a wide range of collection types.</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; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""Name"": ""Product 1"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""ExpiryDate"": ""\/Date(978048000000)\/"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""Price"": 99.95,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""Sizes"": null</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""Name"": ""Product 2"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""ExpiryDate"": ""\/Date(1248998400000)\/"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""Price"": 12.50,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""Sizes"": null</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;">List</span>&lt;<span style="color: #2b91af;">Product</span>&gt; products = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Product</span>&gt;&gt;(json);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(products.Count);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 2</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> p1 = products[0];</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p1.Name);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Product 1</span></pre>
</div>
</div></div>
<h3>Deserializing Dictionaries</h3>
<p>Using Json.NET you can also deserialize a JSON object into a .NET generic dictionary. The JSON object's property names and values will be added to the dictionary.</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;">@"{""key1"":""value1"",""key2"":""value2""}"</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Dictionary</span>&lt;<span style="color: blue;">string</span>, <span style="color: blue;">string</span>&gt; values = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">Dictionary</span>&lt;<span style="color: blue;">string</span>, <span style="color: blue;">string</span>&gt;&gt;(json);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(values.Count);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 2</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(values[<span style="color: #a31515;">"key1"</span>]);</pre>
<pre style="margin: 0px;"><span style="color: green;">// value1</span></pre>
</div>
</div></div>
<div id="footer">
</div>
</div>
</body>
</html>
+1
View File
@@ -24,6 +24,7 @@
<contentItem sourcePath=".\ContractResolver.html" destPath="" excludeItems="False" />
<contentItem sourcePath=".\SerializationCallbacks.html" destPath="" excludeItems="False" />
<contentItem sourcePath=".\SerializationAttributes.html" destPath="" excludeItems="False" />
<contentItem sourcePath=".\SerializingCollections.html" destPath="" excludeItems="False" />
</additionalContent>
<ProjectSummary />
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter</MissingTags>
+2 -1
View File
@@ -3,7 +3,8 @@
<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 Dates in JSON" url=".\DatesInJSON.html" />
<siteMapNode title="Serializing Collections" url=".\SerializingCollections.html" />
<siteMapNode title="Serialization Callbacks" url=".\SerializationCallbacks.html" />
<siteMapNode title="Serialization and Preserving Object References" url=".\PreserveObjectReferences.html" />
<siteMapNode title="CustomCreationConverter" url=".\CustomCreationConverter.html" />