-Line endings
This commit is contained in:
+91
-91
@@ -1,92 +1,92 @@
|
||||
<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;"> {</pre>
|
||||
|
||||
<pre style="margin: 0px;"> 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;"> Name = <span style="color: rgb(163, 21, 21);">"Widget"</span>,</pre>
|
||||
|
||||
<pre style="margin: 0px;"> Price = 9.99m,</pre>
|
||||
|
||||
<pre style="margin: 0px;"> 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;"> };</pre>
|
||||
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = </pre>
|
||||
|
||||
<pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(</pre>
|
||||
|
||||
<pre style="margin: 0px;"> product,</pre>
|
||||
|
||||
<pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
|
||||
<pre style="margin: 0px;"> <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;"> );</pre>
|
||||
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "name": "Widget",</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "expiryDate": "\/Date(1292868060000)\/",</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "price": 9.99,</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "sizes": [</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Small",</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Medium",</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Large"</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
<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;"> {</pre>
|
||||
|
||||
<pre style="margin: 0px;"> 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;"> Name = <span style="color: rgb(163, 21, 21);">"Widget"</span>,</pre>
|
||||
|
||||
<pre style="margin: 0px;"> Price = 9.99m,</pre>
|
||||
|
||||
<pre style="margin: 0px;"> 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;"> };</pre>
|
||||
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = </pre>
|
||||
|
||||
<pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(</pre>
|
||||
|
||||
<pre style="margin: 0px;"> product,</pre>
|
||||
|
||||
<pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
|
||||
<pre style="margin: 0px;"> <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;"> );</pre>
|
||||
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "name": "Widget",</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "expiryDate": "\/Date(1292868060000)\/",</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "price": 9.99,</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "sizes": [</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Small",</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Medium",</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Large"</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+145
-145
@@ -1,146 +1,146 @@
|
||||
<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 @ and should be at the start of the object. </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, #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;">@"<?xml version=""1.0"" standalone=""no""?></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"><root></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <person id=""1""></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <name>Alan</name></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <url>http://www.google.com</url></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> </person></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <person id=""2""></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <name>Louis</name></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <url>http://www.yahoo.com</url></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> </person></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"></root>"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </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;">// "?xml": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "@version": "1.0",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "@standalone": "no"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "root": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "person": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "@id": "1",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "name": "Alan",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "url": "http://www.google.com"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "@id": "2",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "name": "Louis",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "url": "http://www.yahoo.com"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</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;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""?xml"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@version"": ""1.0"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@standalone"": ""no""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""root"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""person"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@id"": ""1"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""name"": ""Alan"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.google.com""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@id"": ""2"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""name"": ""Louis"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.yahoo.com""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </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;">// <?xml version="1.0" standalone="no"?></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <root></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <person id="1"></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <name>Alan</name></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <url>http://www.google.com</url></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// </person></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <person id="2"></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <name>Louis</name></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <url>http://www.yahoo.com</url></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// </person></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// </root></span></pre>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
<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 @ and should be at the start of the object. </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, #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;">@"<?xml version=""1.0"" standalone=""no""?></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"><root></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <person id=""1""></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <name>Alan</name></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <url>http://www.google.com</url></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> </person></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <person id=""2""></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <name>Louis</name></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> <url>http://www.yahoo.com</url></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> </person></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"></root>"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </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;">// "?xml": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "@version": "1.0",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "@standalone": "no"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "root": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "person": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "@id": "1",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "name": "Alan",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "url": "http://www.google.com"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "@id": "2",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "name": "Louis",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "url": "http://www.yahoo.com"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</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;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""?xml"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@version"": ""1.0"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@standalone"": ""no""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""root"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""person"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@id"": ""1"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""name"": ""Alan"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.google.com""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@id"": ""2"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""name"": ""Louis"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.yahoo.com""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </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;">// <?xml version="1.0" standalone="no"?></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <root></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <person id="1"></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <name>Alan</name></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <url>http://www.google.com</url></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// </person></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <person id="2"></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <name>Louis</name></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// <url>http://www.yahoo.com</url></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// </person></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// </root></span></pre>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,27 +1,27 @@
|
||||
<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">
|
||||
<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>
|
||||
@@ -48,14 +48,14 @@
|
||||
<pre style="margin: 0px;"> }</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></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;">// {</span></pre>
|
||||
@@ -89,11 +89,11 @@ This is an extremely simple example. A more complicated scenario could involve a
|
||||
<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>
|
||||
|
||||
</div></div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+100
-100
@@ -1,101 +1,101 @@
|
||||
<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>
|
||||
is the ISO 8601 standard: 2012-03-19T07:22Z. You can read more about it <a href="http://en.wikipedia.org/wiki/ISO_8601" target="_blank">here</a>.</p>
|
||||
<p>Prior to Json.NET 4.5 dates were written using the Microsoft format:
|
||||
"\/Date(1198908717056)\/". If you want to use this format, or you want to
|
||||
maintain compatibility with Microsoft JSON serializers or older versions of
|
||||
Json.NET then change the DateFormatHandling setting to MicrosoftDateFormat.</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;"> <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;"> <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;"> </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;"> <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;"> {</pre>
|
||||
|
||||
<pre style="margin: 0px;"> LogDate = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DateTime</span>(2009, 2, 15, 0, 0, 0, <span style="color: rgb(43, 145, 175);">DateTimeKind</span>.Utc),</pre>
|
||||
<pre style="margin: 0px;"> Details = <span style="color: rgb(163, 21, 21);">"Application started."</span></pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> defaultJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry);</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}</span></pre>
|
||||
<pre style="margin: 0px;"></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> javascriptJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JavaScriptDateTimeConverter</span>());</pre>
|
||||
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// {"Details":"Application started.","LogDate":new Date(1234656000000)}</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> isoJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">IsoDateTimeConverter</span>());</pre>
|
||||
<pre style="margin: 0px;"> <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.
|
||||
Note that since Json.NET 4.5 dates are written using the ISO 8601 format by
|
||||
default and using this converter is unnecessary.</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> </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>
|
||||
|
||||
<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>
|
||||
is the ISO 8601 standard: 2012-03-19T07:22Z. You can read more about it <a href="http://en.wikipedia.org/wiki/ISO_8601" target="_blank">here</a>.</p>
|
||||
<p>Prior to Json.NET 4.5 dates were written using the Microsoft format:
|
||||
"\/Date(1198908717056)\/". If you want to use this format, or you want to
|
||||
maintain compatibility with Microsoft JSON serializers or older versions of
|
||||
Json.NET then change the DateFormatHandling setting to MicrosoftDateFormat.</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;"> <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;"> <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;"> </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;"> <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;"> {</pre>
|
||||
|
||||
<pre style="margin: 0px;"> LogDate = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">DateTime</span>(2009, 2, 15, 0, 0, 0, <span style="color: rgb(43, 145, 175);">DateTimeKind</span>.Utc),</pre>
|
||||
<pre style="margin: 0px;"> Details = <span style="color: rgb(163, 21, 21);">"Application started."</span></pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> defaultJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry);</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}</span></pre>
|
||||
<pre style="margin: 0px;"></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> javascriptJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JavaScriptDateTimeConverter</span>());</pre>
|
||||
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// {"Details":"Application started.","LogDate":new Date(1234656000000)}</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> isoJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">IsoDateTimeConverter</span>());</pre>
|
||||
<pre style="margin: 0px;"> <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.
|
||||
Note that since Json.NET 4.5 dates are written using the ISO 8601 format by
|
||||
default and using this converter is unnecessary.</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> </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
-68
@@ -1,68 +1,68 @@
|
||||
<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'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¤cy_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>
|
||||
<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'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¤cy_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>
|
||||
|
||||
+209
-209
@@ -1,198 +1,198 @@
|
||||
<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;"> </pre>
|
||||
<pre style="margin: 0px;">array.Add(text);</pre>
|
||||
<pre style="margin: 0px;">array.Add(date);</pre>
|
||||
<pre style="margin: 0px;"> </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;">// "Manual text",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "\/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><<span style="color: #2b91af;">Post</span>> posts = GetPosts();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> rss =</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"channel"</span>,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
|
||||
<pre style="margin: 0px;"> <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;"> <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;"> <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;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"item"</span>,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JArray</span>(</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">from</span> p <span style="color: blue;">in</span> posts</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">orderby</span> p.Title</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
|
||||
<pre style="margin: 0px;"> <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;"> <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;"> <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;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"category"</span>,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JArray</span>(</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">from</span> c <span style="color: blue;">in</span> p.Categories</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">JValue</span>(c)))))))));</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(rss.ToString());</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "channel": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "title": "James Newton-King",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "link": "http://james.newtonking.com",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "description": "James Newton-King's blog.",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "item": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "title": "Json.NET 1.3 + New license + Now on CodePlex",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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;">// "link": "http://james.newtonking.com/projects/json-net.aspx",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "category": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Json.NET",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "CodePlex"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "title": "LINQ to JSON beta",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "description": "Annoucing LINQ to JSON",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "link": "http://james.newtonking.com/projects/json-net.aspx",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "category": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Json.NET",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LINQ"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</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;"> channel = <span style="color: blue;">new</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> title = <span style="color: #a31515;">"James Newton-King"</span>,</pre>
|
||||
<pre style="margin: 0px;"> link = <span style="color: #a31515;">"http://james.newtonking.com"</span>,</pre>
|
||||
<pre style="margin: 0px;"> description = <span style="color: #a31515;">"James Newton-King's blog."</span>,</pre>
|
||||
<pre style="margin: 0px;"> item =</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">from</span> p <span style="color: blue;">in</span> posts</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">orderby</span> p.Title</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> <span style="color: blue;">new</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> title = p.Title,</pre>
|
||||
<pre style="margin: 0px;"> description = p.Description,</pre>
|
||||
<pre style="margin: 0px;"> link = p.Link,</pre>
|
||||
<pre style="margin: 0px;"> category = p.Categories</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> }</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;"> CPU: 'Intel',</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> Drives: [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> 'DVD read/writer',</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""500 gigabyte hard drive""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </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'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;"> <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;"> <span style="color: blue;">select</span> (<span style="color: blue;">string</span>)p[<span style="color: #a31515;">"title"</span>];</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <span style="color: #2b91af;">Console</span>.WriteLine(item);</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">var</span> categories =</pre>
|
||||
<pre style="margin: 0px;"> <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<<span style="color: blue;">string</span>>()</pre>
|
||||
<pre style="margin: 0px;"> <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;"> <span style="color: blue;">orderby</span> g.Count() <span style="color: blue;">descending</span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> <span style="color: blue;">new</span> { Category = g.Key, Count = g.Count() };</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <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;"> </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">
|
||||
<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;"> </pre>
|
||||
<pre style="margin: 0px;">array.Add(text);</pre>
|
||||
<pre style="margin: 0px;">array.Add(date);</pre>
|
||||
<pre style="margin: 0px;"> </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;">// "Manual text",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "\/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><<span style="color: #2b91af;">Post</span>> posts = GetPosts();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> rss =</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"channel"</span>,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
|
||||
<pre style="margin: 0px;"> <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;"> <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;"> <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;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"item"</span>,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JArray</span>(</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">from</span> p <span style="color: blue;">in</span> posts</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">orderby</span> p.Title</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
|
||||
<pre style="margin: 0px;"> <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;"> <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;"> <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;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"category"</span>,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JArray</span>(</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">from</span> c <span style="color: blue;">in</span> p.Categories</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">JValue</span>(c)))))))));</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(rss.ToString());</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "channel": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "title": "James Newton-King",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "link": "http://james.newtonking.com",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "description": "James Newton-King's blog.",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "item": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "title": "Json.NET 1.3 + New license + Now on CodePlex",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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;">// "link": "http://james.newtonking.com/projects/json-net.aspx",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "category": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Json.NET",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "CodePlex"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "title": "LINQ to JSON beta",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "description": "Annoucing LINQ to JSON",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "link": "http://james.newtonking.com/projects/json-net.aspx",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "category": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Json.NET",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LINQ"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</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;"> channel = <span style="color: blue;">new</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> title = <span style="color: #a31515;">"James Newton-King"</span>,</pre>
|
||||
<pre style="margin: 0px;"> link = <span style="color: #a31515;">"http://james.newtonking.com"</span>,</pre>
|
||||
<pre style="margin: 0px;"> description = <span style="color: #a31515;">"James Newton-King's blog."</span>,</pre>
|
||||
<pre style="margin: 0px;"> item =</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">from</span> p <span style="color: blue;">in</span> posts</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">orderby</span> p.Title</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> <span style="color: blue;">new</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> title = p.Title,</pre>
|
||||
<pre style="margin: 0px;"> description = p.Description,</pre>
|
||||
<pre style="margin: 0px;"> link = p.Link,</pre>
|
||||
<pre style="margin: 0px;"> category = p.Categories</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> }</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;"> CPU: 'Intel',</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> Drives: [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> 'DVD read/writer',</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""500 gigabyte hard drive""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </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'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;"> <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;"> <span style="color: blue;">select</span> (<span style="color: blue;">string</span>)p[<span style="color: #a31515;">"title"</span>];</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <span style="color: #2b91af;">Console</span>.WriteLine(item);</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">var</span> categories =</pre>
|
||||
<pre style="margin: 0px;"> <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<<span style="color: blue;">string</span>>()</pre>
|
||||
<pre style="margin: 0px;"> <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;"> <span style="color: blue;">orderby</span> g.Count() <span style="color: blue;">descending</span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">select</span> <span style="color: blue;">new</span> { Category = g.Key, Count = g.Count() };</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <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;"> </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>
|
||||
@@ -208,12 +208,12 @@
|
||||
<pre style="margin: 0px;"> <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></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;"> ""short"":{</span></pre>
|
||||
@@ -243,12 +243,12 @@
|
||||
<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>
|
||||
|
||||
|
||||
</div></div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,27 +1,27 @@
|
||||
<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">
|
||||
|
||||
<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;"> {</pre>
|
||||
@@ -49,16 +49,16 @@
|
||||
<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">
|
||||
<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;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializerSettings</span> { PreserveReferencesHandling = <span style="color: #2b91af;">PreserveReferencesHandling</span>.Objects });</pre>
|
||||
@@ -92,27 +92,27 @@
|
||||
<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">
|
||||
|
||||
|
||||
<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>
|
||||
@@ -121,18 +121,18 @@
|
||||
<pre style="margin: 0px;"> <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>
|
||||
|
||||
|
||||
</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>
|
||||
+86
-86
@@ -1,87 +1,87 @@
|
||||
<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;"> </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;"> jsonWriter.Formatting = <span style="color: #2b91af;">Formatting</span>.Indented;</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteStartObject();</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WritePropertyName(<span style="color: #a31515;">"CPU"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"Intel"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WritePropertyName(<span style="color: #a31515;">"PSU"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"500W"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WritePropertyName(<span style="color: #a31515;">"Drives"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteStartArray();</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"DVD read/writer"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteComment(<span style="color: #a31515;">"(broken)"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"500 gigabyte hard drive"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"200 gigabype hard drive"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteEnd();</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteEndObject();</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "CPU": "Intel",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "PSU": "500W",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Drives": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "DVD read/writer"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// /*(broken)*/,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "500 gigabyte hard drive",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "200 gigabype hard drive"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</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;"> <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;"> <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;"> );</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </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>
|
||||
|
||||
<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;"> </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;"> jsonWriter.Formatting = <span style="color: #2b91af;">Formatting</span>.Indented;</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteStartObject();</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WritePropertyName(<span style="color: #a31515;">"CPU"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"Intel"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WritePropertyName(<span style="color: #a31515;">"PSU"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"500W"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WritePropertyName(<span style="color: #a31515;">"Drives"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteStartArray();</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"DVD read/writer"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteComment(<span style="color: #a31515;">"(broken)"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"500 gigabyte hard drive"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteValue(<span style="color: #a31515;">"200 gigabype hard drive"</span>);</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteEnd();</pre>
|
||||
<pre style="margin: 0px;"> jsonWriter.WriteEndObject();</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "CPU": "Intel",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "PSU": "500W",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Drives": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "DVD read/writer"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// /*(broken)*/,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "500 gigabyte hard drive",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "200 gigabype hard drive"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</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;"> <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;"> <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;"> );</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </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>
|
||||
+279
-279
@@ -1,280 +1,280 @@
|
||||
<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;"> <span style="color: green;">// included in JSON</span></pre>
|
||||
<pre style="margin: 0px;"> <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;"> <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;"> <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">List</span><<span style="color: blue;">string</span>> Features { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// ignored</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: rgb(43, 145, 175);">JsonIgnore</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> <span style="color: green;">// included in JSON</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: rgb(43, 145, 175);">DataMember</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> [<span style="color: rgb(43, 145, 175);">DataMember</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// ignored</span></pre>
|
||||
<pre style="margin: 0px;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">List</span><<span style="color: blue;">string</span>> 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;"> </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;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { });</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Name": "Bad Boys III",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Description": "It's no Bad Boys",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Classification": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Studio": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "ReleaseDate": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "ReleaseCountries": null</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"> </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;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Name": "Bad Boys III",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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;"> <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;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// false is default value of bool</span></pre>
|
||||
<pre style="margin: 0px;"> <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;"> <span style="color: green;">// null is default value of nullable</span></pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// customize default values</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: rgb(43, 145, 175);">DefaultValue</span>(30)]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> [<span style="color: rgb(43, 145, 175);">DefaultValue</span>(<span style="color: rgb(163, 21, 21);">""</span>)]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> Company = <span style="color: rgb(163, 21, 21);">"Acme Ltd."</span>,</pre>
|
||||
<pre style="margin: 0px;"> Amount = 50.0m,</pre>
|
||||
<pre style="margin: 0px;"> Paid = <span style="color: blue;">false</span>,</pre>
|
||||
<pre style="margin: 0px;"> FollowUpDays = 30,</pre>
|
||||
<pre style="margin: 0px;"> FollowUpEmailAddress = <span style="color: blue;">string</span>.Empty,</pre>
|
||||
<pre style="margin: 0px;"> PaidDate = <span style="color: blue;">null</span></pre>
|
||||
<pre style="margin: 0px;">};</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { });</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Company": "Acme Ltd.",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Amount": 50.0,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Paid": false,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "PaidDate": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "FollowUpDays": 30,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "FollowUpEmailAddress": ""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"> </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;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Company": "Acme Ltd.",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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;"> <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: blue;">char</span> _startingWithChar;</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> DynamicContractResolver(<span style="color: blue;">char</span> startingWithChar)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> _startingWithChar = startingWithChar;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: rgb(43, 145, 175);">IList</span><<span style="color: rgb(43, 145, 175);">JsonProperty</span>> CreateProperties(<span style="color: rgb(43, 145, 175);">JsonObjectContract</span> contract)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">IList</span><<span style="color: rgb(43, 145, 175);">JsonProperty</span>> properties = <span style="color: blue;">base</span>.CreateProperties(contract);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// only serializer properties that start with the specified character</span></pre>
|
||||
<pre style="margin: 0px;"> properties = </pre>
|
||||
<pre style="margin: 0px;"> properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">return</span> properties;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <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;"> <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;"> <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;"> <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;"> <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;"> {</pre>
|
||||
<pre style="margin: 0px;"> BookName = <span style="color: rgb(163, 21, 21);">"The Gathering Storm"</span>,</pre>
|
||||
<pre style="margin: 0px;"> BookPrice = 16.19m,</pre>
|
||||
<pre style="margin: 0px;"> AuthorName = <span style="color: rgb(163, 21, 21);">"Brandon Sanderson"</span>,</pre>
|
||||
<pre style="margin: 0px;"> AuthorAge = 34,</pre>
|
||||
<pre style="margin: 0px;"> AuthorCountry = <span style="color: rgb(163, 21, 21);">"United States of America"</span></pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "AuthorName": "Brandon Sanderson",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "AuthorAge": 34,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "AuthorCountry": "United States of America"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"> </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;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BookName": "The Gathering Storm",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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>
|
||||
|
||||
<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;"> <span style="color: green;">// included in JSON</span></pre>
|
||||
<pre style="margin: 0px;"> <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;"> <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;"> <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">List</span><<span style="color: blue;">string</span>> Features { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// ignored</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: rgb(43, 145, 175);">JsonIgnore</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> <span style="color: green;">// included in JSON</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: rgb(43, 145, 175);">DataMember</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> [<span style="color: rgb(43, 145, 175);">DataMember</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// ignored</span></pre>
|
||||
<pre style="margin: 0px;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">List</span><<span style="color: blue;">string</span>> 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;"> </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;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { });</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Name": "Bad Boys III",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Description": "It's no Bad Boys",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Classification": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Studio": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "ReleaseDate": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "ReleaseCountries": null</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"> </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;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Name": "Bad Boys III",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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;"> <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;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// false is default value of bool</span></pre>
|
||||
<pre style="margin: 0px;"> <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;"> <span style="color: green;">// null is default value of nullable</span></pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// customize default values</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: rgb(43, 145, 175);">DefaultValue</span>(30)]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> [<span style="color: rgb(43, 145, 175);">DefaultValue</span>(<span style="color: rgb(163, 21, 21);">""</span>)]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> Company = <span style="color: rgb(163, 21, 21);">"Acme Ltd."</span>,</pre>
|
||||
<pre style="margin: 0px;"> Amount = 50.0m,</pre>
|
||||
<pre style="margin: 0px;"> Paid = <span style="color: blue;">false</span>,</pre>
|
||||
<pre style="margin: 0px;"> FollowUpDays = 30,</pre>
|
||||
<pre style="margin: 0px;"> FollowUpEmailAddress = <span style="color: blue;">string</span>.Empty,</pre>
|
||||
<pre style="margin: 0px;"> PaidDate = <span style="color: blue;">null</span></pre>
|
||||
<pre style="margin: 0px;">};</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> { });</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Company": "Acme Ltd.",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Amount": 50.0,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Paid": false,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "PaidDate": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "FollowUpDays": 30,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "FollowUpEmailAddress": ""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"> </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;"> <span style="color: rgb(43, 145, 175);">Formatting</span>.Indented,</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Company": "Acme Ltd.",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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;"> <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: blue;">char</span> _startingWithChar;</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> DynamicContractResolver(<span style="color: blue;">char</span> startingWithChar)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> _startingWithChar = startingWithChar;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: rgb(43, 145, 175);">IList</span><<span style="color: rgb(43, 145, 175);">JsonProperty</span>> CreateProperties(<span style="color: rgb(43, 145, 175);">JsonObjectContract</span> contract)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">IList</span><<span style="color: rgb(43, 145, 175);">JsonProperty</span>> properties = <span style="color: blue;">base</span>.CreateProperties(contract);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// only serializer properties that start with the specified character</span></pre>
|
||||
<pre style="margin: 0px;"> properties = </pre>
|
||||
<pre style="margin: 0px;"> properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">return</span> properties;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <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;"> <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;"> <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;"> <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;"> <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;"> {</pre>
|
||||
<pre style="margin: 0px;"> BookName = <span style="color: rgb(163, 21, 21);">"The Gathering Storm"</span>,</pre>
|
||||
<pre style="margin: 0px;"> BookPrice = 16.19m,</pre>
|
||||
<pre style="margin: 0px;"> AuthorName = <span style="color: rgb(163, 21, 21);">"Brandon Sanderson"</span>,</pre>
|
||||
<pre style="margin: 0px;"> AuthorAge = 34,</pre>
|
||||
<pre style="margin: 0px;"> AuthorCountry = <span style="color: rgb(163, 21, 21);">"United States of America"</span></pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </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;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "AuthorName": "Brandon Sanderson",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "AuthorAge": 34,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "AuthorCountry": "United States of America"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
||||
<pre style="margin: 0px;"> </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;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BookName": "The Gathering Storm",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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>
|
||||
+115
-115
@@ -1,116 +1,116 @@
|
||||
<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;">"Manufacturers[0].Name"</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't be
|
||||
found at the path'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;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Stores"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Lambton Quay"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Willis Street""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Manufacturers"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Acme Co"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Products"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Anvil"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 50</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Contoso"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Products"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Elbow Grease"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 99.95</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Headlight Fluid"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 4</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[0].Name"</span>);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Acme Co</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> productPrice = (<span style="color: blue;">decimal</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[0].Products[0].Price"</span>);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 50</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> productName = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[1].Products[0].Name"</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><<span style="color: blue;">string</span>> storeNames = o.SelectToken(<span style="color: #a31515;">"Stores"</span>).Select(s => (<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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span><<span style="color: blue;">string</span>> firstProductNames = o[<span style="color: #a31515;">"Manufacturers"</span>].Select(m => (<span style="color: blue;">string</span>)m.SelectToken(<span style="color: #a31515;">"Products[1].Name"</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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> totalPrice = o[<span style="color: #a31515;">"Manufacturers"</span>].Sum(m => (<span style="color: blue;">decimal</span>)m.SelectToken(<span style="color: #a31515;">"Products[0].Price"</span>));</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 149.95</span></pre>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
<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;">"Manufacturers[0].Name"</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't be
|
||||
found at the path'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;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Stores"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Lambton Quay"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Willis Street""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Manufacturers"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Acme Co"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Products"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Anvil"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 50</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Contoso"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Products"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Elbow Grease"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 99.95</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Headlight Fluid"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 4</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[0].Name"</span>);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Acme Co</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> productPrice = (<span style="color: blue;">decimal</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[0].Products[0].Price"</span>);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 50</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> productName = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">"Manufacturers[1].Products[0].Name"</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><<span style="color: blue;">string</span>> storeNames = o.SelectToken(<span style="color: #a31515;">"Stores"</span>).Select(s => (<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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span><<span style="color: blue;">string</span>> firstProductNames = o[<span style="color: #a31515;">"Manufacturers"</span>].Select(m => (<span style="color: blue;">string</span>)m.SelectToken(<span style="color: #a31515;">"Products[1].Name"</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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> totalPrice = o[<span style="color: #a31515;">"Manufacturers"</span>].Sum(m => (<span style="color: blue;">decimal</span>)m.SelectToken(<span style="color: #a31515;">"Products[0].Price"</span>));</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 149.95</span></pre>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+122
-122
@@ -1,123 +1,123 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization 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">Serialization 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;"> <span style="color: green;">// "John Smith"</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// "2000-12-15T22:11:03"</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// new Date(976918263055)</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">JavaScriptDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// not serialized</span></pre>
|
||||
<pre style="margin: 0px;"> <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'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;"> <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;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </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;"> {</pre>
|
||||
<pre style="margin: 0px;"> DefaultConverter = date,</pre>
|
||||
<pre style="margin: 0px;"> MemberConverter = date</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </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;">// "DefaultConverter": "\/Date(0)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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>
|
||||
|
||||
<head>
|
||||
<title>Serialization 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">Serialization 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;"> <span style="color: green;">// "John Smith"</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// "2000-12-15T22:11:03"</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// new Date(976918263055)</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonProperty</span>]</pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">JavaScriptDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// not serialized</span></pre>
|
||||
<pre style="margin: 0px;"> <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'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;"> <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;"> [<span style="color: #2b91af;">JsonConverter</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </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;"> {</pre>
|
||||
<pre style="margin: 0px;"> DefaultConverter = date,</pre>
|
||||
<pre style="margin: 0px;"> MemberConverter = date</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </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;">// "DefaultConverter": "\/Date(0)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "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>
|
||||
@@ -1,38 +1,38 @@
|
||||
<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'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">
|
||||
<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'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>
|
||||
@@ -84,11 +84,11 @@
|
||||
<pre style="margin: 0px;"> }</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></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;"> </pre>
|
||||
@@ -129,15 +129,15 @@
|
||||
<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>
|
||||
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,33 +1,33 @@
|
||||
<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">
|
||||
<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><<span style="color: blue;">string</span>> errors = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>>();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
@@ -71,15 +71,15 @@
|
||||
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">
|
||||
<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>
|
||||
@@ -89,21 +89,21 @@
|
||||
<pre style="margin: 0px;"> errors.Add(args.ErrorContext.Error.Message);</pre>
|
||||
<pre style="margin: 0px;"> };</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></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>
|
||||
@@ -131,14 +131,14 @@ against. They will only equal the first time the event is raised against the Ori
|
||||
<pre style="margin: 0px;"> }</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></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;"> {</pre>
|
||||
@@ -158,15 +158,15 @@ against. They will only equal the first time the event is raised against the Ori
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+338
-338
@@ -1,338 +1,338 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Serialization Guide</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
<style type="text/css">
|
||||
.style1
|
||||
{
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization Guide</span></div>
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"></span>
|
||||
<p>
|
||||
The Json.NET serializer can serialize a wide variety of .NET objects. This guide
|
||||
looks at how it works at a high level and in more detail.</p>
|
||||
<h3>
|
||||
Summary</h3>
|
||||
<p>
|
||||
At a high level, the Json.NET serializer will convert primitive .NET values into
|
||||
primitive JSON values, .NET arrays and collections to JSON arrays and everything
|
||||
else to JSON objects.</p>
|
||||
<p>
|
||||
Json.NET will throw an error if it encounters incorrect JSON when deserializing
|
||||
a value. For example if the serializer encounters a JSON property with an array
|
||||
of values and the type of matching .NET property is not a collection then an error
|
||||
will be thrown, and vice-versa.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>Complex Types</strong></h4>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
.NET
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
JSON
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>IList, IEnumerable, IList<T>, Array</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Array
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>IDictionary, IDictionary<TKey, TValue></b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Object
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Object (more detail below)</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Object
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<h4>
|
||||
<strong>Primitive Types</strong></h4>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
.NET
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
JSON
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>String</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Byte<br />
|
||||
SByte<br />
|
||||
UInt16<br />
|
||||
Int16<br />
|
||||
UInt32<br />
|
||||
Int32<br />
|
||||
UInt64<br />
|
||||
Int64<br />
|
||||
<br />
|
||||
</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Integer
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Float<br />
|
||||
Double<br />
|
||||
Decimal<br />
|
||||
<br />
|
||||
</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Float
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Enum</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Integer (can be string with <a href="html/T_Newtonsoft_Json_Converters_StringEnumConverter.htm">
|
||||
StringEnumConverter</a>)
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>DateTime</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String (<a href="DatesInJSON.html">Serializing Dates in JSON</a>)
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Byte[]</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String (base 64 encoded)
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Type</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String (type name)
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Guid</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b><a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx"
|
||||
target="_blank">TypeConverter</a> (convertible to String)</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<h3>
|
||||
Breakdown of Type Serialization</h3>
|
||||
<h4>
|
||||
<strong>Objects</strong></h4>
|
||||
<p>
|
||||
.NET types that don't fall into any other category listed below (i.e. aren't lists,
|
||||
dictionaries, dynamic, implement ISerializable, etc) are serialized as JSON objects.</p>
|
||||
<p>
|
||||
By default types are serialized in opt-out mode. What that means is all public fields
|
||||
and properties with getters are automatically serialized to JSON and members that
|
||||
shouldn't be serialized are opted-out using the JsonIgnoreAttribute. To serialize
|
||||
private members the JsonPropertyAttribute can be placed on private fields and properties
|
||||
or the DefaultMembersSearchFlags can be changed on DefaultContractResolver to change
|
||||
how members are serialized on all types.</p>
|
||||
<p>
|
||||
Types can also be serialized using opt-in mode. Only fields that have a JsonPropertyAttribute
|
||||
or DataMemberAttribute on them will be serialized. Opt-in mode for an object is
|
||||
specified using the JsonObjectAttribute on the class.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>IEnumerable, Lists and Arrays</strong></h4>
|
||||
<p>
|
||||
.NET lists (types that inherit from IEnumerable) and .NET arrays are converted to
|
||||
JSON arrays. Because JSON arrays only support a range of values and not properties,
|
||||
any additional properties and fields declared on .NET collections are not serialized.
|
||||
In situations where a JSON array is not wanted the JsonObjectAttribute can be placed
|
||||
on a .NET type that implements IEnumerable to force the type to be serialized as
|
||||
a JSON object instead.</p>
|
||||
<p>
|
||||
Note that if TypeNameHandling or PreserveReferencesHandling has been enabled for
|
||||
JSON arrays on the serializer then JSON arrays are wrapped it a containing object.
|
||||
The object will have the type name/reference properties and a $values property which
|
||||
will have the collection data.</p>
|
||||
<p>
|
||||
When deserializing if a member is typed as the interface IList<T> then it
|
||||
will be deserialized as a List<T>.</p>
|
||||
<p>
|
||||
Read more about serializing collections here: <a href="SerializingCollections.html">
|
||||
Serializing Collections </a>
|
||||
</p>
|
||||
<h4>
|
||||
<strong>Dictionarys and Hashtables</strong></h4>
|
||||
<p>
|
||||
.NET dictionaries (types that inherit from IDictionary) are converted to JSON objects.
|
||||
Note that only the dictionary name/values will be written to the JSON object when
|
||||
serializing and properties on the JSON object will be added to the dictionary's
|
||||
name/values when deserializing. Additional members on the .NET dictionary are ignored
|
||||
during serialization.</p>
|
||||
<p>
|
||||
When deserializing if a member is typed as the interface IDictionary<TKey, TValue>
|
||||
then it will be deserialized as a Dictionary<TKey, TValue>.</p>
|
||||
<p>
|
||||
Read more about serializing collections here: <a href="SerializingCollections.html">
|
||||
Serializing Collections</a></p>
|
||||
<h4>
|
||||
<strong>Untyped Objects</strong></h4>
|
||||
<p>
|
||||
.NET properties on a class that don't specify a type (i.e. they are just object)
|
||||
are serialized as usual. When untyped properties are deserialized the serializer
|
||||
has no way to know what type to create (unless type name handling is enabled and
|
||||
the JSON contains the type names).</p>
|
||||
<p>
|
||||
For these untyped properties the Json.NET serializer will read the JSON into LINQ
|
||||
to JSON objects and set them to the property. JObject will be created for JSON objects,
|
||||
JArray will be created for JSON arrays and JValue for primitive JSON values.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>Dynamic</strong></h4>
|
||||
<p>
|
||||
There are two different usages of dynamic (introduced in .NET 4) in .NET. The first
|
||||
are .NET properties with a type of dynamic. Dynamic proeprties behave like properties
|
||||
declared as object, any value can be assigned to it, but the difference being that
|
||||
properties and methods can be called on a dynamic property without casting. In Json.NET
|
||||
dynamic properties are serialized and deserialized exactly the same as untyped objects:
|
||||
because dynamic isn't an actual type Json.NET falls back to deserializing the JSON
|
||||
as LINQ to JSON objects.
|
||||
</p>
|
||||
<p>
|
||||
The second usage of dynamic in .NET are classes that implement <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.idynamicmetaobjectprovider.aspx"
|
||||
target="_blank">IDynamicMetaObjectProvider</a>. This interface lets the implementor
|
||||
create dynamic objects that intercept the property and method calls on an object
|
||||
and use them. <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx"
|
||||
target="_blank">ExpandoObject</a> is a good example of a dynamic object.
|
||||
</p>
|
||||
<p>
|
||||
Dynamic objects are serialized as JSON objects. A property is written for every
|
||||
member name returned by <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicmetaobject.getdynamicmembernames.aspx"
|
||||
target="_blank">DynamicMetaObject.GetDynamicMemberNames</a>.</p>
|
||||
<p>
|
||||
When deserializing dynamic objects the serializer first attempts to set JSON property
|
||||
values on a normal .NET member with the matching name. If no .NET member is found
|
||||
with the property name then the serializer will call SetMember on the dynamic object.
|
||||
Because there is no type information for dynamic members on a dynamic object the
|
||||
values assigned to them will be LINQ to JSON objects</p>
|
||||
<h4>
|
||||
<strong>ISerializable</strong></h4>
|
||||
<p>
|
||||
Types that implement ISerializable are serialized as JSON objects. When serializing
|
||||
only the values returned from ISerializable.GetObjectData are used; members on the
|
||||
type are ignored. When deserializing the constructor with a SerializationInfo and
|
||||
StreamingContext is called, passing the JSON object's values.</p>
|
||||
<p>
|
||||
In situations where this behavior is not wanted the JsonObjectAttribute can be placed
|
||||
on a .NET type that implements ISerializable to force it to be serialized as a normal
|
||||
JSON object.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>LINQ to JSON</strong></h4>
|
||||
<p>
|
||||
LINQ to JSON types (e.g. JObject, JArray) are automatically serialized and deserialized
|
||||
to their equivalent JSON when encountered by the Json.NET serializer.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>JsonConverter</strong></h4>
|
||||
<p>
|
||||
Serialization of values that are convertible by a JsonConverter (i.e. CanConvert
|
||||
returns true for its type) is completely overridden by the JsonConverter. The test
|
||||
to see whether a value can be converted by a JsonSerializer takes precedence over
|
||||
all other tests.</p>
|
||||
<p>
|
||||
JsonConverters can be defined and specified in a number of places: in an attribute
|
||||
on a member, in an attribute on a class and added to the JsonSerializer's converters
|
||||
collection. The priority of which JsonConverter is used is the JsonConverter defined
|
||||
by attribute on a member then the JsonConverter defined by an attribute on a class
|
||||
and finally any converters passed to the JsonSerializer.</p>
|
||||
<div id="footer">
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Serialization Guide</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
<style type="text/css">
|
||||
.style1
|
||||
{
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization Guide</span></div>
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"></span>
|
||||
<p>
|
||||
The Json.NET serializer can serialize a wide variety of .NET objects. This guide
|
||||
looks at how it works at a high level and in more detail.</p>
|
||||
<h3>
|
||||
Summary</h3>
|
||||
<p>
|
||||
At a high level, the Json.NET serializer will convert primitive .NET values into
|
||||
primitive JSON values, .NET arrays and collections to JSON arrays and everything
|
||||
else to JSON objects.</p>
|
||||
<p>
|
||||
Json.NET will throw an error if it encounters incorrect JSON when deserializing
|
||||
a value. For example if the serializer encounters a JSON property with an array
|
||||
of values and the type of matching .NET property is not a collection then an error
|
||||
will be thrown, and vice-versa.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>Complex Types</strong></h4>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
.NET
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
JSON
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>IList, IEnumerable, IList<T>, Array</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Array
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>IDictionary, IDictionary<TKey, TValue></b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Object
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Object (more detail below)</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Object
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<h4>
|
||||
<strong>Primitive Types</strong></h4>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
.NET
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
JSON
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>String</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Byte<br />
|
||||
SByte<br />
|
||||
UInt16<br />
|
||||
Int16<br />
|
||||
UInt32<br />
|
||||
Int32<br />
|
||||
UInt64<br />
|
||||
Int64<br />
|
||||
<br />
|
||||
</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Integer
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Float<br />
|
||||
Double<br />
|
||||
Decimal<br />
|
||||
<br />
|
||||
</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Float
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Enum</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Integer (can be string with <a href="html/T_Newtonsoft_Json_Converters_StringEnumConverter.htm">
|
||||
StringEnumConverter</a>)
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>DateTime</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String (<a href="DatesInJSON.html">Serializing Dates in JSON</a>)
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Byte[]</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String (base 64 encoded)
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Type</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String (type name)
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Guid</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b><a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx"
|
||||
target="_blank">TypeConverter</a> (convertible to String)</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
String
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<h3>
|
||||
Breakdown of Type Serialization</h3>
|
||||
<h4>
|
||||
<strong>Objects</strong></h4>
|
||||
<p>
|
||||
.NET types that don't fall into any other category listed below (i.e. aren't lists,
|
||||
dictionaries, dynamic, implement ISerializable, etc) are serialized as JSON objects.</p>
|
||||
<p>
|
||||
By default types are serialized in opt-out mode. What that means is all public fields
|
||||
and properties with getters are automatically serialized to JSON and members that
|
||||
shouldn't be serialized are opted-out using the JsonIgnoreAttribute. To serialize
|
||||
private members the JsonPropertyAttribute can be placed on private fields and properties
|
||||
or the DefaultMembersSearchFlags can be changed on DefaultContractResolver to change
|
||||
how members are serialized on all types.</p>
|
||||
<p>
|
||||
Types can also be serialized using opt-in mode. Only fields that have a JsonPropertyAttribute
|
||||
or DataMemberAttribute on them will be serialized. Opt-in mode for an object is
|
||||
specified using the JsonObjectAttribute on the class.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>IEnumerable, Lists and Arrays</strong></h4>
|
||||
<p>
|
||||
.NET lists (types that inherit from IEnumerable) and .NET arrays are converted to
|
||||
JSON arrays. Because JSON arrays only support a range of values and not properties,
|
||||
any additional properties and fields declared on .NET collections are not serialized.
|
||||
In situations where a JSON array is not wanted the JsonObjectAttribute can be placed
|
||||
on a .NET type that implements IEnumerable to force the type to be serialized as
|
||||
a JSON object instead.</p>
|
||||
<p>
|
||||
Note that if TypeNameHandling or PreserveReferencesHandling has been enabled for
|
||||
JSON arrays on the serializer then JSON arrays are wrapped it a containing object.
|
||||
The object will have the type name/reference properties and a $values property which
|
||||
will have the collection data.</p>
|
||||
<p>
|
||||
When deserializing if a member is typed as the interface IList<T> then it
|
||||
will be deserialized as a List<T>.</p>
|
||||
<p>
|
||||
Read more about serializing collections here: <a href="SerializingCollections.html">
|
||||
Serializing Collections </a>
|
||||
</p>
|
||||
<h4>
|
||||
<strong>Dictionarys and Hashtables</strong></h4>
|
||||
<p>
|
||||
.NET dictionaries (types that inherit from IDictionary) are converted to JSON objects.
|
||||
Note that only the dictionary name/values will be written to the JSON object when
|
||||
serializing and properties on the JSON object will be added to the dictionary's
|
||||
name/values when deserializing. Additional members on the .NET dictionary are ignored
|
||||
during serialization.</p>
|
||||
<p>
|
||||
When deserializing if a member is typed as the interface IDictionary<TKey, TValue>
|
||||
then it will be deserialized as a Dictionary<TKey, TValue>.</p>
|
||||
<p>
|
||||
Read more about serializing collections here: <a href="SerializingCollections.html">
|
||||
Serializing Collections</a></p>
|
||||
<h4>
|
||||
<strong>Untyped Objects</strong></h4>
|
||||
<p>
|
||||
.NET properties on a class that don't specify a type (i.e. they are just object)
|
||||
are serialized as usual. When untyped properties are deserialized the serializer
|
||||
has no way to know what type to create (unless type name handling is enabled and
|
||||
the JSON contains the type names).</p>
|
||||
<p>
|
||||
For these untyped properties the Json.NET serializer will read the JSON into LINQ
|
||||
to JSON objects and set them to the property. JObject will be created for JSON objects,
|
||||
JArray will be created for JSON arrays and JValue for primitive JSON values.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>Dynamic</strong></h4>
|
||||
<p>
|
||||
There are two different usages of dynamic (introduced in .NET 4) in .NET. The first
|
||||
are .NET properties with a type of dynamic. Dynamic proeprties behave like properties
|
||||
declared as object, any value can be assigned to it, but the difference being that
|
||||
properties and methods can be called on a dynamic property without casting. In Json.NET
|
||||
dynamic properties are serialized and deserialized exactly the same as untyped objects:
|
||||
because dynamic isn't an actual type Json.NET falls back to deserializing the JSON
|
||||
as LINQ to JSON objects.
|
||||
</p>
|
||||
<p>
|
||||
The second usage of dynamic in .NET are classes that implement <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.idynamicmetaobjectprovider.aspx"
|
||||
target="_blank">IDynamicMetaObjectProvider</a>. This interface lets the implementor
|
||||
create dynamic objects that intercept the property and method calls on an object
|
||||
and use them. <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx"
|
||||
target="_blank">ExpandoObject</a> is a good example of a dynamic object.
|
||||
</p>
|
||||
<p>
|
||||
Dynamic objects are serialized as JSON objects. A property is written for every
|
||||
member name returned by <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicmetaobject.getdynamicmembernames.aspx"
|
||||
target="_blank">DynamicMetaObject.GetDynamicMemberNames</a>.</p>
|
||||
<p>
|
||||
When deserializing dynamic objects the serializer first attempts to set JSON property
|
||||
values on a normal .NET member with the matching name. If no .NET member is found
|
||||
with the property name then the serializer will call SetMember on the dynamic object.
|
||||
Because there is no type information for dynamic members on a dynamic object the
|
||||
values assigned to them will be LINQ to JSON objects</p>
|
||||
<h4>
|
||||
<strong>ISerializable</strong></h4>
|
||||
<p>
|
||||
Types that implement ISerializable are serialized as JSON objects. When serializing
|
||||
only the values returned from ISerializable.GetObjectData are used; members on the
|
||||
type are ignored. When deserializing the constructor with a SerializationInfo and
|
||||
StreamingContext is called, passing the JSON object's values.</p>
|
||||
<p>
|
||||
In situations where this behavior is not wanted the JsonObjectAttribute can be placed
|
||||
on a .NET type that implements ISerializable to force it to be serialized as a normal
|
||||
JSON object.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>LINQ to JSON</strong></h4>
|
||||
<p>
|
||||
LINQ to JSON types (e.g. JObject, JArray) are automatically serialized and deserialized
|
||||
to their equivalent JSON when encountered by the Json.NET serializer.
|
||||
</p>
|
||||
<h4>
|
||||
<strong>JsonConverter</strong></h4>
|
||||
<p>
|
||||
Serialization of values that are convertible by a JsonConverter (i.e. CanConvert
|
||||
returns true for its type) is completely overridden by the JsonConverter. The test
|
||||
to see whether a value can be converted by a JsonSerializer takes precedence over
|
||||
all other tests.</p>
|
||||
<p>
|
||||
JsonConverters can be defined and specified in a number of places: in an attribute
|
||||
on a member, in an attribute on a class and added to the JsonSerializer's converters
|
||||
collection. The priority of which JsonConverter is used is the JsonConverter defined
|
||||
by attribute on a member then the JsonConverter defined by an attribute on a class
|
||||
and finally any converters passed to the JsonSerializer.</p>
|
||||
<div id="footer">
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+512
-512
File diff suppressed because it is too large
Load Diff
@@ -1,30 +1,30 @@
|
||||
<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">
|
||||
|
||||
<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;"> {</pre>
|
||||
@@ -59,14 +59,14 @@
|
||||
<pre style="margin: 0px;"><span style="color: green;">// }</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></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;"> {</span></pre>
|
||||
@@ -93,15 +93,15 @@
|
||||
<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></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;"> </pre>
|
||||
@@ -113,16 +113,16 @@
|
||||
<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>
|
||||
|
||||
|
||||
</div></div>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+102
-102
@@ -1,103 +1,103 @@
|
||||
<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;"> </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;"> </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;">// "Name": "Apple",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Expiry": "\/Date(1230375600000+1300)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Price": 3.99,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Sizes": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Small",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Medium",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Large"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> deserializedProduct = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">Product</span>>(output);</pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<p>
|
||||
SerializeObject and DeserializeObject both have overloads that take a
|
||||
<a href="html/T_Newtonsoft_Json_JsonSerializerSettings.htm">JsonSerializerSettings</a> object. JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.
|
||||
</p>
|
||||
|
||||
<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;"> </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;"> </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;"> serializer.Serialize(writer, product);</pre>
|
||||
<pre style="margin: 0px;"> <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>
|
||||
<p>Read more about the available JsonSerializer settings here:
|
||||
<a href="SerializationSettings.html">Serialization Settings</a></p>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
<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;"> </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;"> </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;">// "Name": "Apple",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Expiry": "\/Date(1230375600000+1300)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Price": 3.99,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Sizes": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Small",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Medium",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Large"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> deserializedProduct = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">Product</span>>(output);</pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<p>
|
||||
SerializeObject and DeserializeObject both have overloads that take a
|
||||
<a href="html/T_Newtonsoft_Json_JsonSerializerSettings.htm">JsonSerializerSettings</a> object. JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.
|
||||
</p>
|
||||
|
||||
<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;"> </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;"> </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;"> serializer.Serialize(writer, product);</pre>
|
||||
<pre style="margin: 0px;"> <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>
|
||||
<p>Read more about the available JsonSerializer settings here:
|
||||
<a href="SerializationSettings.html">Serialization Settings</a></p>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+124
-124
@@ -1,125 +1,125 @@
|
||||
<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;"> <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;"> <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;"> <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;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseData"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""results"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""GsearchResultClass"": ""GwebSearch"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""unescapedUrl"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""visibleUrl"": ""en.wikipedia.org"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cacheUrl"": ""http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""title"": ""<b>Paris Hilton</b> - Wikipedia, the free encyclopedia"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""titleNoFormatting"": ""Paris Hilton - Wikipedia, the free encyclopedia"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""content"": ""[1] In 2006, she released her debut album...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""GsearchResultClass"": ""GwebSearch"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""unescapedUrl"": ""http://www.imdb.com/name/nm0385296/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.imdb.com/name/nm0385296/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""visibleUrl"": ""www.imdb.com"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cacheUrl"": ""http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""title"": ""<b>Paris Hilton</b>"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""titleNoFormatting"": ""Paris Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""content"": ""Self: Zoolander. Socialite <b>Paris Hilton</b>...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cursor"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""pages"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""0"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 1</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""4"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 2</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""8"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 3</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""12"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 4</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""estimatedResultCount"": ""59600000"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""currentPageIndex"": 0,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""moreResultsUrl"": ""http://www.google.com/search?oe=utf8&ie=utf8...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseDetails"": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseStatus"": 200</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </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><<span style="color: #2b91af;">JToken</span>> results = googleSearch[<span style="color: #a31515;">"responseData"</span>][<span style="color: #a31515;">"results"</span>].Children().ToList();</pre>
|
||||
<pre style="margin: 0px;"> </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><<span style="color: #2b91af;">SearchResult</span>> searchResults = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">SearchResult</span>>();</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;"> <span style="color: #2b91af;">SearchResult</span> searchResult = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">SearchResult</span>>(result.ToString());</pre>
|
||||
<pre style="margin: 0px;"> searchResults.Add(searchResult);</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Title = <b>Paris Hilton</b> - 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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Title = <b>Paris Hilton</b></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...</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>
|
||||
|
||||
<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;"> <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;"> <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;"> <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;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseData"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""results"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""GsearchResultClass"": ""GwebSearch"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""unescapedUrl"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""visibleUrl"": ""en.wikipedia.org"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cacheUrl"": ""http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""title"": ""<b>Paris Hilton</b> - Wikipedia, the free encyclopedia"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""titleNoFormatting"": ""Paris Hilton - Wikipedia, the free encyclopedia"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""content"": ""[1] In 2006, she released her debut album...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""GsearchResultClass"": ""GwebSearch"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""unescapedUrl"": ""http://www.imdb.com/name/nm0385296/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.imdb.com/name/nm0385296/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""visibleUrl"": ""www.imdb.com"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cacheUrl"": ""http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""title"": ""<b>Paris Hilton</b>"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""titleNoFormatting"": ""Paris Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""content"": ""Self: Zoolander. Socialite <b>Paris Hilton</b>...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cursor"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""pages"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""0"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 1</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""4"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 2</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""8"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 3</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""12"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 4</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""estimatedResultCount"": ""59600000"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""currentPageIndex"": 0,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""moreResultsUrl"": ""http://www.google.com/search?oe=utf8&ie=utf8...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseDetails"": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseStatus"": 200</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </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;"> </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><<span style="color: #2b91af;">JToken</span>> results = googleSearch[<span style="color: #a31515;">"responseData"</span>][<span style="color: #a31515;">"results"</span>].Children().ToList();</pre>
|
||||
<pre style="margin: 0px;"> </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><<span style="color: #2b91af;">SearchResult</span>> searchResults = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">SearchResult</span>>();</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;"> <span style="color: #2b91af;">SearchResult</span> searchResult = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">SearchResult</span>>(result.ToString());</pre>
|
||||
<pre style="margin: 0px;"> searchResults.Add(searchResult);</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Title = <b>Paris Hilton</b> - 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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Title = <b>Paris Hilton</b></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...</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>
|
||||
+18
-18
@@ -1,19 +1,19 @@
|
||||
div#content {
|
||||
padding: 0 10px 10px 10px;
|
||||
}
|
||||
|
||||
.overflowpanel
|
||||
{
|
||||
width: 98%;
|
||||
border: solid 1px #ccc;
|
||||
overflow: auto;
|
||||
overflow-y: hidden;
|
||||
background-color: #fcfcfc;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.overflowpanel .code
|
||||
{
|
||||
padding: 10px 4px 20px 10px;
|
||||
display: block;
|
||||
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;
|
||||
}
|
||||
+89
-89
@@ -1,90 +1,90 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
|
||||
<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.9.3.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>..\Working\Documentation\</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>HtmlHelp1, MSHelpViewer, Website</HelpFileFormat>
|
||||
<FrameworkVersion>.NET 3.5</FrameworkVersion>
|
||||
<HelpTitle>Json.NET - Quick Starts &amp%3b API Documentation</HelpTitle>
|
||||
<PresentationStyle>Prototype</PresentationStyle>
|
||||
<NamingMethod>MemberName</NamingMethod>
|
||||
<DocumentationSourcePath>..\Src\Newtonsoft.Json\bin\Release</DocumentationSourcePath>
|
||||
<DocumentationSources>
|
||||
<DocumentationSource sourceFile="$(DocumentationSourcePath)\Newtonsoft.Json.dll" />
|
||||
<DocumentationSource sourceFile="$(DocumentationSourcePath)\Newtonsoft.Json.xml" />
|
||||
</DocumentationSources>
|
||||
<NamespaceSummaries>
|
||||
<NamespaceSummaryItem name="(global)" isDocumented="False" />
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json" isDocumented="True">The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True">The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Linq.ComponentModel" isDocumented="True">The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True">The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> 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 <b>Newtonsoft.Json.Linq</b> 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="SerializationSettings.html" />
|
||||
<Content Include="SerializationGuide.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" />
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
|
||||
<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.9.3.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>..\Working\Documentation\</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>HtmlHelp1, MSHelpViewer, Website</HelpFileFormat>
|
||||
<FrameworkVersion>.NET 3.5</FrameworkVersion>
|
||||
<HelpTitle>Json.NET - Quick Starts &amp%3b API Documentation</HelpTitle>
|
||||
<PresentationStyle>Prototype</PresentationStyle>
|
||||
<NamingMethod>MemberName</NamingMethod>
|
||||
<DocumentationSourcePath>..\Src\Newtonsoft.Json\bin\Release</DocumentationSourcePath>
|
||||
<DocumentationSources>
|
||||
<DocumentationSource sourceFile="$(DocumentationSourcePath)\Newtonsoft.Json.dll" />
|
||||
<DocumentationSource sourceFile="$(DocumentationSourcePath)\Newtonsoft.Json.xml" />
|
||||
</DocumentationSources>
|
||||
<NamespaceSummaries>
|
||||
<NamespaceSummaryItem name="(global)" isDocumented="False" />
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json" isDocumented="True">The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True">The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Linq.ComponentModel" isDocumented="True">The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True">The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> 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 <b>Newtonsoft.Json.Linq</b> 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="SerializationSettings.html" />
|
||||
<Content Include="SerializationGuide.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>
|
||||
+22
-22
@@ -1,23 +1,23 @@
|
||||
<?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="Serialization Settings" url="SerializationSettings.html" />
|
||||
<siteMapNode title="Serialization Guide" url="SerializationGuide.html" />
|
||||
<siteMapNode title="Serialization Attributes" url="SerializationAttributes.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="Serializing Collections" url="SerializingCollections.html" />
|
||||
<siteMapNode title="Serializing Dates in JSON" url="DatesInJSON.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" />
|
||||
<?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="Serialization Settings" url="SerializationSettings.html" />
|
||||
<siteMapNode title="Serialization Guide" url="SerializationGuide.html" />
|
||||
<siteMapNode title="Serialization Attributes" url="SerializationAttributes.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="Serializing Collections" url="SerializingCollections.html" />
|
||||
<siteMapNode title="Serializing Dates in JSON" url="DatesInJSON.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>
|
||||
+58
-58
@@ -1,59 +1,59 @@
|
||||
Json.NET
|
||||
|
||||
http://james.newtonking.com/projects/json-net.aspx
|
||||
http://www.codeplex.com/json/
|
||||
|
||||
|
||||
Description:
|
||||
|
||||
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.
|
||||
|
||||
-Flexible JSON serializer to convert .NET objects to JSON and back again
|
||||
-LINQ to JSON for reading and writing JSON
|
||||
-Writes indented, easy to read JSON
|
||||
-Convert JSON to and from XML
|
||||
-Supports Silverlight and Windows Phone
|
||||
|
||||
|
||||
|
||||
Versions:
|
||||
|
||||
Json.NET comes in different versions for the various .NET frameworks.
|
||||
|
||||
-DotNet:
|
||||
.NET latest (4.0)
|
||||
|
||||
-DotNet35:
|
||||
.NET 3.5 SP1, Mono
|
||||
|
||||
-DotNet20:
|
||||
.NET 2.0
|
||||
|
||||
-Silverlight:
|
||||
Silverlight 4.0
|
||||
|
||||
-WindowsPhone:
|
||||
Windows Phone 7
|
||||
|
||||
Microsoft stopped support for the Compact Framework in Visual Studio 2010.
|
||||
For a Compact Framework 3.5 build download Json.NET 3.5.
|
||||
|
||||
For a Silverlight 3.0 build down download Json.NET 3.5.
|
||||
|
||||
|
||||
Instructions:
|
||||
|
||||
1. Extract Newtonsoft.Json.dll and Newtonsoft.Json.xml from the archive's /bin directory into your own applications.
|
||||
2. Add a reference to Newtonsoft.Json.dll within Visual Studio.NET to your project.
|
||||
|
||||
|
||||
|
||||
License:
|
||||
|
||||
Copyright (c) 2007 James Newton-King
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
Json.NET
|
||||
|
||||
http://james.newtonking.com/projects/json-net.aspx
|
||||
http://www.codeplex.com/json/
|
||||
|
||||
|
||||
Description:
|
||||
|
||||
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.
|
||||
|
||||
-Flexible JSON serializer to convert .NET objects to JSON and back again
|
||||
-LINQ to JSON for reading and writing JSON
|
||||
-Writes indented, easy to read JSON
|
||||
-Convert JSON to and from XML
|
||||
-Supports Silverlight and Windows Phone
|
||||
|
||||
|
||||
|
||||
Versions:
|
||||
|
||||
Json.NET comes in different versions for the various .NET frameworks.
|
||||
|
||||
-DotNet:
|
||||
.NET latest (4.0)
|
||||
|
||||
-DotNet35:
|
||||
.NET 3.5 SP1, Mono
|
||||
|
||||
-DotNet20:
|
||||
.NET 2.0
|
||||
|
||||
-Silverlight:
|
||||
Silverlight 4.0
|
||||
|
||||
-WindowsPhone:
|
||||
Windows Phone 7
|
||||
|
||||
Microsoft stopped support for the Compact Framework in Visual Studio 2010.
|
||||
For a Compact Framework 3.5 build download Json.NET 3.5.
|
||||
|
||||
For a Silverlight 3.0 build down download Json.NET 3.5.
|
||||
|
||||
|
||||
Instructions:
|
||||
|
||||
1. Extract Newtonsoft.Json.dll and Newtonsoft.Json.xml from the archive's /bin directory into your own applications.
|
||||
2. Add a reference to Newtonsoft.Json.dll within Visual Studio.NET to your project.
|
||||
|
||||
|
||||
|
||||
License:
|
||||
|
||||
Copyright (c) 2007 James Newton-King
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+439
-439
@@ -1,439 +1,439 @@
|
||||
/* page style */
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
background-color: #FFFFFF;
|
||||
padding: 0;
|
||||
font-size: 8.5pt;
|
||||
font-family: verdana, arial, sans-serif;
|
||||
color: #000000;
|
||||
}
|
||||
|
||||
html>body {
|
||||
margin: 0;
|
||||
background-color: #FFFFFF;
|
||||
padding: 0;
|
||||
font-size: 8.5pt;
|
||||
font-family: verdana, arial, sans-serif;
|
||||
color: #000000;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
table {
|
||||
/* this is a trick to force tables to inherit the body font size */
|
||||
font-size: 100%;
|
||||
}
|
||||
|
||||
/* non-scrolling (control) region style */
|
||||
|
||||
div#control {
|
||||
margin: 0;
|
||||
background-color: #D4DFFF;
|
||||
padding: 4px;
|
||||
width: 100%;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
z-index: 2;
|
||||
}
|
||||
|
||||
span.productTitle {
|
||||
font-size: 80%;
|
||||
}
|
||||
|
||||
span.topicTitle {
|
||||
font-size: 140%;
|
||||
font-weight: bold;
|
||||
color: #003399;
|
||||
}
|
||||
|
||||
span#chickenFeet {
|
||||
float: left;
|
||||
}
|
||||
|
||||
span#languageFilter {
|
||||
float: right;
|
||||
height: 1px;
|
||||
max-height: 1px;
|
||||
vertical-align: bottom;
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
/* scrolling (content) region style */
|
||||
|
||||
div#main
|
||||
{
|
||||
clear: both;
|
||||
margin: 0;
|
||||
padding: 1em;
|
||||
width: 100%;
|
||||
z-index: 1;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
/* sections */
|
||||
|
||||
div#header {
|
||||
font-size: 80%;
|
||||
color: #666666;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
|
||||
div.summary {
|
||||
margin-top: 0em;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
div.section {
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
div.sectionTitle {
|
||||
display: inline;
|
||||
font-size: 120%;
|
||||
font-weight: bold;
|
||||
color: #003399;
|
||||
}
|
||||
|
||||
div.sectionContent {
|
||||
margin-top: 0.2em;
|
||||
}
|
||||
|
||||
span.subsectionTitle {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
div#footer {
|
||||
margin-top: 1em;
|
||||
border-top: thin solid #003399;
|
||||
padding-top: 0.5em;
|
||||
}
|
||||
|
||||
div#footer p {
|
||||
margin-top: 0.2em;
|
||||
margin-bottom: 0.2em;
|
||||
}
|
||||
|
||||
/* authored content (block) */
|
||||
|
||||
p {
|
||||
margin-top: 1em;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
dl, ul, ol {
|
||||
margin-top: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
|
||||
pre {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: "Andale Mono", "Courier New", Courier, monospace;
|
||||
}
|
||||
|
||||
table.authoredTable {
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
table.authoredTable th {
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.authoredTable td {
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-width: 1px;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
div.alert {
|
||||
border: 1px solid #C8CDDE;
|
||||
background: #F7F7FF;
|
||||
}
|
||||
|
||||
div.media {
|
||||
text-align: center;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
|
||||
/* authored content (inline) */
|
||||
|
||||
span.keyword {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
span.code {
|
||||
font-family: "Andale Mono", "Courier New", Courier, monospace;
|
||||
font-size: 110%;
|
||||
color: #000066;
|
||||
}
|
||||
|
||||
/* auto-generated controls */
|
||||
|
||||
div.langTabs {
|
||||
/*width: 100%;*/
|
||||
}
|
||||
|
||||
div.langTab {
|
||||
float: left;
|
||||
width: 15%;
|
||||
border-top: 1px solid #C8CDDE;
|
||||
border-left: 1px solid #C8CDDE;
|
||||
border-right: 1px solid #C8CDDE;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
div.activeLangTab {
|
||||
float: left;
|
||||
width: 15%;
|
||||
border-top: 1px solid #C8CDDE;
|
||||
border-left: 1px solid #C8CDDE;
|
||||
border-right: 1px solid #C8CDDE;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.members {
|
||||
/* table-layout: fixed; */
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
table.members th.iconColumn {
|
||||
width: 60px;
|
||||
}
|
||||
|
||||
table.members th.nameColumn {
|
||||
width: 40%;
|
||||
}
|
||||
|
||||
table.members th.descriptionColumn {
|
||||
width: 60%;
|
||||
}
|
||||
|
||||
table.members th {
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.members td {
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-width: 1px;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
vertical-align: top;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
table.exceptions {
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
|
||||
table.exceptions th.exceptionNameColumn {
|
||||
width: 40%;
|
||||
}
|
||||
|
||||
table.exceptions th.exceptionConditionColumn {
|
||||
width: 60%;
|
||||
}
|
||||
|
||||
table.exceptions th {
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.exceptions td {
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-width: 1px;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
table.permissions {
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
|
||||
table.permissions th.permissionNameColumn {
|
||||
width: 40%;
|
||||
}
|
||||
|
||||
table.permissions th.permissionConditionColumn {
|
||||
width: 60%;
|
||||
}
|
||||
|
||||
table.permissions th {
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.permissions td {
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-width: 1px;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
span.obsolete {
|
||||
color: red;
|
||||
}
|
||||
|
||||
span.cs {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
span.vb {
|
||||
display: none;
|
||||
}
|
||||
|
||||
span.cpp {
|
||||
display: none;
|
||||
}
|
||||
|
||||
span.nu
|
||||
{
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* syntax styling */
|
||||
|
||||
div.code span.identifier {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
div.code span.keyword {
|
||||
color: green;
|
||||
}
|
||||
|
||||
div.code span.parameter {
|
||||
font-style: italic;
|
||||
color: purple;
|
||||
}
|
||||
|
||||
div.code span.literal {
|
||||
color: purple;
|
||||
}
|
||||
|
||||
div.code span.comment {
|
||||
color: red;
|
||||
}
|
||||
|
||||
span.foreignPhrase {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
span.placeholder {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
span.parameter
|
||||
{
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
span.typeparameter
|
||||
{
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
a {
|
||||
color: blue;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
MSHelp\:link {
|
||||
color: blue;
|
||||
hoverColor: #3366ff;
|
||||
}
|
||||
|
||||
span.nolink {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
span.selflink {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.filter {
|
||||
table-layout: fixed;
|
||||
}
|
||||
|
||||
tr.tabs td.tab {
|
||||
width: 10em;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: normal;
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
tr.tabs td.activeTab {
|
||||
width: 10em;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
td.line {
|
||||
background: #EFEFF7;
|
||||
}
|
||||
|
||||
dt {
|
||||
font-weight: bold;
|
||||
margin-top: 10px;
|
||||
margin-left: 10px;
|
||||
}
|
||||
/* 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;
|
||||
}
|
||||
|
||||
+31
-31
@@ -1,32 +1,32 @@
|
||||
Versions:
|
||||
|
||||
Json.NET comes in different versions for the various .NET frameworks.
|
||||
|
||||
-Net40:
|
||||
.NET latest (4.0 & 4.5)
|
||||
|
||||
-Net35:
|
||||
.NET 3.5 SP1, Mono
|
||||
|
||||
-Net20:
|
||||
.NET 2.0
|
||||
|
||||
-Silverlight:
|
||||
Silverlight 4 & 5
|
||||
|
||||
-WindowsPhone:
|
||||
Windows Phone 7 & 7.1
|
||||
|
||||
-Metro
|
||||
Windows 8 Metro, WinRT 4.5
|
||||
|
||||
|
||||
Notes:
|
||||
|
||||
The Silverlight and Windows Phone builds are not strong-named.
|
||||
Use the assemblies in the Signed sub-directories for the strong-named versions.
|
||||
|
||||
Microsoft stopped support for the Compact Framework in Visual Studio 2010.
|
||||
For a Compact Framework 3.5 build download Json.NET 3.5.
|
||||
|
||||
Versions:
|
||||
|
||||
Json.NET comes in different versions for the various .NET frameworks.
|
||||
|
||||
-Net40:
|
||||
.NET latest (4.0 & 4.5)
|
||||
|
||||
-Net35:
|
||||
.NET 3.5 SP1, Mono
|
||||
|
||||
-Net20:
|
||||
.NET 2.0
|
||||
|
||||
-Silverlight:
|
||||
Silverlight 4 & 5
|
||||
|
||||
-WindowsPhone:
|
||||
Windows Phone 7 & 7.1
|
||||
|
||||
-Metro
|
||||
Windows 8 Metro, WinRT 4.5
|
||||
|
||||
|
||||
Notes:
|
||||
|
||||
The Silverlight and Windows Phone builds are not strong-named.
|
||||
Use the assemblies in the Signed sub-directories for the strong-named versions.
|
||||
|
||||
Microsoft stopped support for the Compact Framework in Visual Studio 2010.
|
||||
For a Compact Framework 3.5 build download Json.NET 3.5.
|
||||
|
||||
For a Silverlight 3.0 build download Json.NET 3.5.
|
||||
Reference in New Issue
Block a user