-Add missing xml documentation

-Add build tools
-Add build scripts
-Move doc directory
This commit is contained in:
JamesNK
2010-02-06 12:17:21 +00:00
parent bb8e2217cd
commit 992985bd7b
62 changed files with 12480 additions and 25 deletions
-92
View File
@@ -1,92 +0,0 @@
<html>
<head>
<title>Contract Resolver</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">Contract Resolvers</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>The <a href="./html/T_Newtonsoft_Json_Serialization_IContractResolver.htm">IContractResolver</a> interface provides a way to customize how the JsonSerializer serializes and deserializes .NET objects to JSON.</p>
<p>Implementing the IContractResolver interface and then assigning an instance to a JsonSerializer lets you control
whether the object is serialized as a JSON object or JSON array, what object members should be serialized, how they are serialized and what they are called.</p>
<h4>DefaultContractResolver</h4>
<p>The <a href="./html/T_Newtonsoft_Json_Serialization_DefaultContractResolver.htm">DefaultContractResolver</a> is the default resolver used by the serializer. It provides many avenues of extensibility in the form of virtual methods that can be overriden.</p>
<h4>CamelCasePropertyNamesContractResolver</h4>
<p><a href="./html/T_Newtonsoft_Json_Serialization_CamelCasePropertyNamesContractResolver.htm">CamelCasePropertyNamesContractResolver</a> inherits from DefaultContractResolver and simply overrides the JSON property name to be written in <a href="http://en.wikipedia.org/wiki/CamelCase" target="_blank">camelcase</a>.</p>
<div class="overflowpanel"> <div class="code"> <div style="font-family: courier new; color: black; font-size: 10pt;"> <pre style="margin: 0px;"><span style="color: rgb(43, 145, 175);">Product</span> product = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Product</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ExpiryDate = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DateTime</span>(2010, 12, 20, 18, 1, 0, <span style="color: rgb(43, 145, 175);">DateTimeKind</span>.Utc),</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name = <span style="color: rgb(163, 21, 21);">"Widget"</span>,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Price = 9.99m,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sizes = <span style="color: blue;">new</span>[] {<span style="color: rgb(163, 21, 21);">"Small"</span>, <span style="color: rgb(163, 21, 21);">"Medium"</span>, <span style="color: rgb(163, 21, 21);">"Large"</span>}</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = </pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; product,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { ContractResolver = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">CamelCasePropertyNamesContractResolver</span>() }</pre>
<pre style="margin: 0px;">&nbsp; );</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; "name": "Widget",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "expiryDate": "\/Date(1292868060000)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "price": 9.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;&nbsp; "Small",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; "Medium",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&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>
</div>
</div>
</div>
<div id="footer">
</div>
</div>
</body>
</html>
-146
View File
@@ -1,146 +0,0 @@
<html>
<head>
<title>Converting between JSON and XML</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">Converting between JSON and XML</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>Json.NET supports converting JSON to XML and vice versa using the
<a href="./html/T_Newtonsoft_Json_Converters_XmlNodeConverter.htm">XmlNodeConverter</a>.</p>
<p>
Elements, attributes, text, comments, character data, processing instructions,
namespaces and the XML declaration are all preserved when converting between the
two. The only caveat is that it is possible to lose the order of differently
named nodes at the same level when they are grouped together into an array.</p>
<h3>Conversion Rules</h3>
<ul>
<li>Elements remain unchanged. </li>
<li>Attributes are prefixed with an @. </li>
<li>Single child text nodes are a value directly against an element, otherwise they
are accessed via #text. </li>
<li>The XML declaration and processing instructions are prefixed with ?. </li>
<li>Charater data, comments, whitespace and significate whitespace nodes are
accessed via #cdata-section,&nbsp;#comment, #whitespace and #significate-whitespace
respectively. </li>
<li>Multiple nodes with the same name at the same level are grouped together into an
array. </li>
<li>Empty elements are null.</li>
</ul>
<h3>SerializeXmlNode</h3>
<p>The JsonConvert has two helper methods for converting between JSON and XML. The first is <a href="./html/M_Newtonsoft_Json_JsonConvert_SerializeXmlNode.htm">SerializeXmlNode</a>. This method takes an
XmlNode and serializes it to JSON text.</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> xml = <span style="color: #a31515;">@&quot;&lt;?xml version=&quot;&quot;1.0&quot;&quot; standalone=&quot;&quot;no&quot;&quot;?&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&lt;root&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;person id=&quot;&quot;1&quot;&quot;&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;name&gt;Alan&lt;/name&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;url&gt;http://www.google.com&lt;/url&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;/person&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;person id=&quot;&quot;2&quot;&quot;&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;name&gt;Louis&lt;/name&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;url&gt;http://www.yahoo.com&lt;/url&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;/person&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&lt;/root&gt;&quot;</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">XmlDocument</span> doc = <span style="color: blue;">new</span> <span style="color: #2b91af;">XmlDocument</span>();</pre>
<pre style="margin: 0px;">doc.LoadXml(xml);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> jsonText = <span style="color: #2b91af;">JsonConvert</span>.SerializeXmlNode(doc);</pre>
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &quot;?xml&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &quot;@version&quot;: &quot;1.0&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &quot;@standalone&quot;: &quot;no&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &quot;root&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &quot;person&quot;: [</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; &quot;@id&quot;: &quot;1&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot;: &quot;Alan&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;url&quot;: &quot;http://www.google.com&quot;</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; &quot;@id&quot;: &quot;2&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot;: &quot;Louis&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;url&quot;: &quot;http://www.yahoo.com&quot;</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>
<h3>DeserializeXmlNode</h3>
<p>The second helper method on JsonConvert is <a href="./html/M_Newtonsoft_Json_JsonConvert_DeserializeXmlNode.htm">DeserializeXmlNode</a>. This method takes
JSON text and deserializes it into a XmlNode.</p>
<p>Because valid XML must have one root element the JSON passed to
DeserializeXmlNode should have one property in the root JSON object. If the root
JSON object has multiple properties then the overload that also takes an element
name should be used. A root element with that name will be inserted into the
deserialized XmlNode.</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;">@&quot;{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;?xml&quot;&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;@version&quot;&quot;: &quot;&quot;1.0&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;@standalone&quot;&quot;: &quot;&quot;no&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;root&quot;&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;person&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;@id&quot;&quot;: &quot;&quot;1&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;name&quot;&quot;: &quot;&quot;Alan&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;url&quot;&quot;: &quot;&quot;http://www.google.com&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;@id&quot;&quot;: &quot;&quot;2&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;name&quot;&quot;: &quot;&quot;Louis&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;url&quot;&quot;: &quot;&quot;http://www.yahoo.com&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">}&quot;</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">XmlDocument</span> doc = (<span style="color: #2b91af;">XmlDocument</span>)<span style="color: #2b91af;">JsonConvert</span>.DeserializeXmlNode(json);</pre>
<pre style="margin: 0px;"><span style="color: green;">// &lt;?xml version=&quot;1.0&quot; standalone=&quot;no&quot;?&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// &lt;root&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;person id=&quot;1&quot;&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;name&gt;Alan&lt;/name&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;url&gt;http://www.google.com&lt;/url&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;/person&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;person id=&quot;2&quot;&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;name&gt;Louis&lt;/name&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;url&gt;http://www.yahoo.com&lt;/url&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;/person&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// &lt;/root&gt;</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
-99
View File
@@ -1,99 +0,0 @@
<html>
<head>
<title>CustomCreationConverter</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">CustomCreationConverter</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>
The CustomCreationConverter is a JsonConverter that provides a way to customize how an object is created
during JSON deserialization.
Once the object has been created it will then have values populated onto it by the serializer.
</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;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IPerson</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">string</span> FirstName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">string</span> LastName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: #2b91af;">DateTime</span> BirthDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Employee</span> : <span style="color: #2b91af;">IPerson</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> FirstName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> LastName { <span style="color: blue;">get</span>; <span style="color: blue;">set</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: 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;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> JobTitle { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PersonConverter</span> : <span style="color: #2b91af;">CustomCreationConverter</span>&lt;<span style="color: #2b91af;">IPerson</span>&gt;</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: #2b91af;">IPerson</span> Create(<span style="color: #2b91af;">Type</span> objectType)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>();</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div></div>
<p>
This is an extremely simple example. A more complicated scenario could involve an object factory or service locator
which resolves the object at runtime.
</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: green;">//[</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "FirstName": "Maurice",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "LastName": "Moss",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "BirthDate": "\/Date(252291661000)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Department": "IT",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "JobTitle": "Support"</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; "FirstName": "Jen",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "LastName": "Barber",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "BirthDate": "\/Date(258771661000)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Department": "IT",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "JobTitle": "Manager"</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;">List</span>&lt;<span style="color: #2b91af;">IPerson</span>&gt; people = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">IPerson</span>&gt;&gt;(json, <span style="color: blue;">new</span> <span style="color: #2b91af;">PersonConverter</span>());</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">IPerson</span> person = people[0];</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(person.GetType());</pre>
<pre style="margin: 0px;"><span style="color: green;">// Newtonsoft.Json.Tests.Employee</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(person.FirstName);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Maurice</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Employee</span> employee = (<span style="color: #2b91af;">Employee</span>)person;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(employee.JobTitle);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Support</span></pre>
</div>
</div></div>
<div id="footer"></div>
</div>
</body>
</html>
-94
View File
@@ -1,94 +0,0 @@
<html>
<head>
<title>Serializing Dates in 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 Dates in JSON</span></div>
<div id="content">
<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>
<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>
<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>
<div class="overflowpanel">
<div class="code">
<div style="font-size: 10pt; color: black; font-family: courier new;">
<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>
<pre style="margin: 0px;">{</pre>
<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>
<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>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">[<span style="color: rgb(43, 145, 175);">Test</span>]</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">void</span> WriteJsonDates()</pre>
<pre style="margin: 0px;">{</pre>
<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>
<pre style="margin: 0px;">&nbsp; {</pre>
<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>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; Details = <span style="color: rgb(163, 21, 21);">"Application started."</span></pre>
<pre style="margin: 0px;">&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">string</span> defaultJson = <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":"\/Date(1234656000000)\/"}</span></pre>
<pre style="margin: 0px;"></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<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>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// {"Details":"Application started.","LogDate":new Date(1234656000000)}</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<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>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}</span></pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</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>
<div class="overflowpanel">
<div class="code">
<div style="font-size: 10pt; color: black; font-family: courier new;">
<pre style="margin: 0px;">new Date(1234656000000)</pre>
</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>
<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.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-size: 10pt; color: black; font-family: courier new;">
<pre style="margin: 0px;"><span style="color: rgb(163, 21, 21);">"2009-02-15T00:00:00Z"</span></pre>
</div>
</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>
</div>
</body>
</html>
-68
View File
@@ -1,68 +0,0 @@
<html>
<head>
<title>Introduction</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">Introduction</span></div>
<div id="content">
<span style="color: DarkGray"></span>
<p>
Json.NET makes working with JSON formatted data in .NET
simple. Quickly read and write JSON using LINQ to JSON or serialize
your .NET objects with a single method call using the JsonSerializer.</p>
<h3>
Features</h3>
<ul>
<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>
Json.NET grew out of projects I was working on in late 2005 involving JavaScript,
AJAX and .NET. At the time there were no libraries for working with JavaScript in
.NET so I began to grow my own.</p>
<p>
Starting out as a couple of static methods for escaping JavaScript strings, Json.NET
evolved as features were added. To add support for reading JSON a major refactor
was required and Json.NET will split into the three major classes it still uses
today, JsonReader, JsonWriter and JsonSerializer.
</p>
<p>
Json.NET was first released in June 2006. Since then Json.NET has been downloaded
thousands of times by developers and is used in a number of major projects open
source projects including <a href="http://www.castleproject.org/monorail/index.html" target="_blank">MonoRail</a>,
Castle Project's MVC web framework, and <a href="http://www.mono-project.com/" target="_blank">Mono</a>,
an open source implementation
of the .NET framework.</p>
<p>
<i>~ James Newton-King</i></p>
<h3>
Donate</h3>
<p>
Json.NET is a free open source project that I have developed in my personal time.</p>
<p>
I really appreciate your feedback and support for Json.NET and its future development.</p>
<p><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=james%40newtonking%2ecom&item_name=Supporting%20Json%2eNET&no_shipping=0&no_note=1&tax=0&currency_code=USD&lc=US&bn=PP%2dDonationsBF&charset=UTF%2d8" target="_blank">
<img src="donate.gif" alt="Donate" style="border-width:0;" />
</a></p>
<div id="footer">
</div>
</div>
</body>
</html>
-254
View File
@@ -1,254 +0,0 @@
<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>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
the <a href="./html/Overload_Newtonsoft_Json_Linq_JToken_Children.htm">Children</a>
method 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. If it is a JObject it will
return a collection of properties to work with and if it is a JArray you will
get a collection of the array&#39;s values.</p>
<p>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> (<span style="color: blue;">string</span>)p[<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>
<p>LINQ to JSON can also be used to manually convert from JSON to a .NET object.</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;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Shortie</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Original { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Shortened { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Short { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ShortieException</span> Error { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShortieException</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Code { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> ErrorMessage { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div></div>
<p>Manually serializing and deserializing between .NET objects is most useful when working with JSON that doesn't closely match your .NET 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;">string</span> jsonText = <span style="color: #a31515;">@"{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ""short"":{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""original"":""http://www.foo.com/"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""short"":""krehqk"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ""error"":{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ""code"":0,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ""msg"":""No action taken""}</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> json = <span style="color: #2b91af;">JObject</span>.Parse(jsonText);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Shortie</span> shortie = <span style="color: blue;">new</span> <span style="color: #2b91af;">Shortie</span></pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Original = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"original"</span>],</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Short = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"short"</span>],</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Error = <span style="color: blue;">new</span> <span style="color: #2b91af;">ShortieException</span></pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Code = (<span style="color: blue;">int</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"error"</span>][<span style="color: #a31515;">"code"</span>],</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ErrorMessage = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"error"</span>][<span style="color: #a31515;">"msg"</span>]</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(shortie.Original);</pre>
<pre style="margin: 0px;"><span style="color: green;">// http://www.foo.com/</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(shortie.Error.ErrorMessage);</pre>
<pre style="margin: 0px;"><span style="color: green;">// No action taken</span></pre>
</div>
</div></div>
<div id="footer"></div>
</div>
</body>
</html>
-138
View File
@@ -1,138 +0,0 @@
<html>
<head>
<title>Serialization and Preserving Object References</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">Serialization and Preserving Object References</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>By default Json.NET will serialize all objects it encounters by value. If a list
contains two Person references, and both references point to the same object
then the JsonSerializer will write out all the names and values for each
reference.</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;">Person</span> p = <span style="color: blue;">new</span> <span style="color: #2b91af;">Person</span></pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; BirthDate = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(1980, 12, 23, 0, 0, 0, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; LastModified = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2009, 2, 20, 12, 59, 21, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Name = <span style="color: #a31515;">"James"</span></pre>
<pre style="margin: 0px;">&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Person</span>&gt; people = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Person</span>&gt;();</pre>
<pre style="margin: 0px;">people.Add(p);</pre>
<pre style="margin: 0px;">people.Add(p);</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(people, <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": "James",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "BirthDate": "\/Date(346377600000)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "LastModified": "\/Date(1235134761000)\/"</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": "James",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "BirthDate": "\/Date(346377600000)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "LastModified": "\/Date(1235134761000)\/"</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>In most cases this is the desired result but in certain scenarios writing the second item in the list as a reference to the first is a better solution.
If the above JSON was deserialized now then the returned list would contain two
completely separate Person objects with the same values. Writing references by value will also cause problems on objects where a
circular reference occurs.</p>
<h3>PreserveReferencesHandling</h3>
<p>Settings PreserveReferencesHandling will track object references when serializing
and deserializing JSON.</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: #2b91af;">JsonConvert</span>.SerializeObject(people, <span style="color: #2b91af;">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializerSettings</span> { PreserveReferencesHandling = <span style="color: #2b91af;">PreserveReferencesHandling</span>.Objects });</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; "$id": "1",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "Name": "James",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "BirthDate": "\/Date(346377600000)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; "LastModified": "\/Date(1235134761000)\/"</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; "$ref": "1"</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;">List</span>&lt;<span style="color: #2b91af;">Person</span>&gt; deserializedPeople = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Person</span>&gt;&gt;(json,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializerSettings</span> { PreserveReferencesHandling = <span style="color: #2b91af;">PreserveReferencesHandling</span>.Objects });</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(deserializedPeople.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;">Person</span> p1 = deserializedPeople[0];</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p2 = deserializedPeople[1];</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;">// James</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p2.Name);</pre>
<pre style="margin: 0px;"><span style="color: green;">// James</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">bool</span> equal = <span style="color: #2b91af;">Object</span>.ReferenceEquals(p1, p2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// true</span></pre>
</div>
</div></div>
<p>The first Person in the list is serizlied with the addition of an object Id. The
second Person in JSON is now only a reference to the first.</p>
<p>With PreserveReferencesHandling on now only one Person object is created on
deserialization and the list contains two references to it, mirroring what we
started with.</p>
<h3>IsReference on JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute</h3>
<p>The PreserveReferencesHandling setting on the JsonSerializer will change
how all objects are serialized and deserialized. For fine grain control over which
objects and members should be serialized as a reference there is the IsReference property on
the JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute.</p>
<p>Setting IsReference on JsonObjectAttribute or JsonArrayAttribute to true will
mean the JsonSerializer will always serialize the type the attribute is against
as a reference. Setting IsReference on the JsonPropertyAttribute to true will
serialize only that property as a reference.</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>(IsReference = <span style="color: blue;">true</span>)]</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">EmployeeReference</span></pre>
<pre style="margin: 0px;">{</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; <span style="color: blue;">public</span> <span style="color: #2b91af;">EmployeeReference</span> Manager { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div></div>
<h3>IReferenceResolver</h3>
<p>To customize how references are generated and resolved the <a href="./html/T_Newtonsoft_Json_Serialization_IReferenceResolver.htm">IReferenceResolver</a> interface is available to inherit from and use with the JsonSerializer.</p>
<div id="footer">
</div>
</div>
</body>
</html>
-87
View File
@@ -1,87 +0,0 @@
<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>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>JTokenReader and JTokenWriter</h3>
<p><a href="./html/T_Newtonsoft_Json_Linq_JTokenReader.htm">JTokenReader</a> and <a href="./html/T_Newtonsoft_Json_Linq_JTokenWriter.htm">JTokenWriter</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;">JTokenReader</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: #2b91af;">Console</span>.WriteLine(p.Name);</pre>
<pre style="margin: 0px;"><span style="color: green;">// John Smith</span></pre>
</div>
</div></div>
<div id="footer"></div>
</div>
</body>
</html>
-280
View File
@@ -1,280 +0,0 @@
<html>
<head>
<title>Reducing Serialized JSON Size</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">Reducing Serialized JSON Size</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>One of the common problems encountered when serializing .NET objects to JSON is that the JSON ends up containing a lot of unwanted properties and values. This can be especially important when returning JSON to the client. More JSON means more bandwidth and a slower website.</p>
<p>To solve the issue of unwanted JSON Json.NET has a range of built in options to fine tune what gets written from a serialized object.</p>
<h3>JsonIgnoreAttribute and DataMemberAttribute </h3>
<p>By default Json.NET will include all of a classes public properties and fields in
the JSON it creates. Adding the <a href="./html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm">JsonIgnoreAttribute</a> to a property tells the serializer
to always skip writing it to the JSON result. </p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: courier new; color: black; font-size: 10pt;">
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Car</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// included in JSON</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Model { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span> Year { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">string</span>&gt; Features { <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;">// ignored</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">JsonIgnore</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span> LastModified { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<p>If a class has many properties and you only want to serialize a small subset of
them then adding JsonIgnore to all the others will be tedious and error prone.
The way to tackle this scenario is to add the
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx"
target="_blank">DataContractAttribute</a> to the class and
DataMemberAttributes to the properties to serialize. This is opt-in
serialization, only the properties you mark up with be serialized, compared to
opt-out serialization using JsonIgnoreAttribute.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: courier new; color: black; font-size: 10pt;">
<pre style="margin: 0px;">[<span style="color: rgb(43, 145, 175);">DataContract</span>]</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Computer</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// included in JSON</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">DataMember</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; [<span style="color: rgb(43, 145, 175);">DataMember</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> SalePrice { <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;">// ignored</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Manufacture { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> StockCount { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> WholeSalePrice { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span> NextShipmentDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<h3>Formatting </h3>
<p>JSON written by the serializer with an option of <a href="./html/T_Newtonsoft_Json_Formatting.htm">Formatting.Indented</a> produces nicely formatted, easy to read JSON – great when you are developing. <a href="./html/T_Newtonsoft_Json_Formatting.htm">Formatting.None</a> on the other hand keeps the JSON result small, skipping all unnecessary spaces and line breaks to produce the most compact and efficient JSON possible. </p>
<p><strong>NullValueHandling </strong></p>
<p><a href="./html/T_Newtonsoft_Json_NullValueHandling.htm">NullValueHandling</a> is an option on the JsonSerializer and controls how the serializer handles properties with a null value. By setting a value of NullValueHandling.Ignore the JsonSerializer skips writing any properties that have a value of null.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: courier new; color: black; font-size: 10pt;">
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Movie</span></pre>
<pre style="margin: 0px;">{</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; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Description { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Classification { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Studio { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span>? ReleaseDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">string</span>&gt; ReleaseCountries { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<div class="overflowpanel">
<div class="code">
<div style="font-family: courier new; color: black; font-size: 10pt;">
<pre style="margin: 0px;"><span style="color: rgb(43, 145, 175);">Movie</span> movie = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Movie</span>();</pre>
<pre style="margin: 0px;">movie.Name = <span style="color: rgb(163, 21, 21);">"Bad Boys III"</span>;</pre>
<pre style="margin: 0px;">movie.Description = <span style="color: rgb(163, 21, 21);">"It's no Bad Boys"</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> included = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(movie,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <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;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Name": "Bad Boys III",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Description": "It's no Bad Boys",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Classification": null,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Studio": null,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "ReleaseDate": null,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "ReleaseCountries": null</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: blue;">string</span> ignored = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(movie,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { NullValueHandling = <span style="color: rgb(43, 145, 175);">NullValueHandling</span>.Ignore });</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;&nbsp; "Name": "Bad Boys III",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Description": "It's no Bad Boys"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
</div>
</div>
</div>
<p>NullValueHandling can also be customized on individual properties using the a <a href="./html/T_Newtonsoft_Json_JsonPropertyAttribute.htm">JsonPropertyAttribute</a>. The JsonPropertyAttribute value of NullValueHandling will override the setting on the JsonSerializer for that property. </p>
<h3>DefaultValueHandling </h3>
<p><a href="./html/T_Newtonsoft_Json_NullValueHandling.htm">DefaultValueHandling</a> is an option on the JsonSerializer and controls how the serializer handles properties with a default value. Setting a value of DefaultValueHandling.Ignore will make the JsonSerializer skip writing any properties that have a default value to the JSON result. For object references this will be null. For value types like int and DateTime the serializer will skip the default unitialized value for that value type. </p>
<p>Json.NET also allows you to customize what the default value of an individual property is using the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx" target="_blank">DefaultValueAttribute</a>. For example if a string property called Department always returns an empty string in its default state and you didn't want that empty string in your JSON then placing the DefaultValueAttribute on Department with that value will mean Department is no longer written to JSON unless it has a value. </p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: courier new; color: black; font-size: 10pt;">
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Invoice</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Company { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> Amount { <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;">// false is default value of bool</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> Paid { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// null is default value of nullable</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span>? PaidDate { <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;">// customize default values</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">DefaultValue</span>(30)]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> FollowUpDays { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: rgb(43, 145, 175);">DefaultValue</span>(<span style="color: rgb(163, 21, 21);">""</span>)]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> FollowUpEmailAddress { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<div class="overflowpanel">
<div class="code">
<div style="font-family: courier new; color: black; font-size: 10pt;">
<pre style="margin: 0px;"><span style="color: rgb(43, 145, 175);">Invoice</span> invoice = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Invoice</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; Company = <span style="color: rgb(163, 21, 21);">"Acme Ltd."</span>,</pre>
<pre style="margin: 0px;">&nbsp; Amount = 50.0m,</pre>
<pre style="margin: 0px;">&nbsp; Paid = <span style="color: blue;">false</span>,</pre>
<pre style="margin: 0px;">&nbsp; FollowUpDays = 30,</pre>
<pre style="margin: 0px;">&nbsp; FollowUpEmailAddress = <span style="color: blue;">string</span>.Empty,</pre>
<pre style="margin: 0px;">&nbsp; PaidDate = <span style="color: blue;">null</span></pre>
<pre style="margin: 0px;">};</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> included = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(invoice,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <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;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Company": "Acme Ltd.",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Amount": 50.0,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Paid": false,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "PaidDate": null,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "FollowUpDays": 30,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "FollowUpEmailAddress": ""</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: blue;">string</span> ignored = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(invoice,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { DefaultValueHandling = <span style="color: rgb(43, 145, 175);">DefaultValueHandling</span>.Ignore });</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;&nbsp; "Company": "Acme Ltd.",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "Amount": 50.0</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
</div>
</div>
</div>
<p>DefaultValueHandling can also be customized on individual properties using the a <a href="./html/T_Newtonsoft_Json_JsonPropertyAttribute.htm">JsonPropertyAttribute</a>. The JsonPropertyAttribute value of DefaultValueHandling will override the setting on the JsonSerializer for that property.</p>
<h3>IContractResolver</h3>
<p>For more flexibility the <a href="./html/T_Newtonsoft_Json_Serialization_IContractResolver.htm">IContractResolver</a> provides an interface to customize almost every aspect of how a .NET object gets serialized to JSON, including changing serialization behavior at runtime.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: courier new; color: black; font-size: 10pt;">
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">DynamicContractResolver</span> : <span style="color: rgb(43, 145, 175);">DefaultContractResolver</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: blue;">char</span> _startingWithChar;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> DynamicContractResolver(<span style="color: blue;">char</span> startingWithChar)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; _startingWithChar = startingWithChar;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: rgb(43, 145, 175);">IList</span>&lt;<span style="color: rgb(43, 145, 175);">JsonProperty</span>&gt; CreateProperties(<span style="color: rgb(43, 145, 175);">JsonObjectContract</span> contract)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">IList</span>&lt;<span style="color: rgb(43, 145, 175);">JsonProperty</span>&gt; properties = <span style="color: blue;">base</span>.CreateProperties(contract);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">// only serializer properties that start with the specified character</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; properties = </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; properties.Where(p =&gt; p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> properties;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Book</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> BookName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">decimal</span> BookPrice { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> AuthorName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> AuthorAge { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> AuthorCountry { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<div class="overflowpanel">
<div class="code">
<div style="font-family: courier new; color: black; font-size: 10pt;">
<pre style="margin: 0px;"><span style="color: rgb(43, 145, 175);">Book</span> book = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Book</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BookName = <span style="color: rgb(163, 21, 21);">"The Gathering Storm"</span>,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BookPrice = 16.19m,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthorName = <span style="color: rgb(163, 21, 21);">"Brandon Sanderson"</span>,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthorAge = 34,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthorCountry = <span style="color: rgb(163, 21, 21);">"United States of America"</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> startingWithA = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(book, <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { ContractResolver = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DynamicContractResolver</span>(<span style="color: rgb(163, 21, 21);">'A'</span>) });</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;&nbsp; "AuthorName": "Brandon Sanderson",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "AuthorAge": 34,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "AuthorCountry": "United States of America"</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: blue;">string</span> startingWithB = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(book, <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { ContractResolver = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DynamicContractResolver</span>(<span style="color: rgb(163, 21, 21);">'B'</span>) });</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;&nbsp; "BookName": "The Gathering Storm",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; "BookPrice": 16.19</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
-116
View File
@@ -1,116 +0,0 @@
<html>
<head>
<title>Querying LINQ to JSON with SelectToken</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">Querying LINQ to JSON with SelectToken</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p><a href="./html/M_Newtonsoft_Json_Linq_JToken_SelectToken.htm">SelectToken</a> provides a method to query LINQ to JSON using a single string path to a
desired <a href="./html/T_Newtonsoft_Json_Linq_JToken.htm">JToken</a>. SelectToken makes dynamic queries
easy because the entire
query is defined in a string.</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> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[0].Name&quot;</span>);</pre>
</div>
</div>
</div>
<h3>SelectToken</h3>
<p>SelectToken is a method on JToken and takes a string path to a child token.
SelectToken returns the child token or a null reference if a token couldn&#39;t be
found at the path&#39;s location.</p>
<p>The
path is made up of property names and array indexes separated by periods. Array indexes
can use either square or round brackets. Both
of the following are valid paths and are equivalent to each other: <code>Manufacturers[0].Name</code>
and <code>Manufacturers(0).Name</code>.</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>.Parse(<span style="color: #a31515;">@&quot;{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;Stores&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;Lambton Quay&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;Willis Street&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ],</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;Manufacturers&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Acme Co&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Products&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Anvil&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 50</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Contoso&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Products&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Elbow Grease&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 99.95</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Headlight Fluid&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 4</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">}&quot;</span>);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[0].Name&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Acme Co</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> productPrice = (<span style="color: blue;">decimal</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[0].Products[0].Price&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 50</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> productName = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[1].Products[0].Name&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Elbow Grease</span></pre>
</div>
</div>
</div>
<h3>SelectToken with LINQ</h3>
<p>SelectToken can be used in combination with standard LINQ methods.</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;">IList</span>&lt;<span style="color: blue;">string</span>&gt; storeNames = o.SelectToken(<span style="color: #a31515;">&quot;Stores&quot;</span>).Select(s =&gt; (<span style="color: blue;">string</span>)s).ToList();</pre>
<pre style="margin: 0px;"><span style="color: green;">// Lambton Quay</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Willis Street</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span>&lt;<span style="color: blue;">string</span>&gt; firstProductNames = o[<span style="color: #a31515;">&quot;Manufacturers&quot;</span>].Select(m =&gt; (<span style="color: blue;">string</span>)m.SelectToken(<span style="color: #a31515;">&quot;Products[1].Name&quot;</span>)).ToList();</pre>
<pre style="margin: 0px;"><span style="color: green;">// null</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Headlight Fluid</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> totalPrice = o[<span style="color: #a31515;">&quot;Manufacturers&quot;</span>].Sum(m =&gt; (<span style="color: blue;">decimal</span>)m.SelectToken(<span style="color: #a31515;">&quot;Products[0].Price&quot;</span>));</pre>
<pre style="margin: 0px;"><span style="color: green;">// 149.95</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
-123
View File
@@ -1,123 +0,0 @@
<html>
<head>
<title>Customizing JSON serialization with attributes</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">Customizing JSON serialization with attributes</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>Attributes can be used to control how Json.NET serializes and deserializes .NET objects.</p>
<ul>
<li><a href="./html/T_Newtonsoft_Json_JsonObjectAttribute.htm">JsonObjectAttribute</a> - Placed on classes to control how it should be serialized as a JSON object.</li>
<li><a href="./html/T_Newtonsoft_Json_JsonArrayAttribute.htm">JsonArrayAttribute</a> - Placed on collections to control how it should be serialized as a JSON array.</li>
<li><a href="./html/T_Newtonsoft_Json_JsonPropertyAttribute.htm">JsonPropertyAttribute</a> - Placed on fields and properties to control how it should be serialized as a property in a JSON object.</li>
<li><a href="./html/T_Newtonsoft_Json_JsonConverterAttribute.htm">JsonConverterAttribute</a> - Placed on either classes or fields and properties to specify which JsonConverter should be used during serialization.</li>
</ul>
<p>As well as using the built-in Json.NET attributes, Json.NET also looks for the
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx" target="_blank">DataContract</a> and
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx" target="_blank">DataMember</a> attributes when determining how JSON is to be serialized and deserialized. If both are present the Json.NET serialization attributes take precedence.</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: green;">// "John Smith"</span></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: green;">// "2000-12-15T22:11:03"</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</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;">// new Date(976918263055)</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonProperty</span>]</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">JavaScriptDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> LastModified { <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 JsonProperty or DataMember attribute to be serialized) or opt-out (everything is serialized by default but can be ignored with the JsonIgnoreAttribute,
Json.NET&#39;s default behavor).</p>
<p>Json.NET serializes .NET classes that implement IEnumerable as an
JSON array populated with the IEnumerable values. Placing the JsonPropertyAttribute overrides this
behavor and forces the serializer to serialize the class's fields and properties.</p>
<h5>JsonPropertyAttribute</h5>
<p>JsonPropertyAttribute has a number of uses:</p>
<ul>
<li>By default the JSON property will have the same name as the .NET property. This attribute allows the name to be customized.</li>
<li>Indicates that a property should be serialized when member serialization is set to opt-in.</li>
<li>Includes non-public properties in serialization and deserialization.</li>
</ul>
<h5>JsonIgnoreAttribute</h5>
<p>Excludes a field or property from serialization.</p>
<h5>JsonConverterAttribute</h5>
<p>The JsonConverterAttribute specifies which JsonSerializer is used to convert an object.</p>
<p>The attribute can be placed on a class or a member. When placed on a class the JsonConverter
specified by the attribute will be the default way of serializing that class. When the attribute is on a field
or property then the specified JsonConverter will always be used to serialize that value.</p>
<p>The priority of which JsonConverter is used is member attribute then class attribute and
finally any converters passed to the 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: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">MemberConverterClass</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> DefaultConverter { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span> MemberConverter { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div></div>
<p>This example shows the JsonConverterAttribute being applied to a property.</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;">DateTime</span> date = <span style="color: #2b91af;">Convert</span>.ToDateTime(<span style="color: #a31515;">"1970-01-01T00:00:00Z"</span>).ToUniversalTime();</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">MemberConverterClass</span> c = <span style="color: blue;">new</span> <span style="color: #2b91af;">MemberConverterClass</span></pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; DefaultConverter = date,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; MemberConverter = date</pre>
<pre style="margin: 0px;">&nbsp; };</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(c, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(json);</pre>
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "DefaultConverter": "\/Date(0)\/",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "MemberConverter": "1970-01-01T00:00:00Z"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
</div>
</div></div>
<div id="footer">
</div>
</div>
</body>
</html>
-143
View File
@@ -1,143 +0,0 @@
<html>
<head>
<title>Serialization Callbacks</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">Serialization Callbacks</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>Json.NET supports serialization callback methods. A callback can be used to
manipulate an object before and after its serialization and deserialization by
the JsonSerializer.</p>
<ul>
<li><b>OnSerializing</b></li>
<li><b>OnSerialized</b></li>
<li><b>OnDeserializing</b></li>
<li><b>OnDeserialized</b></li>
</ul>
<p>To tell the serializer which methods should be called during the object&#39;s serialization
lifecycle, decorate a method with the appropraite attribute (<a
href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.onserializingattribute.aspx" target="_blank">OnSerializingAttribute</a>,
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.onserializedattribute.aspx" target="_blank">OnSerializedAttribute</a>,
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializingattribute.aspx" target="_blank">OnDeserializingAttribute</a>,
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx" target="_blank">OnDeserializedAttribute</a>).</p>
<p>Example object with serialization callback methods:</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;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SerializationEventTestObject</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// This member is serialized and deserialized with no change.</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Member1 { <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;">// The value of this field is set and reset during and </span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// after serialization.</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member2 { <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;">// This field is not serialized. The OnDeserializedAttribute </span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: green;">// is used to set the member value after serialization.</span></pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">JsonIgnore</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member3 { <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;">// This field is set to null, but populated after deserialization.</span></pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member4 { <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: blue;">public</span> SerializationEventTestObject()</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Member1 = 11;</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Member2 = <span style="color: #a31515;">"Hello World!"</span>;</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Member3 = <span style="color: #a31515;">"This is a nonserialized value"</span>;</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Member4 = <span style="color: blue;">null</span>;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">OnSerializing</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnSerializingMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Member2 = <span style="color: #a31515;">"This value went into the data file during serialization."</span>;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">OnSerialized</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnSerializedMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Member2 = <span style="color: #a31515;">"This value was reset after serialization."</span>;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">OnDeserializing</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnDeserializingMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Member3 = <span style="color: #a31515;">"This value was set during deserialization"</span>;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; [<span style="color: #2b91af;">OnDeserialized</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnDeserializedMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Member4 = <span style="color: #a31515;">"This value was set after deserialization."</span>;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div></div>
<p>The example object being serialized and deserialized by Json.NET:</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;">SerializationEventTestObject</span> obj = <span style="color: blue;">new</span> <span style="color: #2b91af;">SerializationEventTestObject</span>();</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member1);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Hello World!</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This is a nonserialized value</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
<pre style="margin: 0px;"><span style="color: green;">// null</span></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(obj, <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; "Member1": 11,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Member2": "This value went into the data file during serialization.",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Member4": null</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;">Console</span>.WriteLine(obj.Member1);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This value was reset after serialization.</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This is a nonserialized value</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
<pre style="margin: 0px;"><span style="color: green;">// null</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">obj = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">SerializationEventTestObject</span>&gt;(json);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member1);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This value went into the data file during serialization.</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This value was set during deserialization</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
<pre style="margin: 0px;"><span style="color: green;">// This value was set after deserialization.</span></pre>
</div>
</div></div>
<div id="footer">
</div>
</div>
</body>
</html>
-172
View File
@@ -1,172 +0,0 @@
<html>
<head>
<title>Serialization Error Handling</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">Serialization Error Handling</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>Json.NET supports error handling during serialization and deserialization. Error handling lets
you catch an error and choose whether to handle it and continue with serialization or let the error
bubble up and be thrown in your application.</p>
<p>Error handling is defined through two methods:
the Error event on JsonSerializer and the OnErrorAttribute.</p>
<h3>Error Event</h3>
<p>
The <a href="./html/E_Newtonsoft_Json_JsonSerializer_Error.htm">Error</a> event is an event handler found on <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a>. The error event is raised whenever an
exception is thrown while serializing or deserialing JSON. Like all settings found on JsonSerializer
it can also be set on <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializerSettings</a> and passed to the serialization methods on JsonConvert.</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: blue;">string</span>&gt; errors = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">string</span>&gt;();</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">DateTime</span>&gt; c = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">DateTime</span>&gt;&gt;(<span style="color: #a31515;">@"[</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ""2009-09-09T00:00:00Z"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ""I am not a date and will error!"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; 1</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ],</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ""1977-02-20T00:00:00Z"",</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; null,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ""2000-12-01T00:00:00Z""</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">]"</span>,</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializerSettings</span></pre>
<pre style="margin: 0px;">&nbsp; &nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; Error = <span style="color: blue;">delegate</span>(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">ErrorEventArgs</span> args)</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errors.Add(args.ErrorContext.Error.Message);</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args.ErrorContext.Handled = <span style="color: blue;">true</span>;</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; },</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; Converters = { <span style="color: blue;">new</span> <span style="color: #2b91af;">IsoDateTimeConverter</span>() }</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; });</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// 2009-09-09T00:00:00Z</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// 1977-02-20T00:00:00Z</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// 2000-12-01T00:00:00Z</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Unexpected token parsing date. Expected String, got StartArray.</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Cannot convert null value to System.DateTime.</span></pre>
</div>
</div>
</div>
<p>
In this example we are deserializing a JSON array to a collection of DateTimes. On the JsonSerializerSettings
a handler has been assigned to the Error event which will log the error message and mark the error as handled.
</p>
<p>
The result of deserializing the JSON is three successfully deserialized dates and three error messages:
one for the badly formatted string, "I am not a date and will error!", one for the nested JSON array and one
for the null value since the list doesn't allow nullable DateTimes. The event handler has logged these messages
and Json.NET has continued on deserializing the JSON because the errors were marked as handled.
</p>
<p>
One thing to note with error handling in Json.NET is that an unhandled error will bubble up and raise the event
on each of its parents, e.g. an unhandled error when serializing a collection of objects will be raised twice,
once against the object and then again on the collection. This will let you handle an error either where it
occurred or on one of its parents.
</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;">JsonSerializer</span> serializer = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializer</span>();</pre>
<pre style="margin: 0px;">serializer.Error += <span style="color: blue;">delegate</span>(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">ErrorEventArgs</span> args)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; <span style="color: green;">// only log an error once</span></pre>
<pre style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">if</span> (args.CurrentObject == args.ErrorContext.OriginalObject)</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; errors.Add(args.ErrorContext.Error.Message);</pre>
<pre style="margin: 0px;">&nbsp; };</pre>
</div>
</div></div>
<p>If you aren't immediately handling an error and only want to perform an action against it once then
you can check to see whether the <a href="./html/T_Newtonsoft_Json_Serialization_ErrorEventArgs.htm">ErrorEventArg</a>'s CurrentObject is equal to the OriginalObject.
OriginalObject is the object that threw the error and CurrentObject is the object that the event is being raised
against. They will only equal the first time the event is raised against the OriginalObject.</p>
<h3>OnErrorAttribute</h3>
<p>
The <a href="./html/T_Newtonsoft_Json_Serialization_OnErrorAttribute.htm">OnErrorAttribute</a> works much like the other <a href="SerializationCallbacks.html">.NET serialization attributes</a> that Json.NET supports.
To use it you simply place the attribute on a method which takes the correct parameters: a StreamingContext and a ErrorContext.
The name of the method doesn't matter. </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;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PersonError</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">private</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">string</span>&gt; _roles;</pre>
<pre style="margin: 0px;">&nbsp;</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; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Age { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">string</span>&gt; Roles</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">get</span></pre>
<pre style="margin: 0px;">&nbsp; &nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (_roles == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">Exception</span>(<span style="color: #a31515;">"Roles not loaded!"</span>);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> _roles;</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; }</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">set</span> { _roles = <span style="color: blue;">value</span>; }</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Title { <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;">OnError</span>]</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnError(<span style="color: #2b91af;">StreamingContext</span> context, <span style="color: #2b91af;">ErrorContext</span> errorContext)</pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; errorContext.Handled = <span style="color: blue;">true</span>;</pre>
<pre style="margin: 0px;">&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div></div>
<p>
In this example accessing the the Roles property will throw an exception when no roles have
been set. The HandleError method will set the error when serializing Roles as handled and allow Json.NET to continue
serializing the class.
</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;">PersonError</span> person = <span style="color: blue;">new</span> <span style="color: #2b91af;">PersonError</span></pre>
<pre style="margin: 0px;">&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Name = <span style="color: #a31515;">"George Michael Bluth"</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Age = 16,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Roles = <span style="color: blue;">null</span>,</pre>
<pre style="margin: 0px;">&nbsp; &nbsp; Title = <span style="color: #a31515;">"Mister Manager"</span></pre>
<pre style="margin: 0px;">&nbsp; };</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(person, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(json);</pre>
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Name": "George Michael Bluth",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Age": 16,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; "Title": "Mister Manager"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
</div>
</div></div>
<div id="footer">
</div>
</div>
</body>
</html>
-128
View File
@@ -1,128 +0,0 @@
<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>
-146
View File
@@ -1,146 +0,0 @@
<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 where you want to convert to and from a JSON string the <a href="./html/Overload_Newtonsoft_Json_JsonConvert_SerializeObject.htm">SerializeObject</a> and <a href="./html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm">DeserializeObject</a> methods on <a href="./html/T_Newtonsoft_Json_JsonConvert.htm">JsonConvert</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;">JsonConvert</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;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">Product</span>&gt;(output);</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 is able to read and write JSON text directly to
a stream via <a href="./html/T_Newtonsoft_Json_JsonTextReader.htm">JsonTextReader</a> and <a href="./html/T_Newtonsoft_Json_JsonTextWriter.htm">JsonTextWriter</a>. Other kinds of JsonWriters can
also be used such as <a href="./html/T_Newtonsoft_Json_Linq_JTokenReader.htm">JTokenReader</a>/<a href="./html/T_Newtonsoft_Json_Linq_JTokenWriter.htm">JTokenWriter</a> to
convert your object to and
from LINQ to JSON objects or <a href="./html/T_Newtonsoft_Json_Bson_BsonReader.htm">BsonReader</a>/<a href="./html/T_Newtonsoft_Json_Bson_BsonWriter.htm">BsonWriter</a> to convert to and from BSON.</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>
<p>JsonSerializer has a number of properties on it to customize how it serializes JSON.
These can also be used with the methods on JsonConvert via the
JsonSerializerSettings overloads.</p>
<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. Ignore or error.</p>
<h5>NullValueHandling</h5>
<p>Controls how null values are handled during serialization and deserialization.
Include or ignore.</p>
<h5>DefaultValueHandling</h5>
<p>Controls whether a value will be written to JSON or not if it matches the value specified in
the member's DefaultValueAttribute. Include or ignore.</p>
<h5>ObjectCreationHandling</h5>
<p>Controls how objects are created during deserialization. Auto, reuse, replace.</p>
<h5>TypeNameHandling</h5>
<p>Controls whether .NET type names are included in serialized JSON and read during deserialization when creating objects. None, Objects, Arrays or All.</p>
<h5>ConstructorHandling</h5>
<p>Controls how constructors are used when initializing objects during deserialization. Default or AllowNonPublicDefaultConstructor.</p>
<h5>Converters</h5>
<p>A collection of JsonConverters that will be used during serialization and
deserialization.</p>
<h3>JsonConverters</h3>
<p>JsonConverters allows JSON to be manually 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>DateTime JSON Converters</h5>
<p>Json.NET comes with a number of JsonConverters for serializing and deserializing DateTimes. Read
more about dates and Json.NET <a href="DatesInJSON.html">here</a>.</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
or you must define a root node name to be inserted when using this converter. This is required because properties are converted into nodes and
well formed XML can only have one root node. XmlNodeConverter has an option to
insert a root node for you.</p>
<h5>BinaryConverter</h5>
<p>Converts binary data like the SqlBinary object to JSON. The binary
data is written as a string in JSON and is encoded in Base64.</p>
<h5>CustomCreationConverter</h5>
<p>An abstract JsonConverter for customizing how an object is create during deserialization.
Inherit from this class and implement the Create method with your own code to create and return an object.
The object will then be populated with JSON values by the serializer.</p>
<p>A possible example of using this converter would be to call out to a dependency injection framework to resolve what object should be created.</p>
<div id="footer">
</div>
</div>
</body>
</html>
-125
View File
@@ -1,125 +0,0 @@
<html>
<head>
<title>Serializing Partial JSON Fragments</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 Partial JSON Fragments</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>Often when working with large JSON documents you're only interested in a small fragment of information.
This scenario can be annoying when you want to serialize that Json.NET into .NET
objects because you have to define .NET classes for the entire JSON result.</p>
<p>With Json.NET it is easy to get around this problem. Using LINQ to JSON you can
extract the pieces of JSON you want to serialize before passing them to the Json.NET serializer.</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;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SearchResult</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Title { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Content { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Url { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<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> googleSearchText = <span style="color: #a31515;">@&quot;{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;responseData&quot;&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;results&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;GsearchResultClass&quot;&quot;: &quot;&quot;GwebSearch&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;unescapedUrl&quot;&quot;: &quot;&quot;http://en.wikipedia.org/wiki/Paris_Hilton&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;url&quot;&quot;: &quot;&quot;http://en.wikipedia.org/wiki/Paris_Hilton&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;visibleUrl&quot;&quot;: &quot;&quot;en.wikipedia.org&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;cacheUrl&quot;&quot;: &quot;&quot;http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;title&quot;&quot;: &quot;&quot;&lt;b&gt;Paris Hilton&lt;/b&gt; - Wikipedia, the free encyclopedia&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;titleNoFormatting&quot;&quot;: &quot;&quot;Paris Hilton - Wikipedia, the free encyclopedia&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;content&quot;&quot;: &quot;&quot;[1] In 2006, she released her debut album...&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;GsearchResultClass&quot;&quot;: &quot;&quot;GwebSearch&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;unescapedUrl&quot;&quot;: &quot;&quot;http://www.imdb.com/name/nm0385296/&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;url&quot;&quot;: &quot;&quot;http://www.imdb.com/name/nm0385296/&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;visibleUrl&quot;&quot;: &quot;&quot;www.imdb.com&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;cacheUrl&quot;&quot;: &quot;&quot;http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;title&quot;&quot;: &quot;&quot;&lt;b&gt;Paris Hilton&lt;/b&gt;&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;titleNoFormatting&quot;&quot;: &quot;&quot;Paris Hilton&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;content&quot;&quot;: &quot;&quot;Self: Zoolander. Socialite &lt;b&gt;Paris Hilton&lt;/b&gt;...&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ],</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;cursor&quot;&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;pages&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;start&quot;&quot;: &quot;&quot;0&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;label&quot;&quot;: 1</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;start&quot;&quot;: &quot;&quot;4&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;label&quot;&quot;: 2</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;start&quot;&quot;: &quot;&quot;8&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;label&quot;&quot;: 3</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;start&quot;&quot;: &quot;&quot;12&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;label&quot;&quot;: 4</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ],</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;estimatedResultCount&quot;&quot;: &quot;&quot;59600000&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;currentPageIndex&quot;&quot;: 0,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;moreResultsUrl&quot;&quot;: &quot;&quot;http://www.google.com/search?oe=utf8&amp;ie=utf8...&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;responseDetails&quot;&quot;: null,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;responseStatus&quot;&quot;: 200</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">}&quot;</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> googleSearch = <span style="color: #2b91af;">JObject</span>.Parse(googleSearchText);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// get JSON result objects into a list</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span>&lt;<span style="color: #2b91af;">JToken</span>&gt; results = googleSearch[<span style="color: #a31515;">&quot;responseData&quot;</span>][<span style="color: #a31515;">&quot;results&quot;</span>].Children().ToList();</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// serialize JSON results into .NET objects</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span>&lt;<span style="color: #2b91af;">SearchResult</span>&gt; searchResults = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">SearchResult</span>&gt;();</pre>
<pre style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: #2b91af;">JToken</span> result <span style="color: blue;">in</span> results)</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: #2b91af;">SearchResult</span> searchResult = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">SearchResult</span>&gt;(result.ToString());</pre>
<pre style="margin: 0px;">&nbsp; searchResults.Add(searchResult);</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// Title = &lt;b&gt;Paris Hilton&lt;/b&gt; - Wikipedia, the free encyclopedia</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Content = [1] In 2006, she released her debut album...</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Url = http://en.wikipedia.org/wiki/Paris_Hilton</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// Title = &lt;b&gt;Paris Hilton&lt;/b&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Content = Self: Zoolander. Socialite &lt;b&gt;Paris Hilton&lt;/b&gt;...</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Url = http://www.imdb.com/name/nm0385296/</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
-19
View File
@@ -1,19 +0,0 @@
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;
}
-86
View File
@@ -1,86 +0,0 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<!-- The configuration and platform will be used to determine which
assemblies to include from solution and project documentation
sources -->
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{fd19f68e-72c8-4576-9775-b4480b0db686}</ProjectGuid>
<SHFBSchemaVersion>1.8.0.0</SHFBSchemaVersion>
<!-- AssemblyName, Name, and RootNamespace are not used by SHFB but Visual
Studio adds them anyway -->
<AssemblyName>Documentation</AssemblyName>
<RootNamespace>Documentation</RootNamespace>
<Name>Documentation</Name>
<!-- SHFB properties -->
<OutputPath>.\Output\</OutputPath>
<HtmlHelpName>Documentation</HtmlHelpName>
<ProjectSummary>
</ProjectSummary>
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter</MissingTags>
<VisibleItems>InheritedMembers, InheritedFrameworkMembers, Protected, SealedProtected</VisibleItems>
<HtmlHelp1xCompilerPath>
</HtmlHelp1xCompilerPath>
<HtmlHelp2xCompilerPath>
</HtmlHelp2xCompilerPath>
<SandcastlePath>
</SandcastlePath>
<WorkingPath>
</WorkingPath>
<BuildLogFile>
</BuildLogFile>
<HelpFileFormat>Help1xAndWebsite</HelpFileFormat>
<FrameworkVersion>3.5</FrameworkVersion>
<HelpTitle>Json.NET - Quick Starts &amp;amp%3b API Documentation</HelpTitle>
<PresentationStyle>Prototype</PresentationStyle>
<NamingMethod>MemberName</NamingMethod>
<DocumentationSources>
<DocumentationSource sourceFile="..\Newtonsoft.Json\bin\Release\Newtonsoft.Json.dll" />
<DocumentationSource sourceFile="..\Newtonsoft.Json\bin\Release\Newtonsoft.Json.xml" />
</DocumentationSources>
<NamespaceSummaries>
<NamespaceSummaryItem name="(global)" 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.Linq.ComponentModel" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Linq.ComponentModel&lt;/b&gt; namespace provides classes for LINQ to JSON databinding.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Schema&lt;/b&gt; namespace provides classes that are used to implement JSON schema.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Linq&lt;/b&gt; namespace provides classes that are used when serializing and deserializing JSON.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Utilities" isDocumented="False" />
<NamespaceSummaryItem name="Newtonsoft.Json.Bson" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Linq&lt;/b&gt; namespace provides classes that are used to implement BSON.</NamespaceSummaryItem>
</NamespaceSummaries>
</PropertyGroup>
<!-- There are no properties for these two groups but they need to appear in
order for Visual Studio to perform the build. -->
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
</PropertyGroup>
<ItemGroup>
<SiteMap Include="doc.sitemap" />
</ItemGroup>
<ItemGroup>
<Content Include="CustomCreationConverter.html" />
<Content Include="ContractResolver.html" />
<Content Include="custom.css" />
<Content Include="ConvertingJSONandXML.html" />
<Content Include="SerializingJSONFragments.html" />
<Content Include="SelectToken.html" />
<Content Include="ReducingSerializedJSONSize.html" />
<Content Include="donate.gif" />
<Content Include="styles.css" />
<Content Include="ReadingWritingJSON.html" />
<Content Include="LINQtoJSON.html" />
<Content Include="PreserveObjectReferences.html" />
<Content Include="SerializationErrorHandling.html" />
<Content Include="SerializationCallbacks.html" />
<Content Include="SerializingCollections.html" />
<Content Include="DatesInJSON.html" />
<Content Include="SerializationAttributes.html" />
<Content Include="SerializingJSON.html" />
<Content Include="Introduction.html" />
</ItemGroup>
<!-- Import the SHFB build targets -->
<Import Project="$(SHFBROOT)\SandcastleHelpFileBuilder.targets" />
</Project>
-21
View File
@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<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 Collections" url="SerializingCollections.html" />
<siteMapNode title="Serialization Callbacks" url="SerializationCallbacks.html" />
<siteMapNode title="Serialization Error Handling" url="SerializationErrorHandling.html" />
<siteMapNode title="Serialization and Preserving Object References" url="PreserveObjectReferences.html" />
<siteMapNode title="CustomCreationConverter" url="CustomCreationConverter.html" />
<siteMapNode title="Contract Resolvers" url="ContractResolver.html" />
<siteMapNode title="Reducing Serialized JSON Size" url="ReducingSerializedJSONSize.html" />
<siteMapNode title="Serializing Partial JSON Fragments" url="SerializingJSONFragments.html" />
</siteMapNode>
<siteMapNode title="LINQ to JSON" url="LINQtoJSON.html">
<siteMapNode title="Querying LINQ to JSON with SelectToken" url="SelectToken.html" />
</siteMapNode>
<siteMapNode title="Reading and writing JSON" url="ReadingWritingJSON.html" />
<siteMapNode title="Converting between JSON and XML" url="ConvertingJSONandXML.html" />
</siteMap>
Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

-439
View File
@@ -1,439 +0,0 @@
/* 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;
}
-22
View File
@@ -11,28 +11,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json", "Newtonso
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.Tests", "Newtonsoft.Json.Tests\Newtonsoft.Json.Tests.csproj", "{3E6E2335-B079-4B5B-A65A-9D586914BCBB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Doc", "Doc", "{8F9E13D9-4181-4BA6-8802-EB5B3EB9445A}"
ProjectSection(SolutionItems) = preProject
Doc\ContractResolver.html = Doc\ContractResolver.html
Doc\custom.css = Doc\custom.css
Doc\CustomCreationConverter.html = Doc\CustomCreationConverter.html
Doc\DatesInJSON.html = Doc\DatesInJSON.html
Doc\doc.sitemap = Doc\doc.sitemap
Doc\donate.gif = Doc\donate.gif
Doc\Introduction.html = Doc\Introduction.html
Doc\LINQtoJSON.html = Doc\LINQtoJSON.html
Doc\PreserveObjectReferences.html = Doc\PreserveObjectReferences.html
Doc\ReadingWritingJSON.html = Doc\ReadingWritingJSON.html
Doc\ReducingSerializedJSONSize.html = Doc\ReducingSerializedJSONSize.html
Doc\SelectToken.html = Doc\SelectToken.html
Doc\SerializationAttributes.html = Doc\SerializationAttributes.html
Doc\SerializationErrorHandling.html = Doc\SerializationErrorHandling.html
Doc\SerializingCollections.html = Doc\SerializingCollections.html
Doc\SerializingJSON.html = Doc\SerializingJSON.html
Doc\SerializingJSONFragments.html = Doc\SerializingJSONFragments.html
Doc\styles.css = Doc\styles.css
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -316,6 +316,11 @@ namespace Newtonsoft.Json.Serialization
}
#if !SILVERLIGHT && !PocketPC
/// <summary>
/// Creates a <see cref="JsonISerializableContract"/> for the given type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>A <see cref="JsonISerializableContract"/> for the given type.</returns>
protected virtual JsonISerializableContract CreateISerializableContract(Type objectType)
{
JsonISerializableContract contract = new JsonISerializableContract(objectType);
@@ -333,6 +338,11 @@ namespace Newtonsoft.Json.Serialization
}
#endif
/// <summary>
/// Creates a <see cref="JsonStringContract"/> for the given type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>A <see cref="JsonStringContract"/> for the given type.</returns>
protected virtual JsonStringContract CreateStringContract(Type objectType)
{
JsonStringContract contract = new JsonStringContract(objectType);
@@ -341,6 +351,11 @@ namespace Newtonsoft.Json.Serialization
return contract;
}
/// <summary>
/// Determines which contract type is created for the given type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>A <see cref="JsonContract"/> for the given type.</returns>
protected virtual JsonContract CreateContract(Type objectType)
{
if (JsonConvert.IsJsonPrimitiveType(objectType))
@@ -37,6 +37,10 @@ namespace Newtonsoft.Json.Serialization
/// </summary>
public class JsonISerializableContract : JsonContract
{
/// <summary>
/// Gets or sets the ISerializable object constructor.
/// </summary>
/// <value>The ISerializable object constructor.</value>
public ObjectConstructor<object> ISerializableCreator { get; set; }
/// <summary>
@@ -25,5 +25,8 @@
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// Represents a method that constructs an object.
/// </summary>
public delegate object ObjectConstructor<T>(params object[] args);
}