-Fixed missing XML documentation
-Updated custom documentation pages to use MAML
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="ContractResolver" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<para>The IContractResolver interface provides a way to customize how the
|
||||
JsonSerializer serializes and deserializes .NET objects to JSON.</para>
|
||||
<para>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.</para>
|
||||
</introduction>
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>DefaultContractResolver</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The <codeEntityReference>T:Newtonsoft.Json.Serialization.DefaultContractResolver</codeEntityReference>
|
||||
is the default resolver used by the
|
||||
serializer. It provides many avenues of extensibility in the form of
|
||||
virtual methods that can be overriden.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>CamelCasePropertyNamesContractResolver</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para><codeEntityReference>T:Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver</codeEntityReference>
|
||||
inherits from DefaultContractResolver and simply overrides the JSON
|
||||
property name to be written in <externalLink>
|
||||
<linkText>camelcase</linkText>
|
||||
<linkUri>http://en.wikipedia.org/wiki/CamelCase</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink>.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ContractResolver" title="ContractResolver" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,92 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Contract Resolver</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Contract Resolvers</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
|
||||
<p>The <a href="./html/T_Newtonsoft_Json_Serialization_IContractResolver.htm">IContractResolver</a> interface provides a way to customize how the JsonSerializer serializes and deserializes .NET objects to JSON.</p>
|
||||
<p>Implementing the IContractResolver interface and then assigning an instance to a JsonSerializer lets you control
|
||||
whether the object is serialized as a JSON object or JSON array, what object members should be serialized, how they are serialized and what they are called.</p>
|
||||
|
||||
<h4>DefaultContractResolver</h4>
|
||||
<p>The <a href="./html/T_Newtonsoft_Json_Serialization_DefaultContractResolver.htm">DefaultContractResolver</a> is the default resolver used by the serializer. It provides many avenues of extensibility in the form of virtual methods that can be overriden.</p>
|
||||
|
||||
<h4>CamelCasePropertyNamesContractResolver</h4>
|
||||
<p><a href="./html/T_Newtonsoft_Json_Serialization_CamelCasePropertyNamesContractResolver.htm">CamelCasePropertyNamesContractResolver</a> inherits from DefaultContractResolver and simply overrides the JSON property name to be written in <a href="http://en.wikipedia.org/wiki/CamelCase" target="_blank">camelcase</a>.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code"> <div style="font-family: courier new; color: black; font-size: 10pt;"> <pre style="margin: 0px;"><span style="color: rgb(43, 145, 175);">Product</span> product = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Product</span></pre>
|
||||
|
||||
<pre style="margin: 0px;"> {</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>
|
||||
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="ConvertingJSONandXML" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>Json.NET supports converting JSON to XML and vice versa using the
|
||||
<codeEntityReference>T:Newtonsoft.Json.Converters.XmlNodeConverter</codeEntityReference>.</para>
|
||||
<para>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.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Conversion Rules</title>
|
||||
<content>
|
||||
|
||||
<list class="bullet">
|
||||
<listItem><para>Elements remain unchanged.</para></listItem>
|
||||
<listItem><para>Attributes are prefixed with an @ and should be at the start of the object.</para></listItem>
|
||||
<listItem><para>Single child text nodes are a value directly against an element, otherwise they are accessed via #text.</para></listItem>
|
||||
<listItem><para>The XML declaration and processing instructions are prefixed with ?.</para></listItem>
|
||||
<listItem><para>Charater data, comments, whitespace and significate whitespace nodes are accessed via
|
||||
#cdata-section, #comment, #whitespace and #significate-whitespace respectively. </para></listItem>
|
||||
<listItem><para>Multiple nodes with the same name at the same level are grouped together into an array.</para></listItem>
|
||||
<listItem><para>Empty elements are null.</para></listItem>
|
||||
</list>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>SerializeXmlNode</title>
|
||||
<content>
|
||||
|
||||
<para>The JsonConvert has two helper methods for converting between JSON and XML. The first is
|
||||
<codeEntityReference>Overload:Newtonsoft.Json.JsonConvert.SerializeXmlNode</codeEntityReference>.
|
||||
This method takes an XmlNode and serializes it to JSON text.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\ConvertingJsonAndXmlTests.cs" region="SerializeXmlNode" title="Converting XML to JSON with SerializeXmlNode" />
|
||||
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>DeserializeXmlNode</title>
|
||||
<content>
|
||||
|
||||
<para>The second helper method on JsonConvert is
|
||||
<codeEntityReference>Overload:Newtonsoft.Json.JsonConvert.DeserializeXmlNode</codeEntityReference>.
|
||||
This method takes JSON text and deserializes it into a XmlNode.</para>
|
||||
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\ConvertingJsonAndXmlTests.cs" region="DeserializeXmlNode" title="Converting JSON to XML with DeserializeXmlNode" />
|
||||
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,146 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Converting between JSON and XML</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Converting between JSON and XML</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
<p>Json.NET supports converting JSON to XML and vice versa using the
|
||||
<a href="./html/T_Newtonsoft_Json_Converters_XmlNodeConverter.htm">XmlNodeConverter</a>.</p>
|
||||
|
||||
<p>
|
||||
Elements, attributes, text, comments, character data, processing instructions,
|
||||
namespaces and the XML declaration are all preserved when converting between the
|
||||
two. The only caveat is that it is possible to lose the order of differently
|
||||
named nodes at the same level when they are grouped together into an array.</p>
|
||||
<h3>Conversion Rules</h3>
|
||||
<ul>
|
||||
<li>Elements remain unchanged. </li>
|
||||
<li>Attributes are prefixed with an @ 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>
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="CustomCreationConverter" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<para>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.</para>
|
||||
</introduction>
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>Example</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="CustomCreationConverterObject" title="CustomCreationConverter" />
|
||||
<para>This is an extremely simple example. A more complicated scenario
|
||||
could involve an object factory or service locator which resolves the
|
||||
object at runtime.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="CustomCreationConverterExample" title="CustomCreationConverter Example" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,99 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>CustomCreationConverter</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">CustomCreationConverter</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>
|
||||
The CustomCreationConverter is a JsonConverter that provides a way to customize how an object is created
|
||||
during JSON deserialization.
|
||||
Once the object has been created it will then have values populated onto it by the serializer.
|
||||
</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IPerson</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> FirstName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> LastName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <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;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Employee</span> : <span style="color: #2b91af;">IPerson</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> FirstName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> LastName { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <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: 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;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> JobTitle { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PersonConverter</span> : <span style="color: #2b91af;">CustomCreationConverter</span><<span style="color: #2b91af;">IPerson</span>></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: #2b91af;">IPerson</span> Create(<span style="color: #2b91af;">Type</span> objectType)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">Employee</span>();</pre>
|
||||
<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 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>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "FirstName": "Maurice",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LastName": "Moss",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BirthDate": "\/Date(252291661000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Department": "IT",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "JobTitle": "Support"</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;">// "FirstName": "Jen",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LastName": "Barber",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BirthDate": "\/Date(258771661000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Department": "IT",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "JobTitle": "Manager"</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;">List</span><<span style="color: #2b91af;">IPerson</span>> people = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">IPerson</span>>>(json, <span style="color: blue;">new</span> <span style="color: #2b91af;">PersonConverter</span>());</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">IPerson</span> person = people[0];</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(person.GetType());</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Newtonsoft.Json.Tests.Employee</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(person.FirstName);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Maurice</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Employee</span> employee = (<span style="color: #2b91af;">Employee</span>)person;</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(employee.JobTitle);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Support</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,93 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="DatesInJSON" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<para>DateTimes in JSON are hard.</para>
|
||||
<para>The problem comes from the <externalLink>
|
||||
<linkText>JSON spec</linkText>
|
||||
<linkUri>http://www.ietf.org/rfc/rfc4627.txt</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink> 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.</para>
|
||||
</introduction>
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>Dates and Json.NET</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The default format used by Json.NET is the <externalLink>
|
||||
<linkText>ISO 8601 standard</linkText>
|
||||
<linkUri>http://en.wikipedia.org/wiki/ISO_8601</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink>: 2012-03-19T07:22Z.</para>
|
||||
<para>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
|
||||
<codeEntityReference>T:Newtonsoft.Json.DateFormatHandling</codeEntityReference>
|
||||
setting to MicrosoftDateFormat.</para>
|
||||
<para>Json.NET also has the <codeEntityReference>T:Newtonsoft.Json.DateTimeZoneHandling</codeEntityReference>
|
||||
setting. This can be
|
||||
used to convert DateTime's kind when serializing, e.g. set
|
||||
DateTimeZoneHandling to Utc to serialize all DateTimes as UTC dates.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>DateTime JsonConverters</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializingDatesInJson" title="DateTime JsonConverters Example" />
|
||||
|
||||
<para>Simply pass the JsonConverter you wish to use to the Json.NET
|
||||
serializer.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>JavaScriptDateTimeConverter</title>
|
||||
<content>
|
||||
<para>The JavaScriptDateTimeConverter class is one of the two DateTime
|
||||
JsonConverters that come with Json.NET. This converter serializes a
|
||||
DateTime as a <externalLink>
|
||||
<linkText>JavaScript Date object</linkText>
|
||||
<linkUri>http://msdn.microsoft.com/en-us/library/cd9w2te4.aspx</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink>: <codeInline>new Date(1234656000000)</codeInline></para>
|
||||
<para>Technically this is invalid JSON according to the spec but all
|
||||
browsers, and some JSON frameworks, including Json.NET, support it.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>IsoDateTimeConverter</title>
|
||||
<content>
|
||||
<alert class="note">
|
||||
<para>Since Json.NET 4.5 dates are written using the ISO 8601
|
||||
format by default and using this converter is unnecessary.</para>
|
||||
</alert>
|
||||
<para>IsoDateTimeConverter seralizes a DateTime to an <externalLink>
|
||||
<linkText>ISO 8601</linkText>
|
||||
<linkUri>http://en.wikipedia.org/wiki/ISO_8601</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink> formatted
|
||||
string: <codeInline>"2009-02-15T00:00:00Z"</codeInline></para>
|
||||
<para>The IsoDateTimeConverter class has a property, DateTimeFormat, to
|
||||
further customize the formatted string.</para>
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,107 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serializing Dates in JSON</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serializing Dates in JSON</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
<p>DateTimes in JSON are hard.</p>
|
||||
<p>
|
||||
The problem comes from the <a href="http://www.ietf.org/rfc/rfc4627.txt" target="_blank">JSON spec</a> itself: there is no literal syntax for dates in JSON. The spec has objects, arrays, strings, integers and floats, but it defines no standard for what a date looks like.</p>
|
||||
<p>The default format used by <a href="http://james.newtonking.com/projects/json-net.aspx" target="_blank">Json.NET</a>
|
||||
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 <a href="html/T_Newtonsoft_Json_DateFormatHandling.htm">DateFormatHandling</a> setting to MicrosoftDateFormat.</p>
|
||||
<p>Json.NET also has the <a href="html/T_Newtonsoft_Json_DateTimeZoneHandling.htm">DateTimeZoneHandling</a> setting. This can be used to convert
|
||||
DateTime's kind when serializing, e.g. set DateTimeZoneHandling to Utc to
|
||||
serialize all DateTimes as UTC dates.</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: green;">// default as of Json.NET 4.5</span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> isoJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry);</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>
|
||||
<pre style="margin: 0px;"> <span style="color: rgb(43, 145, 175);">JsonSerializerSettings</span> microsoftDateFormatSettings = <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;"> DateFormatHandling = <span style="color: rgb(43, 145, 175);">DateFormatHandling</span>.MicrosoftDateFormat</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">string</span> microsoftJson = <span style="color: rgb(43, 145, 175);">JsonConvert</span>.SerializeObject(entry, microsoftDateFormatSettings);</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;"> <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>
|
||||
</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="http://msdn.microsoft.com/en-us/library/cd9w2te4.aspx" 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>
|
||||
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,92 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="Introduction" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<para>Json.NET is a popular high-performance JSON framework for .NET</para>
|
||||
</introduction>
|
||||
<!-- Optional procedures followed by optional code example but must have
|
||||
at least one procedure or code example -->
|
||||
<section>
|
||||
<title>Benefits and Features</title>
|
||||
<content>
|
||||
<list class="bullet">
|
||||
<listItem>
|
||||
<para>Flexible JSON serializer for converting between .NET objects and JSON</para>
|
||||
</listItem>
|
||||
<listItem>
|
||||
<para>LINQ to JSON for manually reading and writing JSON </para>
|
||||
</listItem>
|
||||
<listItem>
|
||||
<para>High performance, faster than .NET's built-in JSON serializers</para>
|
||||
</listItem>
|
||||
<listItem>
|
||||
<para>Write indented, easy to read JSON</para>
|
||||
</listItem>
|
||||
<listItem>
|
||||
<para>Convert JSON to and from XML</para>
|
||||
</listItem>
|
||||
<listItem>
|
||||
<para>Supports .NET 2, .NET 3.5, .NET 4, Silverlight, Windows Phone and Windows 8 Metro</para>
|
||||
</listItem>
|
||||
|
||||
</list>
|
||||
<para>The JSON serializer is a good choice when the JSON you are reading or writing maps closely to a .NET class.</para>
|
||||
<para>LINQ to JSON is good for situations where you are only interested in
|
||||
getting values from JSON, you 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.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>History</title>
|
||||
<content><para>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.</para>
|
||||
<para>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.</para>
|
||||
<para>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 <externalLink>
|
||||
<linkText>MonoRail</linkText>
|
||||
<linkUri>http://www.castleproject.org/projects/monorail</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink>,
|
||||
Castle Project's MVC web framework, and <externalLink>
|
||||
<linkText>Mono</linkText>
|
||||
<linkUri>http://www.mono-project.com/</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink>,
|
||||
an open source implementation
|
||||
of the .NET framework.</para>
|
||||
<para><externalLink>
|
||||
<linkText>James Newton-King</linkText>
|
||||
<linkUri>http://twitter.com/jamesnk</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink></para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Donate</title>
|
||||
<content><para>Json.NET is a free open source project developed in my own time.</para>
|
||||
<para>I really appreciate your feedback and support for Json.NET and its future development.</para></content>
|
||||
</section>
|
||||
|
||||
<markup>
|
||||
<p>
|
||||
<a target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=james%40newtonking%2ecom&item_name=Supporting%20Json%2eNET&no_shipping=0&no_note=1&tax=0&currency_code=USD&lc=US&bn=PP%2dDonationsBF&charset=UTF%2d8">
|
||||
<img style="border-width:0;" alt="Donate" src="../donate.gif" />
|
||||
</a>
|
||||
</p>
|
||||
</markup>
|
||||
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,68 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Introduction</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Introduction</span></div>
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"></span>
|
||||
<p>
|
||||
Json.NET makes working with JSON formatted data in .NET
|
||||
simple. Quickly read and write JSON using LINQ to JSON or serialize
|
||||
your .NET objects with a single method call using the JsonSerializer.</p>
|
||||
<h3>
|
||||
Features</h3>
|
||||
<ul>
|
||||
<li>Flexible JSON serializer to convert .NET objects to JSON and back again</li>
|
||||
<li>LINQ to JSON for reading and writing JSON</li>
|
||||
<li>Writes indented, easy to read JSON</li>
|
||||
<li>Convert JSON to and from XML</li>
|
||||
<li>Supports Silverlight and the Compact Framework</li>
|
||||
</ul>
|
||||
<p>The JSON serializer is a good choice when the JSON you are reading or writing maps closely
|
||||
to a .NET class. The serializer automatically reads and writes JSON for the class.</p>
|
||||
<p class="MsoNormal">
|
||||
For situations where you are only interested in getting values from JSON, don'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>
|
||||
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="LINQtoJSON" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>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
|
||||
<codeEntityReference>N:Newtonsoft.Json.Linq</codeEntityReference>
|
||||
namespace.</para>
|
||||
</introduction>
|
||||
|
||||
<section>
|
||||
<title>Creating JSON</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateNormal" title="Creating JSON" />
|
||||
|
||||
<para>Another option is to create JSON objects declaratively.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateDeclaratively" title="Creating JSON Declaratively" />
|
||||
|
||||
<para>You can create a JSON object from a non-JSON type using the
|
||||
<codeEntityReference>Overload:Newtonsoft.Json.Linq.JObject.FromObject</codeEntityReference>
|
||||
method.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateFromObject" title="Creating JSON From An Object" />
|
||||
|
||||
<para>Finally JSON objects can be created from a string use the
|
||||
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Parse(System.String)</codeEntityReference>
|
||||
method.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParse" title="Creating JSON From Text" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Querying JSON</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The properties and methods that are the most useful when querying JSON
|
||||
objects are
|
||||
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference>
|
||||
and
|
||||
<codeEntityReference>P:Newtonsoft.Json.Linq.JToken.Item(System.Object)</codeEntityReference>
|
||||
.</para>
|
||||
|
||||
<para>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.</para>
|
||||
|
||||
<para>The index property is used to get a specific child, either by
|
||||
index position for JSON arrays or property name for JSON objects.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonQuerying" title="Querying JSON" />
|
||||
|
||||
<para>LINQ to JSON can also be used to manually convert from JSON to a .NET object.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonDeserializeObject" title="Deserializing Using LINQ Objects" />
|
||||
|
||||
<para>Manually serializing and deserializing between .NET objects is most useful when
|
||||
working with JSON that doesn't closely match your .NET objects.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonDeserializeExample" title="Deserializing Using LINQ Example" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,254 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>LINQ to JSON</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">LINQ to JSON</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>LINQ to JSON is a programming API for working with JSON objects. The API has been designed with LINQ in mind to enable to quick querying and creation of JSON objects. LINQ to JSON sits under the <a href="./html/N_Newtonsoft_Json_Linq.htm">Newtonsoft.Json.Linq</a> namespace.</p>
|
||||
|
||||
<h3>Creating JSON</h3>
|
||||
<p>There are a number of different options when it comes to creating JSON using LINQ to JSON. The first to create objects imperatively. You have total control but it is more verbose than other options.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JArray</span> array = <span style="color: blue;">new</span> <span style="color: #2b91af;">JArray</span>();</pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JValue</span> text = <span style="color: blue;">new</span> <span style="color: #2b91af;">JValue</span>(<span style="color: #a31515;">"Manual text"</span>);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JValue</span> date = <span style="color: blue;">new</span> <span style="color: #2b91af;">JValue</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2000, 5, 23));</pre>
|
||||
<pre style="margin: 0px;"> </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>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Original { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Shortened { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Short { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">ShortieException</span> Error { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShortieException</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">int</span> Code { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> ErrorMessage { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
<p>Manually serializing and deserializing between .NET objects is most useful when working with JSON that doesn't closely match your .NET objects.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> jsonText = <span style="color: #a31515;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""short"":{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""original"":""http://www.foo.com/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""short"":""krehqk"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""error"":{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""code"":0,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""msg"":""No action taken""}</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> json = <span style="color: #2b91af;">JObject</span>.Parse(jsonText);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Shortie</span> shortie = <span style="color: blue;">new</span> <span style="color: #2b91af;">Shortie</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Original = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"original"</span>],</pre>
|
||||
<pre style="margin: 0px;"> Short = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"short"</span>],</pre>
|
||||
<pre style="margin: 0px;"> Error = <span style="color: blue;">new</span> <span style="color: #2b91af;">ShortieException</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Code = (<span style="color: blue;">int</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"error"</span>][<span style="color: #a31515;">"code"</span>],</pre>
|
||||
<pre style="margin: 0px;"> ErrorMessage = (<span style="color: blue;">string</span>)json[<span style="color: #a31515;">"short"</span>][<span style="color: #a31515;">"error"</span>][<span style="color: #a31515;">"msg"</span>]</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(shortie.Original);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// http://www.foo.com/</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(shortie.Error.ErrorMessage);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// No action taken</span></pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="PreserveObjectReferences" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="PreservingObjectReferencesOff" title="Preserve Object References Off" />
|
||||
|
||||
<para>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.</para>
|
||||
</introduction>
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>PreserveReferencesHandling</title>
|
||||
<content>
|
||||
<para>Settings PreserveReferencesHandling will track object references
|
||||
when serializing and deserializing JSON.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="PreservingObjectReferencesOn" title="Preserve Object References On" />
|
||||
|
||||
<para>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.</para>
|
||||
<para>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.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>IsReference</title>
|
||||
<content>
|
||||
<para>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.</para>
|
||||
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="PreservingObjectReferencesAttribute" title="IsReference" />
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>IReferenceResolver</title>
|
||||
<content>
|
||||
<para>To customize how references are generated and resolved the
|
||||
<codeEntityReference>T:Newtonsoft.Json.Serialization.IReferenceResolver</codeEntityReference>
|
||||
interface is available to inherit from and use with
|
||||
the JsonSerializer.</para>
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,138 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization and Preserving Object References</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization and Preserving Object References</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>By default Json.NET will serialize all objects it encounters by value. If a list
|
||||
contains two Person references, and both references point to the same object
|
||||
then the JsonSerializer will write out all the names and values for each
|
||||
reference.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p = <span style="color: blue;">new</span> <span style="color: #2b91af;">Person</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> BirthDate = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(1980, 12, 23, 0, 0, 0, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
|
||||
<pre style="margin: 0px;"> LastModified = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2009, 2, 20, 12, 59, 21, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
|
||||
<pre style="margin: 0px;"> Name = <span style="color: #a31515;">"James"</span></pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Person</span>> people = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Person</span>>();</pre>
|
||||
<pre style="margin: 0px;">people.Add(p);</pre>
|
||||
<pre style="margin: 0px;">people.Add(p);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(people, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//[</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Name": "James",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BirthDate": "\/Date(346377600000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LastModified": "\/Date(1235134761000)\/"</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;">// "Name": "James",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BirthDate": "\/Date(346377600000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LastModified": "\/Date(1235134761000)\/"</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>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>
|
||||
<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": "1",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Name": "James",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "BirthDate": "\/Date(346377600000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "LastModified": "\/Date(1235134761000)\/"</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;">// "$ref": "1"</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;">List</span><<span style="color: #2b91af;">Person</span>> deserializedPeople = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Person</span>>>(json,</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>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(deserializedPeople.Count);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 2</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p1 = deserializedPeople[0];</pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p2 = deserializedPeople[1];</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p1.Name);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// James</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p2.Name);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// James</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">bool</span> equal = <span style="color: #2b91af;">Object</span>.ReferenceEquals(p1, p2);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// true</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
<p>The first Person in the list is serizlied with the addition of an object Id. The
|
||||
second Person in JSON is now only a reference to the first.</p>
|
||||
<p>With PreserveReferencesHandling on now only one Person object is created on
|
||||
deserialization and the list contains two references to it, mirroring what we
|
||||
started with.</p>
|
||||
|
||||
<h3>IsReference on JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute</h3>
|
||||
|
||||
|
||||
<p>The PreserveReferencesHandling setting on the JsonSerializer will change
|
||||
how all objects are serialized and deserialized. For fine grain control over which
|
||||
objects and members should be serialized as a reference there is the IsReference property on
|
||||
the JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute.</p>
|
||||
<p>Setting IsReference on JsonObjectAttribute or JsonArrayAttribute to true will
|
||||
mean the JsonSerializer will always serialize the type the attribute is against
|
||||
as a reference. Setting IsReference on the JsonPropertyAttribute to true will
|
||||
serialize only that property as a reference.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;">[<span style="color: #2b91af;">JsonObject</span>(IsReference = <span style="color: blue;">true</span>)]</pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">EmployeeReference</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <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: #2b91af;">EmployeeReference</span> Manager { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
<h3>IReferenceResolver</h3>
|
||||
<p>To customize how references are generated and resolved the <a href="./html/T_Newtonsoft_Json_Serialization_IReferenceResolver.htm">IReferenceResolver</a> interface is available to inherit from and use with the JsonSerializer.</p>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="ReadingWritingJSON" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>To manually read and write JSON Json.NET provides the
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonReader</codeEntityReference>
|
||||
and
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonWriter</codeEntityReference> classes.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>JsonTextReader and JsonTextWriter</title>
|
||||
<content>
|
||||
<para><codeEntityReference>T:Newtonsoft.Json.JsonTextReader</codeEntityReference>
|
||||
and <codeEntityReference>T:Newtonsoft.Json.JsonTextWriter</codeEntityReference>
|
||||
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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\ReadingAndWritingJsonTests.cs" region="ReadingAndWritingJsonText" title="Writing JSON with JsonTextWriter" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>JTokenReader and JTokenWriter</title>
|
||||
<content>
|
||||
<para><codeEntityReference>T:Newtonsoft.Json.Linq.JTokenReader</codeEntityReference>
|
||||
and <codeEntityReference>T:Newtonsoft.Json.Linq.JTokenWriter</codeEntityReference>
|
||||
read and write LINQ to JSON objects. They are located in the
|
||||
<codeEntityReference>N:Newtonsoft.Json.Linq</codeEntityReference>
|
||||
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.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\ReadingAndWritingJsonTests.cs" region="ReadingAndWritingJsonLinq" title="Deserializing with JTokenReader" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,87 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Reading and Writing JSON</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Reading and Writing JSON</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>To manually read and write JSON Json.NET provides the <a href="./html/T_Newtonsoft_Json_JsonReader.htm">JsonReader</a> and <a href="./html/T_Newtonsoft_Json_JsonWriter.htm">JsonWriter</a> classes.</p>
|
||||
|
||||
<h3>JsonTextReader and JsonTextWriter</h3>
|
||||
<p><a href="./html/T_Newtonsoft_Json_JsonTextReader.htm">JsonTextReader</a> and <a href="./html/T_Newtonsoft_Json_JsonTextWriter.htm">JsonTextWriter</a> are used to read and write JSON text. The JsonTextWriter has a number of settings on it to control how JSON is formatted when it is written. These options include formatting, indention character, indent count and quote character.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">StringBuilder</span> sb = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringBuilder</span>();</pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">StringWriter</span> sw = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringWriter</span>(sb);</pre>
|
||||
<pre style="margin: 0px;"> </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>
|
||||
@@ -0,0 +1,127 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="ReducingSerializedJSONSize" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<!-- Uncomment this to generate an outline of the section and sub-section
|
||||
titles. Specify a numeric value as the inner text to limit it to
|
||||
a specific number of sub-topics when creating the outline. Specify
|
||||
zero (0) to limit it to top-level sections only. -->
|
||||
<!-- <autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<para>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.</para>
|
||||
</introduction>
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>JsonIgnoreAttribute and DataMemberAttribute</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>By default Json.NET will include all of a classes public properties and fields
|
||||
in the JSON it creates. Adding the
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonIgnoreAttribute</codeEntityReference>
|
||||
to a property tells the serializer to always skip writing it to the JSON result.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeOptOut" title="Opt-out Serialization Example" />
|
||||
|
||||
<para>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
|
||||
<codeEntityReference>T:System.Runtime.Serialization.DataContractAttribute</codeEntityReference>
|
||||
to the class and
|
||||
<codeEntityReference>T:System.Runtime.Serialization.DataMemberAttributes</codeEntityReference>
|
||||
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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeOptIn" title="Opt-in Serialization Example" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Formatting</title>
|
||||
<content>
|
||||
<para>JSON written by the serializer with an option of
|
||||
<codeEntityReference>T:Newtonsoft.Json.Formatting</codeEntityReference>
|
||||
set to Indented produces
|
||||
nicely formatted, easy to read JSON that is great for readability when you are
|
||||
developing. Formatting.None 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.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>NullValueHandling</title>
|
||||
<content>
|
||||
<para><codeEntityReference>T:Newtonsoft.Json.NullValueHandling</codeEntityReference>
|
||||
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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeNullValueHandlingObject" title="NullValueHandling Class" />
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeNullValueHandlingExample" title="NullValueHandling Ignore Example" />
|
||||
|
||||
<para>NullValueHandling can also be customized on individual properties
|
||||
using the a
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonPropertyAttribute</codeEntityReference>.
|
||||
The JsonPropertyAttribute value of
|
||||
NullValueHandling will override the setting on the JsonSerializer for that
|
||||
property.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>DefaultValueHandling</title>
|
||||
<content>
|
||||
<para><codeEntityReference>T:Newtonsoft.Json.DefaultValueHandling</codeEntityReference>
|
||||
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.</para>
|
||||
|
||||
<para>Json.NET also allows you to customize what the default value of an individual
|
||||
property is using the
|
||||
<codeEntityReference>T:System.ComponentModel.DefaultValueAttribute</codeEntityReference>.
|
||||
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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeDefaultValueHandlingObject" title="DefaultValueHandling Class" />
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeDefaultValueHandlingExample" title="DefaultValueHandling Ignore Example" />
|
||||
|
||||
<para>DefaultValueHandling can also be customized on individual properties using
|
||||
the a
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonPropertyAttribute</codeEntityReference>.
|
||||
The JsonPropertyAttribute value of DefaultValueHandling
|
||||
will override the setting on the JsonSerializer for that property.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>IContractResolver</title>
|
||||
<content>
|
||||
<para>For more flexibility the
|
||||
<codeEntityReference>T:Newtonsoft.Json.Serialization.IContractResolver</codeEntityReference>
|
||||
provides an interface to customize
|
||||
almost every aspect of how a .NET object gets serialized to JSON, including changing
|
||||
serialization behavior at runtime.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeContractResolverObject" title="IContractResolver Class" />
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="ReducingSerializedJsonSizeContractResolverExample" title="IContractResolver Example" />
|
||||
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,280 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Reducing Serialized JSON Size</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Reducing Serialized JSON Size</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
<p>One of the common problems encountered when serializing .NET objects to JSON is that the JSON ends up containing a lot of unwanted properties and values. This can be especially important when returning JSON to the client. More JSON means more bandwidth and a slower website.</p>
|
||||
|
||||
<p>To solve the issue of unwanted JSON Json.NET has a range of built in options to fine tune what gets written from a serialized object.</p>
|
||||
|
||||
<h3>JsonIgnoreAttribute and DataMemberAttribute </h3>
|
||||
<p>By default Json.NET will include all of a classes public properties and fields in
|
||||
the JSON it creates. Adding the <a href="./html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm">JsonIgnoreAttribute</a> to a property tells the serializer
|
||||
to always skip writing it to the JSON result. </p>
|
||||
|
||||
<div class="overflowpanel">
|
||||
<div class="code">
|
||||
<div style="font-family: courier new; color: black; font-size: 10pt;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Car</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <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>
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SelectToken" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>
|
||||
<codeEntityReference>Overload:Newtonsoft.Json.Linq.JToken.SelectToken</codeEntityReference>
|
||||
provides a method to query LINQ to JSON using a single string path to a desired
|
||||
<codeEntityReference>T:Newtonsoft.Json.Linq.JToken</codeEntityReference>.
|
||||
SelectToken makes dynamic queries easy because the entire query is defined in a string.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>SelectToken</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<para>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: <codeInline>Manufacturers[0].Name</codeInline>
|
||||
and <codeInline>Manufacturers(0).Name</codeInline>.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="SelectTokenComplex" title="SelectToken Example" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>SelectToken with LINQ</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>SelectToken can be used in combination with standard LINQ methods.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="SelectTokenLinq" title="SelectToken With LINQ Example" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,116 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Querying LINQ to JSON with SelectToken</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Querying LINQ to JSON with SelectToken</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
<p><a href="./html/M_Newtonsoft_Json_Linq_JToken_SelectToken.htm">SelectToken</a> provides a method to query LINQ to JSON using a single string path to a
|
||||
desired <a href="./html/T_Newtonsoft_Json_Linq_JToken.htm">JToken</a>. SelectToken makes dynamic queries
|
||||
easy because the entire
|
||||
query is defined in a string.</p>
|
||||
|
||||
<div class="overflowpanel">
|
||||
<div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">"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>
|
||||
@@ -0,0 +1,138 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializationAttributes" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<!-- Uncomment this to generate an outline of the section and sub-section
|
||||
titles. Specify a numeric value as the inner text to limit it to
|
||||
a specific number of sub-topics when creating the outline. Specify
|
||||
zero (0) to limit it to top-level sections only. -->
|
||||
<!-- <autoOutline /> -->
|
||||
<para>Attributes can be used to control how Json.NET serializes and deserializes .NET objects.</para>
|
||||
|
||||
<list class="bullet">
|
||||
<listItem><para><codeEntityReference>T:Newtonsoft.Json.JsonObjectAttribute</codeEntityReference> - Placed on classes to control how it should be serialized as a JSON object.</para></listItem>
|
||||
<listItem><para><codeEntityReference>T:Newtonsoft.Json.JsonArrayAttribute</codeEntityReference> - Placed on collections to control how it should be serialized as a JSON array.</para></listItem>
|
||||
<listItem><para><codeEntityReference>T:Newtonsoft.Json.JsonDictionaryAttribute</codeEntityReference> - Placed on dictionaries to control how it should be serialized as a JSON object.</para></listItem>
|
||||
<listItem><para><codeEntityReference>T:Newtonsoft.Json.JsonPropertyAttribute</codeEntityReference> - Placed on fields and properties to control how it should be serialized as a property in a JSON object.</para></listItem>
|
||||
<listItem><para><codeEntityReference>T:Newtonsoft.Json.JsonConverterAttribute</codeEntityReference> - Placed on either classes or fields and properties to specify which JsonConverter should be used during serialization.</para></listItem>
|
||||
</list>
|
||||
|
||||
</introduction>
|
||||
|
||||
<section>
|
||||
<title>Standard .NET Serialization Attributes</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>As well as using the built-in Json.NET attributes, Json.NET also looks for the <codeEntityReference>T:System.SerializableAttribute</codeEntityReference>
|
||||
(if IgnoreSerializableAttribute on DefaultContractResolver is set to false)
|
||||
<codeEntityReference>T:System.Runtime.Serialization.DataContractAttribute</codeEntityReference>,
|
||||
<codeEntityReference>T:System.Runtime.Serialization.DataMemberAttribute</codeEntityReference>
|
||||
and <codeEntityReference>T:System.NonSerializedAttribute</codeEntityReference> and attributes when determining how JSON is to be serialized and deserialized.
|
||||
</para>
|
||||
|
||||
|
||||
<alert class="note">
|
||||
<para>Json.NET attributes take presidence over standard .NET serialization attributes, e.g. if both JsonPropertyAttribute
|
||||
and DataMemberAttribute are present on a property and both customize the name,
|
||||
the name from JsonPropertyAttribute will be used.</para>
|
||||
</alert>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationAttributes" title="Serialization Attributes Example" />
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Json.NET Serialization Attributes</title>
|
||||
<sections>
|
||||
<section>
|
||||
<title>JsonObjectAttribute</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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), opt-out (everything is
|
||||
serialized by default but can be ignored with the JsonIgnoreAttribute, Json.NET's default behavor) or
|
||||
fields (all public and private fields are serialized, properties are ignored).</para>
|
||||
<para>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.</para>
|
||||
<para>The DataContractAttribute can be used as substitute for JsonObjectAttribute. The
|
||||
DataContractAttribute will default member serialization to opt-in.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>JsonArrayAttribute/JsonDictionaryAttribute</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The JsonArrayAttribute and JsonDictionaryAttributes are used to specified whether a class is serialized as that collection type.</para>
|
||||
<para>The collection attributes have options to customize the JsonConverter, type name handling and reference handling that are applied to collection items.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>JsonPropertyAttribute</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>JsonPropertyAttribute has a number of uses:</para>
|
||||
|
||||
<list class="bullet">
|
||||
<listItem><para>By default the JSON property will have the same name as the .NET property. This attribute allows the name to be customized.</para></listItem>
|
||||
<listItem><para>Indicates that a property should be serialized when member serialization is set to opt-in.</para></listItem>
|
||||
<listItem><para>Includes non-public properties in serialization and deserialization.</para></listItem>
|
||||
<listItem><para>Customize type name, reference, null and default value handling for the property value.</para></listItem>
|
||||
<listItem><para>Customize the property's collection items JsonConverter, type name handing and reference handling.</para></listItem>
|
||||
</list>
|
||||
|
||||
<para> The DataMemberAttribute can be used as substitute for JsonPropertyAttribute.</para>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>JsonIgnoreAttribute</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>Excludes a field or property from serialization.</para>
|
||||
<para>The <codeEntityReference>T:System.NonSerializedAttribute</codeEntityReference> can be used as substitute for JsonIgnoreAttribute.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>JsonConverterAttribute</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The JsonConverterAttribute specifies which JsonSerializer is used to convert an object.</para>
|
||||
<para>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.</para>
|
||||
<para>The priority of which JsonConverter is used is member attribute then class attribute and finally
|
||||
any converters passed to the JsonSerializer.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationCallbacksObject" title="JsonConverter Attribute" />
|
||||
|
||||
<para>This example shows the JsonConverterAttribute being applied to a property.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationCallbacksExample" title="JsonConverter Example" />
|
||||
|
||||
<para>To apply a JsonConverter to the items in a collection use either <codeEntityReference>T:Newtonsoft.Json.JsonArrayAttribute</codeEntityReference>,
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonDictionaryAttribute</codeEntityReference> or
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonPropertyAttribute</codeEntityReference>
|
||||
and set the ItemConverterType property to the converter type you want to use.</para>
|
||||
</content>
|
||||
</section>
|
||||
</sections>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,153 +0,0 @@
|
||||
<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_JsonDictionaryAttribute.htm">JsonDictionaryAttribute</a> - Placed on
|
||||
dictionaries to control how it should be serialized as a JSON object.</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.serializableattribute.aspx" target="_blank">
|
||||
Serializable</a> (if IgnoreSerializableAttribute on DefaultContractResolver is
|
||||
set to false)
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx" target="_blank">DataContract</a>,
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx" target="_blank">DataMember</a>
|
||||
and
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx" target="_blank">
|
||||
NotSerialized</a> and
|
||||
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: 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 because mode is opt-in</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), opt-out (everything is serialized by default but can be ignored with the JsonIgnoreAttribute,
|
||||
Json.NET's default behavor) or fields (all public and private fields are
|
||||
serialized, properties are ignored).</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>
|
||||
<p>The DataContractAttribute can be used as substitute for JsonObjectAttribute.
|
||||
The DataContractAttribute will default member serialization to opt-in.</p>
|
||||
|
||||
<h5>JsonArrayAttribute/JsonDictionaryAttribute</h5>
|
||||
<p>The JsonArrayAttribute and JsonDictionaryAttributes are used to specified
|
||||
whether a class is serialized as that collection type.</p>
|
||||
<p>The collection attributes have options to customize the JsonConverter, type name handling
|
||||
and reference handling that are applied to collection items.</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>
|
||||
<li>Customize type name, reference, null and default value handling for the
|
||||
property value.</li>
|
||||
<li>Customize the property's collection items JsonConverter, type name handing and
|
||||
reference handling.</li>
|
||||
</ul>
|
||||
<p>
|
||||
The DataMemberAttribute can be used as substitute for JsonPropertyAttribute.</p>
|
||||
<h5>JsonIgnoreAttribute</h5>
|
||||
<p>Excludes a field or property from serialization.</p>
|
||||
<p>The NotSerializedAttribute can be used as substitute for JsonIgnoreAttribute.</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>
|
||||
|
||||
<p>To apply a JsonConverter to the items in a collection use either
|
||||
<a href="html/T_Newtonsoft_Json_JsonArrayAttribute.htm">JsonArrayAttribute</a>,
|
||||
<a href="html/T_Newtonsoft_Json_JsonDictionaryAttribute.htm">
|
||||
JsonDictionaryAttribute</a> or
|
||||
<a href="html/T_Newtonsoft_Json_JsonPropertyAttribute.htm">JsonPropertyAttribute</a> and set
|
||||
the ItemConverterType property to the converter type you want to use.</p>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializationCallbacks" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<!-- Uncomment this to generate an outline of the section and sub-section
|
||||
titles. Specify a numeric value as the inner text to limit it to
|
||||
a specific number of sub-topics when creating the outline. Specify
|
||||
zero (0) to limit it to top-level sections only. -->
|
||||
<!-- <autoOutline /> -->
|
||||
<para>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.</para>
|
||||
|
||||
<list class="bullet">
|
||||
<listItem><para><legacyBold>OnSerializing</legacyBold></para></listItem>
|
||||
<listItem><para><legacyBold>OnSerialized</legacyBold></para></listItem>
|
||||
<listItem><para><legacyBold>OnDeserializing</legacyBold></para></listItem>
|
||||
<listItem><para><legacyBold>OnDeserialized</legacyBold></para></listItem>
|
||||
</list>
|
||||
<para>
|
||||
To tell the serializer which methods should be called during the object's
|
||||
serialization lifecycle, decorate a method with the appropraite attribute
|
||||
(<codeEntityReference>T:System.Runtime.Serialization.OnSerializingAttribute</codeEntityReference>,
|
||||
<codeEntityReference>T:System.Runtime.Serialization.OnSerializedAttribute</codeEntityReference>,
|
||||
<codeEntityReference>T:System.Runtime.Serialization.OnDeserializingAttribute</codeEntityReference>,
|
||||
<codeEntityReference>T:System.Runtime.Serialization.OnDeserializedAttribute</codeEntityReference>).
|
||||
</para>
|
||||
</introduction>
|
||||
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>Example</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>Example object with serialization callback methods:</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationCallbacksObject" title="Serialization Callback Attributes" />
|
||||
|
||||
<para>The example object being serialized and deserialized by Json.NET:</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationCallbacksExample" title="Serialization Callback Example" />
|
||||
|
||||
</content>
|
||||
<!-- If a section contains a sections element, its content creates
|
||||
sub-sections. These are not collapsible.
|
||||
<sections>
|
||||
<section address="SubSection1">
|
||||
<title>Sub-section 1</title>
|
||||
<content>
|
||||
<para>Sub-section content.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section address="SubSection2">
|
||||
<title>Sub-section 2</title>
|
||||
<content>
|
||||
<para>Sub-section content.</para>
|
||||
</content>
|
||||
</section>
|
||||
</sections> -->
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,143 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization Callbacks</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization Callbacks</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>Json.NET supports serialization callback methods. A callback can be used to
|
||||
manipulate an object before and after its serialization and deserialization by
|
||||
the JsonSerializer.</p>
|
||||
<ul>
|
||||
<li><b>OnSerializing</b></li>
|
||||
<li><b>OnSerialized</b></li>
|
||||
<li><b>OnDeserializing</b></li>
|
||||
<li><b>OnDeserialized</b></li>
|
||||
</ul>
|
||||
<p>To tell the serializer which methods should be called during the object's serialization
|
||||
lifecycle, decorate a method with the appropraite attribute (<a
|
||||
href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.onserializingattribute.aspx" target="_blank">OnSerializingAttribute</a>,
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.onserializedattribute.aspx" target="_blank">OnSerializedAttribute</a>,
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializingattribute.aspx" target="_blank">OnDeserializingAttribute</a>,
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx" target="_blank">OnDeserializedAttribute</a>).</p>
|
||||
<p>Example object with serialization callback methods:</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SerializationEventTestObject</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// This member is serialized and deserialized with no change.</span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">int</span> Member1 { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// The value of this field is set and reset during and </span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// after serialization.</span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member2 { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// This field is not serialized. The OnDeserializedAttribute </span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// is used to set the member value after serialization.</span></pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">JsonIgnore</span>]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member3 { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// This field is set to null, but populated after deserialization.</span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Member4 { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> SerializationEventTestObject()</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Member1 = 11;</pre>
|
||||
<pre style="margin: 0px;"> Member2 = <span style="color: #a31515;">"Hello World!"</span>;</pre>
|
||||
<pre style="margin: 0px;"> Member3 = <span style="color: #a31515;">"This is a nonserialized value"</span>;</pre>
|
||||
<pre style="margin: 0px;"> Member4 = <span style="color: blue;">null</span>;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnSerializing</span>]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnSerializingMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Member2 = <span style="color: #a31515;">"This value went into the data file during serialization."</span>;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnSerialized</span>]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnSerializedMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Member2 = <span style="color: #a31515;">"This value was reset after serialization."</span>;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnDeserializing</span>]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnDeserializingMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Member3 = <span style="color: #a31515;">"This value was set during deserialization"</span>;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnDeserialized</span>]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnDeserializedMethod(<span style="color: #2b91af;">StreamingContext</span> context)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Member4 = <span style="color: #a31515;">"This value was set after deserialization."</span>;</pre>
|
||||
<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 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>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member1);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Hello World!</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// This is a nonserialized value</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// null</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(obj, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Member1": 11,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Member2": "This value went into the data file during serialization.",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Member4": 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: #2b91af;">Console</span>.WriteLine(obj.Member1);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// This value was reset after serialization.</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// This is a nonserialized value</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// null</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;">obj = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">SerializationEventTestObject</span>>(json);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member1);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 11</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member2);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// This value went into the data file during serialization.</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member3);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// This value was set during deserialization</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(obj.Member4);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// This value was set after deserialization.</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,98 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializationErrorHandling" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<!-- Uncomment this to generate an outline of the section and sub-section
|
||||
titles. Specify a numeric value as the inner text to limit it to
|
||||
a specific number of sub-topics when creating the outline. Specify
|
||||
zero (0) to limit it to top-level sections only. -->
|
||||
<!-- <autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<para>Error handling is defined through two methods: the Error event on
|
||||
JsonSerializer and the OnErrorAttribute.</para>
|
||||
</introduction>
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>Error Event</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The <codeEntityReference>E:Newtonsoft.Json.JsonSerializer.Error</codeEntityReference>
|
||||
event is an event handler found on <codeEntityReference>T:Newtonsoft.Json.JsonSerializer</codeEntityReference>.
|
||||
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 <codeEntityReference>T:Newtonsoft.Json.JsonSerializerSettings</codeEntityReference>
|
||||
and passed to the serialization methods on JsonConvert.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationErrorHandling" title="Serialization Error Handling" />
|
||||
|
||||
<para>In this example we are deserializing a JSON array to a collection
|
||||
of DateTimes. On the JsonSerializerSettings a handler has been assigned
|
||||
to the Error event which will log the error message and mark the error
|
||||
as handled.</para>
|
||||
|
||||
<para>The result of deserializing the JSON is three successfully deserialized
|
||||
dates and three error messages: one for the badly formatted string, "I am
|
||||
not a date and will error!", one for the nested JSON array and one for the
|
||||
null value since the list doesn't allow nullable DateTimes. The event
|
||||
handler has logged these messages and Json.NET has continued on deserializing
|
||||
the JSON because the errors were marked as handled.</para>
|
||||
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationErrorHandlingWithParent" title="Parent Error Handling" />
|
||||
|
||||
<para>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
|
||||
<codeEntityReference>T:Newtonsoft.Json.Serialization.ErrorEventArgs</codeEntityReference>'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.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>OnErrorAttribute</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The <codeEntityReference>T:Newtonsoft.Json.Serialization.OnErrorAttribute</codeEntityReference>
|
||||
works much like the other <externalLink>
|
||||
<linkText>.NET serialization attributes</linkText>
|
||||
<linkUri>SerializationAttributes.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationErrorHandlingAttributeObject" title="Serialization Error Handling Attribute" />
|
||||
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializationErrorHandlingAttributeExample" title="Serialization Error Handling Example" />
|
||||
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,172 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization Error Handling</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serialization Error Handling</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>Json.NET supports error handling during serialization and deserialization. Error handling lets
|
||||
you catch an error and choose whether to handle it and continue with serialization or let the error
|
||||
bubble up and be thrown in your application.</p>
|
||||
<p>Error handling is defined through two methods:
|
||||
the Error event on JsonSerializer and the OnErrorAttribute.</p>
|
||||
<h3>Error Event</h3>
|
||||
|
||||
<p>
|
||||
The <a href="./html/E_Newtonsoft_Json_JsonSerializer_Error.htm">Error</a> event is an event handler found on <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a>. The error event is raised whenever an
|
||||
exception is thrown while serializing or deserialing JSON. Like all settings found on JsonSerializer
|
||||
it can also be set on <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializerSettings</a> and passed to the serialization methods on JsonConvert.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span><<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>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span><<span style="color: #2b91af;">DateTime</span>> c = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">DateTime</span>>>(<span style="color: #a31515;">@"[</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""2009-09-09T00:00:00Z"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""I am not a date and will error!"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> 1</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""1977-02-20T00:00:00Z"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""2000-12-01T00:00:00Z""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">]"</span>,</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializerSettings</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Error = <span style="color: blue;">delegate</span>(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">ErrorEventArgs</span> args)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> errors.Add(args.ErrorContext.Error.Message);</pre>
|
||||
<pre style="margin: 0px;"> args.ErrorContext.Handled = <span style="color: blue;">true</span>;</pre>
|
||||
<pre style="margin: 0px;"> },</pre>
|
||||
<pre style="margin: 0px;"> Converters = { <span style="color: blue;">new</span> <span style="color: #2b91af;">IsoDateTimeConverter</span>() }</pre>
|
||||
<pre style="margin: 0px;"> });</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 2009-09-09T00:00:00Z</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 1977-02-20T00:00:00Z</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 2000-12-01T00:00:00Z</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Unexpected token parsing date. Expected String, got StartArray.</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Cannot convert null value to System.DateTime.</span></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>
|
||||
In this example we are deserializing a JSON array to a collection of DateTimes. On the JsonSerializerSettings
|
||||
a handler has been assigned to the Error event which will log the error message and mark the error as handled.
|
||||
</p>
|
||||
<p>
|
||||
The result of deserializing the JSON is three successfully deserialized dates and three error messages:
|
||||
one for the badly formatted string, "I am not a date and will error!", one for the nested JSON array and one
|
||||
for the null value since the list doesn't allow nullable DateTimes. The event handler has logged these messages
|
||||
and Json.NET has continued on deserializing the JSON because the errors were marked as handled.
|
||||
</p>
|
||||
<p>
|
||||
One thing to note with error handling in Json.NET is that an unhandled error will bubble up and raise the event
|
||||
on each of its parents, e.g. an unhandled error when serializing a collection of objects will be raised twice,
|
||||
once against the object and then again on the collection. This will let you handle an error either where it
|
||||
occurred or on one of its parents.
|
||||
</p>
|
||||
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializer</span>();</pre>
|
||||
<pre style="margin: 0px;">serializer.Error += <span style="color: blue;">delegate</span>(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">ErrorEventArgs</span> args)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: green;">// only log an error once</span></pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">if</span> (args.CurrentObject == args.ErrorContext.OriginalObject)</pre>
|
||||
<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 style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PersonError</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">private</span> <span style="color: #2b91af;">List</span><<span style="color: blue;">string</span>> _roles;</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;">int</span> Age { <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: #2b91af;">List</span><<span style="color: blue;">string</span>> Roles</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">get</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">if</span> (_roles == <span style="color: blue;">null</span>)</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">Exception</span>(<span style="color: #a31515;">"Roles not loaded!"</span>);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">return</span> _roles;</pre>
|
||||
<pre style="margin: 0px;"> }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">set</span> { _roles = <span style="color: blue;">value</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;"> </pre>
|
||||
<pre style="margin: 0px;"> [<span style="color: #2b91af;">OnError</span>]</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">internal</span> <span style="color: blue;">void</span> OnError(<span style="color: #2b91af;">StreamingContext</span> context, <span style="color: #2b91af;">ErrorContext</span> errorContext)</pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> errorContext.Handled = <span style="color: blue;">true</span>;</pre>
|
||||
<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 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>
|
||||
<pre style="margin: 0px;"> Name = <span style="color: #a31515;">"George Michael Bluth"</span>,</pre>
|
||||
<pre style="margin: 0px;"> Age = 16,</pre>
|
||||
<pre style="margin: 0px;"> Roles = <span style="color: blue;">null</span>,</pre>
|
||||
<pre style="margin: 0px;"> Title = <span style="color: #a31515;">"Mister Manager"</span></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(person, <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;">// "Name": "George Michael Bluth",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Age": 16,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Title": "Mister Manager"</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
||||
</div>
|
||||
</div></div>
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,288 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializationGuide" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<!-- Uncomment this to generate an outline of the section and sub-section
|
||||
titles. Specify a numeric value as the inner text to limit it to
|
||||
a specific number of sub-topics when creating the outline. Specify
|
||||
zero (0) to limit it to top-level sections only. -->
|
||||
<!-- <autoOutline /> -->
|
||||
<para> The Json.NET serializer can serialize a wide variety of .NET objects. This guide looks at how it works, first at a high level and then in more detail.</para>
|
||||
</introduction>
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>Summary</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<para>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.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Complex Types</title>
|
||||
<content>
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>.NET</para></entry>
|
||||
<entry><para>JSON</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>IList, IEnumerable, IList<T>, Array</legacyBold></para></entry>
|
||||
<entry><para>Array</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>IDictionary, IDictionary<TKey, TValue></legacyBold></para></entry>
|
||||
<entry><para>Object</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Object (more detail below)</legacyBold></para></entry>
|
||||
<entry><para>Object</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Primitive Types</title>
|
||||
<content>
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>.NET</para></entry>
|
||||
<entry><para>JSON</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>String</legacyBold></para></entry>
|
||||
<entry><para>String</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Byte</legacyBold></para>
|
||||
<para><legacyBold>SByte</legacyBold></para>
|
||||
<para><legacyBold>UInt16</legacyBold></para>
|
||||
<para><legacyBold>Int16</legacyBold></para>
|
||||
<para><legacyBold>UInt32</legacyBold></para>
|
||||
<para><legacyBold>Int32</legacyBold></para>
|
||||
<para><legacyBold>UInt64</legacyBold></para>
|
||||
<para><legacyBold>Int64</legacyBold></para></entry>
|
||||
<entry><para>Integer</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>
|
||||
<para><legacyBold>Float</legacyBold></para>
|
||||
<para><legacyBold>Double</legacyBold></para>
|
||||
<para><legacyBold>Decimal</legacyBold></para>
|
||||
</entry>
|
||||
<entry><para>Float</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>
|
||||
<para><legacyBold>Enum</legacyBold></para>
|
||||
</entry>
|
||||
<entry><para>Integer (can be the enum value name with <codeEntityReference>T:Newtonsoft.Json.Converters.StringEnumConverter</codeEntityReference>)</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>
|
||||
<para><legacyBold>DateTime</legacyBold></para>
|
||||
</entry>
|
||||
<entry><para>String (<externalLink>
|
||||
<linkText>Serializing Dates in JSON</linkText>
|
||||
<linkUri>DatesInJSON.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>)</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>
|
||||
<para><legacyBold>Byte[]</legacyBold></para>
|
||||
</entry>
|
||||
<entry><para>String (base 64 encoded)</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>
|
||||
<para><legacyBold>Type</legacyBold></para>
|
||||
</entry>
|
||||
<entry><para>String (type name)</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>
|
||||
<para><legacyBold>Guid</legacyBold></para>
|
||||
</entry>
|
||||
<entry><para>String</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>
|
||||
<para><legacyBold><codeEntityReference>T:System.ComponentModel.TypeConverter</codeEntityReference> (convertible to String)</legacyBold></para>
|
||||
</entry>
|
||||
<entry><para>String</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Breakdown of Type Serialization</title>
|
||||
<content>
|
||||
<autoOutline />
|
||||
</content>
|
||||
<sections>
|
||||
<section address="Objects">
|
||||
<title>Objects</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>.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.</para>
|
||||
<para>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.</para>
|
||||
<para>Types can also be serialized using opt-in mode. Only properties and 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.</para>
|
||||
<para>Finally types can be serialized using a fields mode. All fields, both public and private, are serialized
|
||||
and properties are ignored. This can be specified by setting MemberSerialization.Fields on a type with the JsonObject
|
||||
attribute or by the .NET <codeEntityReference>T:System.SerializableAttribute</codeEntityReference>
|
||||
and setting IgnoreSerializableAttribute on DefaultContractResolver to false.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section address="Lists">
|
||||
<title>IEnumerable, Lists and Arrays</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para> .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.</para>
|
||||
<para>JsonArrayAttribute has options on it to customize the JsonConverter, type name handling and reference handling
|
||||
that are applied to collection items.</para>
|
||||
<para>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.</para>
|
||||
<para>When deserializing if a member is typed as the interface IList<T> then it will be deserialized as a
|
||||
List<T>.</para>
|
||||
<para>Read more about serializing collections here: <externalLink>
|
||||
<linkText>Serializing Collections</linkText>
|
||||
<linkUri>SerializingCollections.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink></para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section address="Dictionarys">
|
||||
<title>Dictionarys and Hashtables</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>.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.</para>
|
||||
<para>JsonDictionaryAttribute has options on it to customize the JsonConverter, type name handling and reference
|
||||
handling that are applied to collection items.</para>
|
||||
<para>When deserializing if a member is typed as the interface IDictionary<TKey, TValue> then it will be
|
||||
deserialized as a Dictionary<TKey, TValue>.</para>
|
||||
<para>Read more about serializing collections here: <externalLink>
|
||||
<linkText>Serializing Collections</linkText>
|
||||
<linkUri>SerializingCollections.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink></para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section address="Untyped_Objects">
|
||||
<title>Untyped Objects</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>.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).</para>
|
||||
<para>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.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section address="Dynamic">
|
||||
<title>Dynamic</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<para>The second usage of dynamic in .NET are classes that implement
|
||||
<codeEntityReference>T:System.Dynamic.IDynamicMetaObjectProvider</codeEntityReference>. This interface lets
|
||||
the implementor create dynamic objects that intercept the property and method calls on an object and use them.
|
||||
<codeEntityReference>T:System.Dynamic.ExpandoObject</codeEntityReference>
|
||||
is a good example of a dynamic object.</para>
|
||||
<para>Dynamic objects are serialized as JSON objects. A property is written for every member name returned by
|
||||
<codeEntityReference>M:System.Dynamic.DynamicMetaObject.GetDynamicMemberNames</codeEntityReference>.</para>
|
||||
<para>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.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section address="ISerializable">
|
||||
<title>ISerializable</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<para>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.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section address="LINQ">
|
||||
<title>LINQ to JSON</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>LINQ to JSON types (e.g. JObject, JArray) are automatically serialized and deserialized to their equivalent JSON
|
||||
when encountered by the Json.NET serializer.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section address="JsonConverter">
|
||||
<title>JsonConverter</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<para> 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.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
</sections>
|
||||
</section>
|
||||
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,346 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Serialization Guide</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 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
|
||||
properties and 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>
|
||||
<p>
|
||||
Finally types can be serialized using a fields mode. All fields, both public and
|
||||
private, are serialized and properties are ignored. This can be specified by
|
||||
setting MemberSerialization.Fields on a type with the JsonObject attribute or by the .NET
|
||||
<a href="http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx" target="_blank">
|
||||
Serializable</a> attribute and setting IgnoreSerializableAttribute on
|
||||
DefaultContractResolver to false.</p>
|
||||
<h4>
|
||||
<strong>IEnumerable, Lists and Arrayss</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>
|
||||
JsonArrayAttribute has options on it to customize the JsonConverter, type name handling
|
||||
and reference handling that are applied to collection items.</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>
|
||||
JsonDictionaryAttribute has options on it to customize the JsonConverter, type name handling
|
||||
and reference handling that are applied to collection items.</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>
|
||||
@@ -0,0 +1,326 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializationSettings" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<para>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.</para>
|
||||
</introduction>
|
||||
|
||||
<section>
|
||||
<title>DateFormatHandling</title>
|
||||
<content><para>DateFormatHandling controls how dates are serialized.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>IsoDateFormat</legacyBold></para></entry>
|
||||
<entry><para>By default Json.NET writes dates in the ISO 8601 format, e.g. "2012-03-21T05:40Z".</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>MicrosoftDateFormat</legacyBold></para></entry>
|
||||
<entry><para>Dates are written in the Microsoft JSON format, e.g. "\/Date(1198908717056)\/".</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>MissingMemberHandling</title>
|
||||
<content><para>MissingMemberHandling controls how missing members, e.g. JSON contains a property that isn't a member on the object, are handled during deserialization.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>Ignore</legacyBold></para></entry>
|
||||
<entry><para>By default Json.NET ignores JSON if there is no field or property for its value to be set to during deserialization.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Error</legacyBold></para></entry>
|
||||
<entry><para>Json.NET errors when there is a missing member during deserialization.</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>ReferenceLoopHandling</title>
|
||||
<content><para>ReferenceLoopHandling controls how circular referencing objects, e.g. a Person object referencing itself via a Manager property, are serialized.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para>Error</para></entry>
|
||||
<entry><para>By default Json.NET will error if a reference loop is encountered (otherwise the serializer will get into an infinite loop).</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para>Ignore</para></entry>
|
||||
<entry><para>Json.NET will ignore objects in reference loops and not serialize them. The first time an object is encountered it will be serialized as usual but if the object is encountered as a child object of itself the serializer will skip serializing it.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para>Serialize</para></entry>
|
||||
<entry><para>This option forces Json.NET to serialize objects in reference loops. This is useful if objects are nested but not indefinitely.</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
<para>ReferenceLoopHandling can also be customized on individual properties with JsonPropertyAttribute.</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>NullValueHandling</title>
|
||||
<content><para>NullValueHandling controls how null values on .NET objects are handled during serialization and how null values in JSON are handled during deserialization.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para>Include</para></entry>
|
||||
<entry><para>By default Json.NET writes null values to JSON when serializing and sets null values to fields/properties when deserializing.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para>Ignore</para></entry>
|
||||
<entry><para>Json.NET will skip writing JSON properties if the .NET value is null when serializing and will skip setting fields/properties if the JSON property is null when deserializing.</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
<para>NullValueHandling can also be customized on individual properties with JsonPropertyAttribute.</para>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>DefaultValueHandling</title>
|
||||
<content><para>DefaultValueHandling controls how Json.NET uses default values set using the .NET DefaultValueAttribute when serializing and deserializing.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>Include</legacyBold></para></entry>
|
||||
<entry><para>By default Json.NET will write a field/property value to JSON when serializing if the value is the same as the field/property's default value. The Json.NET deserializer will continue setting a field/property if the JSON value is the same as the default value.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Ignore</legacyBold></para></entry>
|
||||
<entry><para>Json.NET will skip writing a field/property value to JSON if the value is the same as the field/property's default value. The Json.NET deserializer will skip setting a .NET object's field/property if the JSON value is the same as the default value.</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
<para>DefaultValueHandling can also be customized on individual properties with JsonPropertyAttribute.</para>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>ObjectCreationHandling</title>
|
||||
<content><para>ObjectCreationHandling controls how objects are created and deserialized to during deserialization.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>Auto</legacyBold></para></entry>
|
||||
<entry><para>By default Json.NET will attempt to set JSON values onto existing objects and add JSON values to existing collections during deserialization. </para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Reuse</legacyBold></para></entry>
|
||||
<entry><para>Same behaviour as auto.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Replace</legacyBold></para></entry>
|
||||
<entry><para>Json.NET will always recreate objects and collections before setting values to them during deserialization.</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
<para>ObjectCreationHandling can also be customized on individual properties with JsonPropertyAttribute.</para>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>TypeNameHandling</title>
|
||||
<content><para>TypeNameHandling controls whether Json.NET includes .NET type names during serialization with a $type property and reads .NET type names from that property to determine what type to create during deserialization.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>None</legacyBold></para></entry>
|
||||
<entry><para>By default Json.NET does not read or write type names during deserialization.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Objects</legacyBold></para></entry>
|
||||
<entry><para>Json.NET will write and use type names for objects but not collections.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Arrays</legacyBold></para></entry>
|
||||
<entry><para>Json.NET will write and use type names for collections but not objects.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Auto</legacyBold></para></entry>
|
||||
<entry><para>Json.NET will check whether an object/collection matches its declared property and writes the type name if they do not match, e.g. a property with a type of Mammal has a derived instance of Dog assigned. Auto will ensure that type information isn't lost when serializing/deserializing automatically without having to write type names for every object.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>All</legacyBold></para></entry>
|
||||
<entry><para>Json.NET will write and use type names for objects and collections.</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
<para>TypeNameHandling can also be customized on individual properties with JsonPropertyAttribute.</para>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>TypeNameAssemblyFormat</title>
|
||||
<content><para>TypeNameAssemblyFormat controls how type names are written during serialization.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>Simple</legacyBold></para></entry>
|
||||
<entry><para>By default Json.NET writes the partial assembly name with the type, e.g. System.Data.DataSet, System.Data. Note that Silverlight and Windows Phone are not able to use this format.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>Full</legacyBold></para></entry>
|
||||
<entry><para>Json.NET will write the full assembly name, including version number, culture and public key token.</para></entry>
|
||||
</row>
|
||||
</table>
|
||||
<para>Read more about the valid values at <codeEntityReference>T:System.Runtime.Serialization.Formatters.FormatterAssemblyStyle</codeEntityReference>.</para>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Binder</title>
|
||||
<content><para>The serialization binder is used to resolve type names to a .NET type.</para>
|
||||
<para>Read more about the serialization binder here: <codeEntityReference>T:System.Runtime.Serialization.SerializationBinder</codeEntityReference></para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>ConstructorHandling</title>
|
||||
<content><para>ConstructorHandling controls how constructors are used when initializing objects during deserialization.</para>
|
||||
|
||||
<table>
|
||||
<tableHeader>
|
||||
<row>
|
||||
<entry><para>Member</para></entry>
|
||||
<entry><para>Description</para></entry>
|
||||
</row>
|
||||
</tableHeader>
|
||||
<row>
|
||||
<entry><para><legacyBold>Default</legacyBold></para></entry>
|
||||
<entry><para>By default Json.NET will first look for a constructor marked with the JsonConstructorAttribute, then look for a public default constructor
|
||||
(a constructor that doesn't take any arguments), then check if the class has a single public constructor with arguments and finally check for a
|
||||
non-public default constructor. If the class has multiple public constructors with arguments an error will be thrown. This can be fixed by
|
||||
marking one of the constructors with the JsonConstructorAttribute.</para></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><para><legacyBold>AllowNonPublicDefaultConstructor</legacyBold></para></entry>
|
||||
<entry><para>Json.NET will use a classes private default constructor before constructors with arguments if available. </para></entry>
|
||||
</row>
|
||||
</table>
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Converters</title>
|
||||
<content><para>This is the collection of JsonConverters that will be used during serialization and deserialization.</para>
|
||||
<para>JsonConverters allows JSON to be manually written during serialization and read during deserialization.
|
||||
This is useful for particularly complex JSON structures or for when you want to change how a type is serialized.</para>
|
||||
<para>When a JsonConverter has been added to a JsonSerializer it will be checked for every value that is being serialized/deserialized
|
||||
using its CanConvert to see if it should be used. If CanConvert returns true then the JsonConverter will be used to read or write
|
||||
the JSON for that value. Note that while a JsonConverter gives you complete control over that values JSON, many Json.NET serialization
|
||||
features are no longer available like type name and reference handling.</para>
|
||||
<para>To create your own custom converter inherit from the JsonConverter class. Read more about the built-in JsonConverters below:</para>
|
||||
|
||||
|
||||
<list class="bullet">
|
||||
<listItem><para><externalLink>
|
||||
<linkText>Serializing Dates in JSON</linkText>
|
||||
<linkUri>DatesInJSON.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink></para></listItem>
|
||||
<listItem><para><externalLink>
|
||||
<linkText>Converting between JSON and XML</linkText>
|
||||
<linkUri>ConvertingJSONandXML.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink></para></listItem>
|
||||
<listItem><para><externalLink>
|
||||
<linkText>CustomCreationConverter</linkText>
|
||||
<linkUri>CustomCreationConverter.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink></para></listItem>
|
||||
<listItem><para><codeEntityReference>T:Newtonsoft.Json.Converters.StringEnumConverter</codeEntityReference></para></listItem>
|
||||
</list>
|
||||
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>ContractResolver</title>
|
||||
<content><para>Internally for every .NET type the JsonSerializer will create a contract of how the type should be serialized and deserialized,
|
||||
based on type metadata and attributes applied to the class. Specifying a custom IContractResolver allows the creation of contracts to be customized.</para>
|
||||
<para>Read more about Contract Resolvers here: <externalLink>
|
||||
<linkText>Contract Resolvers</linkText>
|
||||
<linkUri>ContractResolver.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink></para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Error</title>
|
||||
<content><para>The Error event can catch errors during serialization and either handle the event and continue with
|
||||
serialization or let the error bubble up and be thrown to the application.</para>
|
||||
<para>Read more about error handling here: <externalLink>
|
||||
<linkText>Serialization Error Handling</linkText>
|
||||
<linkUri>SerializationErrorHandling.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,513 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serialization Settings</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 Settings</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<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>
|
||||
|
||||
<h3>DateFormatHandling</h3>
|
||||
<p>DateFormatHandling controls how dates are serialized.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>IsoDateFormat</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET writes dates in the ISO 8601 format, e.g. "2012-03-21T05:40Z".
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>MicrosoftDateFormat</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Dates are written in the Microsoft JSON format, e.g. "\/Date(1198908717056)\/".
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<h3>MissingMemberHandling</h3>
|
||||
<p>MissingMemberHandling controls how missing members, e.g. JSON contains a property
|
||||
that isn't a member on the object, are handled during deserialization.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Ignore</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET ignores JSON if there is no field
|
||||
or property for its value to be set to during deserialization.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Error</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET errors when there is a missing member during
|
||||
deserialization.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3>ReferenceLoopHandling</h3>
|
||||
<p>ReferenceLoopHandling controls how circular referencing objects, e.g. a Person
|
||||
object referencing itself via a Manager property, are serialized.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Error</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET will error if a reference loop is
|
||||
encountered (otherwise the serializer will get into an infinite loop).
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Ignore</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will ignore objects in reference loops and
|
||||
not serialize them. The first time an object is encountered it will be
|
||||
serialized as usual but if the object is encountered as a child object of itself
|
||||
the serializer will skip serializing it.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Serialize</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
This option forces Json.NET to serialize objects in
|
||||
reference loops. This is useful if objects are nested but not indefinitely.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>ReferenceLoopHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
|
||||
<h3>NullValueHandling</h3>
|
||||
<p>NullValueHandling controls how null values on .NET objects are handled during
|
||||
serialization and how null values in JSON are handled during deserialization.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Include</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET writes null values to JSON when
|
||||
serializing and sets null values to fields/properties when deserializing.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Ignore</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will skip writing JSON properties if the .NET
|
||||
value is null when serializing and will skip setting fields/properties if the
|
||||
JSON property is null when deserializing.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>NullValueHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
|
||||
<h3>
|
||||
DefaultValueHandling</h3>
|
||||
<p>
|
||||
DefaultValueHandling controls how Json.NET uses default values set using the .NET
|
||||
DefaultValueAttribute when serializing and deserializing.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Include</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET will write a field/property value
|
||||
to JSON when serializing if the value is the same as the field/property's
|
||||
default value. The Json.NET deserializer will continue setting a field/property
|
||||
if the JSON value is the same as the default value.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Ignore</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will skip writing a field/property value to
|
||||
JSON if the value is the same as the field/property's default value. The
|
||||
Json.NET deserializer will skip setting a .NET object's field/property if the
|
||||
JSON value is the same as the default value.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>DefaultValueHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
|
||||
<h3>ObjectCreationHandling</h3>
|
||||
<p>ObjectCreationHandling controls how objects are created and deserialized to
|
||||
during deserialization.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Auto</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET will attempt to set JSON values onto
|
||||
existing objects and add JSON values to existing collections during
|
||||
deserialization.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Reuse</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Same behaviour as auto.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Replace</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will always recreate objects and collections
|
||||
before setting values to them during deserialization.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>ObjectCreationHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
|
||||
<h3>TypeNameHandling</h3>
|
||||
<p>TypeNameHandling controls whether Json.NET includes .NET type names during
|
||||
serialization with a $type property and reads .NET type names from that property
|
||||
to determine what type to create during deserialization.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>None</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET does not read or write type names
|
||||
during deserialization.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Objects</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will write and use type names for objects
|
||||
but not collections.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Arrays</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will write and use type names for collections
|
||||
but not objects.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Auto</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will check whether an object/collection matches
|
||||
its declared property and writes the type name if they do not match, e.g. a
|
||||
property with a type of Mammal has a derived instance of Dog assigned. Auto will
|
||||
ensure that type information isn't lost when serializing/deserializing
|
||||
automatically without having to write type names for every object.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>All</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will write and use type names for objects and
|
||||
collections.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>TypeNameHandling can also be customized on individual properties with JsonPropertyAttribute.</p>
|
||||
<h3>TypeNameAssemblyFormat</h3>
|
||||
<p>TypeNameAssemblyFormat controls how type names are written during serialization.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Simple</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET writes the partial assembly name
|
||||
with the type, e.g. System.Data.DataSet, System.Data. Note that Silverlight and
|
||||
Windows Phone are not able to use this format.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Full</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will write the full assembly name, including
|
||||
version number, culture and public key token.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Read more about the valid values at
|
||||
<a href="http://msdn.microsoft.com/en-us/library/tt9xha1h" target="_blank">
|
||||
FormatterAssemblyStyle</a>. </p>
|
||||
<h3>Binder</h3>
|
||||
<p>The serialization binder is used to resolve type names to a .NET type.</p>
|
||||
<p>Read more about the serialization binder here:
|
||||
<a href="http://msdn.microsoft.com/en-us/library/ffas09b2" target="_blank">SerializationBinder</a></p>
|
||||
<h3>ConstructorHandling</h3>
|
||||
<p>ConstructorHandling controls how constructors are used when initializing objects
|
||||
during deserialization.</p>
|
||||
<table class="members">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th class="nameColumn">
|
||||
Member
|
||||
</th>
|
||||
<th class="descriptionColumn">
|
||||
Description
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>Default</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
By default Json.NET will first look for a constructor
|
||||
marked with the JsonConstructorAttribute, then look for a public default
|
||||
constructor (a constructor that doesn't take any arguments), then check if
|
||||
the class has a single public constructor with arguments and finally check
|
||||
for a non-public default constructor. If the class has multiple public
|
||||
constructors with arguments an error will be thrown. This can be fixed by
|
||||
marking one of the constructors with the JsonConstructorAttribute.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<b>AllowNonPublicDefaultConstructor</b>
|
||||
</td>
|
||||
<td>
|
||||
<div class="summary">
|
||||
Json.NET will use a classes private default constructor before constructors
|
||||
with arguments if available.
|
||||
</div>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3>Converters</h3>
|
||||
<p>This is the collection of JsonConverters that will be used during serialization
|
||||
and deserialization. </p>
|
||||
<p>JsonConverters allows JSON to be manually written during serialization and read
|
||||
during deserialization. This is useful for particularly complex JSON structures
|
||||
or for when you want to change how a type is serialized.</p>
|
||||
<p>When a JsonConverter has been added to a JsonSerializer it will be checked for
|
||||
every value that is being serialized/deserialized using its CanConvert to see if
|
||||
it should be used. If CanConvert returns true then the JsonConverter will be
|
||||
used to read or write the JSON for that value. Note that while a JsonConverter
|
||||
gives you complete control over that values JSON, many Json.NET serialization
|
||||
features are no longer available like type name and reference handling.</p>
|
||||
<p>To create your own custom converter inherit from the JsonConverter class. Read
|
||||
more about the built-in JsonConverters below:</p>
|
||||
<ul>
|
||||
<li><a href="DatesInJSON.html">Serializing Dates in JSON</a></li>
|
||||
<li><a href="ConvertingJSONandXML.html">Converting between JSON and XML</a></li>
|
||||
<li><a href="CustomCreationConverter.html">CustomCreationConverter</a></li>
|
||||
<li><a href="html/T_Newtonsoft_Json_Converters_StringEnumConverter.htm">
|
||||
StringEnumConverter</a></li>
|
||||
</ul>
|
||||
<h3>ContractResolver</h3>
|
||||
<p>Internally for every .NET type the JsonSerializer will create a contract of how
|
||||
the type should be serialized and deserialized, based on type metadata and
|
||||
attributes applied to the class. Specifying a custom IContractResolver allows
|
||||
the creation of contracts to be customized.</p>
|
||||
<p>Read more about Contract Resolvers here: <a href="ContractResolver.html">Contract
|
||||
Resolvers</a></p>
|
||||
<h3>Error</h3>
|
||||
<p>The Error event can catch errors during serialization and either handle the event
|
||||
and continue with serialization or let the error bubble up and be thrown to the
|
||||
application.</p>
|
||||
<p>Read more about error handling here: <a href="SerializationErrorHandling.html">
|
||||
Serialization Error Handling</a></p>
|
||||
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializingCollections" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<!-- Uncomment this to generate an outline of the section and sub-section
|
||||
titles. Specify a numeric value as the inner text to limit it to
|
||||
a specific number of sub-topics when creating the outline. Specify
|
||||
zero (0) to limit it to top-level sections only. -->
|
||||
<!-- <autoOutline /> -->
|
||||
<para>Json.NET has excellent support for serializing and deserializing
|
||||
collections of objects.</para>
|
||||
</introduction>
|
||||
<!-- Add one or more top-level section elements. These are collapsible.
|
||||
If using <autoOutline />, add an address attribute to identify it
|
||||
and specify a title so that it can be jumped to with a hyperlink. -->
|
||||
<section>
|
||||
<title>Serializing Collections</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializingCollectionsSerializing" title="Serializing Collections" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Deserializing Collections</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializingCollectionsDeserializing" title="Deserializing Collections" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Deserializing Dictionaries</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>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.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializingCollectionsDeserializingDictionaries" title="Deserializing Dictionaries" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,128 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serializing Collections</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serializing Collections</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>The <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a> has
|
||||
great support for serializing and deserializing collections of objects.</p>
|
||||
|
||||
<h3>Serializing</h3>
|
||||
<p>To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for.
|
||||
Json.NET will serialize the collection and all of the values it contains.</p>
|
||||
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> p1 = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span></pre>
|
||||
<pre style="margin: 0px;"> {</pre>
|
||||
<pre style="margin: 0px;"> Name = <span style="color: #a31515;">"Product 1"</span>,</pre>
|
||||
<pre style="margin: 0px;"> Price = 99.95m,</pre>
|
||||
<pre style="margin: 0px;"> ExpiryDate = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2000, 12, 29, 0, 0, 0, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
|
||||
<pre style="margin: 0px;"> };</pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> p2 = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> Name = <span style="color: #a31515;">"Product 2"</span>,</pre>
|
||||
<pre style="margin: 0px;"> Price = 12.50m,</pre>
|
||||
<pre style="margin: 0px;"> ExpiryDate = <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(2009, 7, 31, 0, 0, 0, <span style="color: #2b91af;">DateTimeKind</span>.Utc),</pre>
|
||||
<pre style="margin: 0px;">};</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Product</span>> products = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Product</span>>();</pre>
|
||||
<pre style="margin: 0px;">products.Add(p1);</pre>
|
||||
<pre style="margin: 0px;">products.Add(p2);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #2b91af;">JsonConvert</span>.SerializeObject(products, <span style="color: #2b91af;">Formatting</span>.Indented);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">//[</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Name": "Product 1",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "ExpiryDate": "\/Date(978048000000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Price": 99.95,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Sizes": null</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;">// "Name": "Product 2",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "ExpiryDate": "\/Date(1248998400000)\/",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Price": 12.50,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// "Sizes": null</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>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>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Name"": ""Product 1"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""ExpiryDate"": ""\/Date(978048000000)\/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 99.95,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Sizes"": null</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"": ""Product 2"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""ExpiryDate"": ""\/Date(1248998400000)\/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Price"": 12.50,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""Sizes"": null</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;">List</span><<span style="color: #2b91af;">Product</span>> products = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">List</span><<span style="color: #2b91af;">Product</span>>>(json);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(products.Count);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 2</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> p1 = products[0];</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p1.Name);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Product 1</span></pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<h3>Deserializing Dictionaries</h3>
|
||||
|
||||
<p>Using Json.NET you can also deserialize a JSON object into a .NET generic dictionary. The JSON object's property names and values will be added to the dictionary.</p>
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #a31515;">@"{""key1"":""value1"",""key2"":""value2""}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Dictionary</span><<span style="color: blue;">string</span>, <span style="color: blue;">string</span>> values = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">Dictionary</span><<span style="color: blue;">string</span>, <span style="color: blue;">string</span>>>(json);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(values.Count);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// 2</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(values[<span style="color: #a31515;">"key1"</span>]);</pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// value1</span></pre>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
|
||||
<div id="footer">
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializingJSON" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<!--
|
||||
<summary>
|
||||
<para>Optional summary abstract</para>
|
||||
</summary>
|
||||
-->
|
||||
<introduction>
|
||||
<para>The quickest method of converting between JSON text and a .NET object is using the
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonSerializer</codeEntityReference>.
|
||||
The JsonSerializer converts .NET objects into their JSON equivalent and back again by mapping their properties to each other via name.</para>
|
||||
</introduction>
|
||||
<!-- Optional procedures followed by optional code example but must have
|
||||
at least one procedure or code example -->
|
||||
<section>
|
||||
<title>JsonConvert</title>
|
||||
<content><para>For simple scenarios where you want to convert to and from a JSON string the
|
||||
<codeEntityReference>M:Newtonsoft.Json.JsonConvert.SerializeObject(System.Object,Newtonsoft.Json.Formatting)</codeEntityReference> and
|
||||
<codeEntityReference>M:Newtonsoft.Json.JsonConvert.DeserializeObject``1(System.String,Newtonsoft.Json.JsonSerializerSettings)</codeEntityReference>
|
||||
methods on JsonConvert provide an easy to use wrapper over JsonSerializer.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializeObject" title="Serializing JSON with JsonConvert" />
|
||||
<para>
|
||||
SerializeObject and DeserializeObject both have overloads that take a <codeEntityReference>T:Newtonsoft.Json.JsonSerializerSettings</codeEntityReference> object.
|
||||
JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.
|
||||
</para>
|
||||
</content>
|
||||
</section>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<section>
|
||||
<title>JsonSerializer</title>
|
||||
<content><para>For more control over how an object is serialized the <codeEntityReference>T:Newtonsoft.Json.JsonSerializer</codeEntityReference> can be used directly.
|
||||
The JsonSerializer is able to read and write JSON text directly to a stream via <codeEntityReference>T:Newtonsoft.Json.JsonTextReader</codeEntityReference>
|
||||
and <codeEntityReference>T:Newtonsoft.Json.JsonTextWriter</codeEntityReference>.
|
||||
Other kinds of JsonWriters can also be used such as
|
||||
<codeEntityReference>T:Newtonsoft.Json.Linq.JTokenReader</codeEntityReference>/<codeEntityReference>T:Newtonsoft.Json.Linq.JTokenWriter</codeEntityReference>
|
||||
to convert your object to and from
|
||||
LINQ to JSON objects or
|
||||
<codeEntityReference>T:Newtonsoft.Json.Bson.BsonReader</codeEntityReference>/<codeEntityReference>T:Newtonsoft.Json.Bson.BsonWriter</codeEntityReference> to convert to and from BSON.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="JsonSerializerToStream" title="Serializing JSON to a Stream with JsonSerializer" />
|
||||
|
||||
<para>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.</para>
|
||||
<para>Read more about the available JsonSerializer settings here: <externalLink>
|
||||
<linkText>Serialization Settings</linkText>
|
||||
<linkUri>SerializationSettings.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink></para>
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,103 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serializing and Deserializing JSON</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serializing and Deserializing JSON</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
|
||||
<p>The quickest method of converting between JSON text and a .NET object is using
|
||||
the <a href="./html/T_Newtonsoft_Json_JsonSerializer.htm">JsonSerializer</a>. The JsonSerializer converts .NET objects into their JSON
|
||||
equivalent and back again.</p>
|
||||
|
||||
|
||||
<p>For simple scenarios where you want to convert to and from a JSON string the <a href="./html/Overload_Newtonsoft_Json_JsonConvert_SerializeObject.htm">SerializeObject</a> and <a href="./html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm">DeserializeObject</a> methods on <a href="./html/T_Newtonsoft_Json_JsonConvert.htm">JsonConvert</a> provide an easy to use wrapper over
|
||||
JsonSerializer.</p>
|
||||
|
||||
|
||||
<div class="overflowpanel"> <div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">Product</span> product = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span>();</pre>
|
||||
<pre style="margin: 0px;"> </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>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializingJSONFragments" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
|
||||
<introduction>
|
||||
<para>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.</para>
|
||||
<para>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.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializingPartialJsonFragmentsObject" title="Fragments Object" />
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\SerializationTests.cs" region="SerializingPartialJsonFragmentsExample" title="Serializing Partial JSON Fragment Example" />
|
||||
|
||||
</introduction>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -1,125 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serializing Partial JSON Fragments</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serializing Partial JSON Fragments</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
<p>Often when working with large JSON documents you're only interested in a small fragment of information.
|
||||
This scenario can be annoying when you want to serialize that Json.NET into .NET
|
||||
objects because you have to define .NET classes for the entire JSON result.</p>
|
||||
<p>With Json.NET it is easy to get around this problem. Using LINQ to JSON you can
|
||||
extract the pieces of JSON you want to serialize before passing them to the Json.NET serializer.</p>
|
||||
|
||||
<div class="overflowpanel">
|
||||
<div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SearchResult</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <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>
|
||||
@@ -1,19 +0,0 @@
|
||||
div#content {
|
||||
padding: 0 10px 10px 10px;
|
||||
}
|
||||
|
||||
.overflowpanel
|
||||
{
|
||||
width: 98%;
|
||||
border: solid 1px #ccc;
|
||||
overflow: auto;
|
||||
overflow-y: hidden;
|
||||
background-color: #fcfcfc;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.overflowpanel .code
|
||||
{
|
||||
padding: 10px 4px 20px 10px;
|
||||
display: block;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Topics>
|
||||
<Topic id="Introduction" visible="True" isDefault="true" />
|
||||
<Topic id="SerializingJSON" visible="True" isExpanded="true" title="Serializing and Deserializing JSON">
|
||||
<Topic id="SerializationSettings" visible="True" title="Serialization Settings" />
|
||||
<Topic id="SerializationGuide" visible="True" title="Serialization Guide" />
|
||||
<Topic id="SerializationAttributes" visible="True" title="Serialization Attributes" />
|
||||
<Topic id="SerializationCallbacks" visible="True" title="Serialization Callbacks" />
|
||||
<Topic id="SerializationErrorHandling" visible="True" title="Serialization Error Handling" />
|
||||
<Topic id="PreserveObjectReferences" visible="True" isSelected="true" title="Preserving Object Referencess" />
|
||||
<Topic id="CustomCreationConverter" visible="True" />
|
||||
<Topic id="ContractResolver" visible="True" />
|
||||
<Topic id="SerializingCollections" visible="True" title="Serializing Collections" />
|
||||
<Topic id="DatesInJSON" visible="True" title="Serializing Dates in JSON" />
|
||||
<Topic id="ReducingSerializedJSONSize" visible="True" title="Reducing Serialized JSON Size" />
|
||||
<Topic id="SerializingJSONFragments" visible="True" title="Serializing Partial JSON Fragments" />
|
||||
</Topic>
|
||||
<Topic id="LINQtoJSON" visible="True" isExpanded="true" title="LINQ to JSON">
|
||||
<Topic id="SelectToken" visible="True" title="Querying LINQ to JSON with SelectToken" />
|
||||
</Topic>
|
||||
<Topic id="ReadingWritingJSON" visible="True" title="Reading and Writing JSON" />
|
||||
<Topic id="ConvertingJSONandXML" visible="True" title="Converting between JSON and XML" />
|
||||
</Topics>
|
||||
+98
-27
@@ -19,7 +19,7 @@
|
||||
<HtmlHelpName>Documentation</HtmlHelpName>
|
||||
<ProjectSummary>
|
||||
</ProjectSummary>
|
||||
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter</MissingTags>
|
||||
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter, AutoDocumentDispose</MissingTags>
|
||||
<VisibleItems>InheritedMembers, InheritedFrameworkMembers, Protected, SealedProtected</VisibleItems>
|
||||
<HtmlHelp1xCompilerPath>
|
||||
</HtmlHelp1xCompilerPath>
|
||||
@@ -31,10 +31,10 @@
|
||||
</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>
|
||||
<HelpFileFormat>Website</HelpFileFormat>
|
||||
<FrameworkVersion>.NET 4.0.30319</FrameworkVersion>
|
||||
<HelpTitle>Json.NET - Documentation</HelpTitle>
|
||||
<PresentationStyle>vs2005</PresentationStyle>
|
||||
<NamingMethod>MemberName</NamingMethod>
|
||||
<DocumentationSourcePath>..\Src\Newtonsoft.Json\bin\Release</DocumentationSourcePath>
|
||||
<DocumentationSources>
|
||||
@@ -52,6 +52,71 @@
|
||||
<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>
|
||||
<RootNamespaceContainer>True</RootNamespaceContainer>
|
||||
<RootNamespaceTitle>API Reference</RootNamespaceTitle>
|
||||
<ComponentConfigurations>
|
||||
<ComponentConfig id="Post-transform Component" enabled="True">
|
||||
<component id="Post-transform Component" type="SandcastleBuilder.Components.PostTransformComponent" assembly="{@SHFBFolder}SandcastleBuilder.Components.dll">
|
||||
<!-- Code colorizer files (required).
|
||||
Attributes:
|
||||
Stylesheet file (required)
|
||||
Script file (required)
|
||||
"Copy" image file (required) -->
|
||||
<colorizer stylesheet="{@SHFBFolder}Colorizer\highlight.css" scriptFile="{@SHFBFolder}Colorizer\highlight.js" copyImage="{@SHFBFolder}Colorizer\CopyCode.gif" />
|
||||
<!-- Base output paths for the files (required). These should match the parent folder of the output path
|
||||
of the HTML files (see each of the SaveComponent instances in the configuration files). -->
|
||||
<outputPaths>
|
||||
{@HelpFormatOutputPaths}
|
||||
</outputPaths>
|
||||
<!-- Logo image file (optional). Filename is required. The height, width, altText, placement, and
|
||||
alignment attributes are optional. -->
|
||||
<logoFile filename="D:\Development\Source\Json\Newtonsoft.Json\Doc\logo.jpg" height="0" width="0" altText="Json.NET" placement="left" alignment="left" />
|
||||
</component>
|
||||
</ComponentConfig>
|
||||
<ComponentConfig id="Code Block Component" enabled="True"><component id="Code Block Component" type="SandcastleBuilder.Components.CodeBlockComponent" assembly="{@SHFBFolder}SandcastleBuilder.Components.dll">
|
||||
<basePath value="{@HtmlEncProjectFolder}" />
|
||||
<languageFilter value="true" />
|
||||
<allowMissingSource value="false" />
|
||||
<removeRegionMarkers value="false" />
|
||||
<colorizer syntaxFile="{@SHFBFolder}Colorizer\highlight.xml" styleFile="{@SHFBFolder}Colorizer\highlight.xsl" copyImageUrl="../icons/CopyCode.gif" language="cs" tabSize="0" numberLines="true" outlining="false" keepSeeTags="false" defaultTitle="true" />
|
||||
</component></ComponentConfig>
|
||||
<ComponentConfig id="Cached Reflection Index Data" enabled="True"><component id="Cached Reflection Index Data" type="SandcastleBuilder.Components.CachedCopyFromIndexComponent" assembly="{@SHFBFolder}SandcastleBuilder.Components.dll">
|
||||
<index name="reflection" value="/reflection/apis/api" key="@id" cache="10">
|
||||
<cache base="{@SandcastlePath}Data\Reflection" recurse="true" files="*.xml" cacheFile="{@LocalDataFolder}Cache\Reflection.cache" />
|
||||
<data files="reflection.xml" />
|
||||
</index>
|
||||
<copy name="reflection" source="*" target="/document/reference" />
|
||||
</component></ComponentConfig>
|
||||
<ComponentConfig id="Cached MSDN URL References" enabled="True"><component id="Cached MSDN URL References" type="SandcastleBuilder.Components.CachedResolveReferenceLinksComponent" assembly="{@SHFBFolder}SandcastleBuilder.Components.dll" locale="{@Locale}" linkTarget="{@SdkLinkTarget}">
|
||||
<helpOutput format="HtmlHelp1">
|
||||
<cache filename="{@LocalDataFolder}Cache\MsdnUrl.cache" />
|
||||
<targets base="{@SandcastlePath}Data\Reflection" recurse="true" files="*.xml" type="{@HtmlSdkLinkType}" />
|
||||
<targets files="reflection.xml" type="Local" />
|
||||
</helpOutput>
|
||||
<helpOutput format="MSHelp2">
|
||||
<cache filename="{@LocalDataFolder}Cache\MsdnUrl.cache" />
|
||||
<targets base="{@SandcastlePath}Data\Reflection" recurse="true" files="*.xml" type="{@MSHelp2SdkLinkType}" />
|
||||
<targets files="reflection.xml" type="Index" />
|
||||
</helpOutput>
|
||||
<helpOutput format="MSHelpViewer">
|
||||
<cache filename="{@LocalDataFolder}Cache\MsdnUrl.cache" />
|
||||
<targets base="{@SandcastlePath}Data\Reflection" recurse="true" files="*.xml" type="{@MSHelpViewerSdkLinkType}" />
|
||||
<targets files="reflection.xml" type="Id" />
|
||||
</helpOutput>
|
||||
<helpOutput format="Website">
|
||||
<cache filename="{@LocalDataFolder}Cache\MsdnUrl.cache" />
|
||||
<targets base="{@SandcastlePath}Data\Reflection" recurse="true" files="*.xml" type="{@WebsiteSdkLinkType}" />
|
||||
<targets files="reflection.xml" type="Local" />
|
||||
</helpOutput>
|
||||
</component></ComponentConfig>
|
||||
<ComponentConfig id="Cached Framework Comments Index Data" enabled="True"><component id="Cached Framework Comments Index Data" type="SandcastleBuilder.Components.CachedCopyFromIndexComponent" assembly="{@SHFBFolder}SandcastleBuilder.Components.dll">
|
||||
<index name="comments" value="/doc/members/member" key="@name" cache="100">
|
||||
{@CachedFrameworkCommentList}
|
||||
{@CommentFileList}
|
||||
</index>
|
||||
<copy name="comments" source="*" target="/document/comments" />
|
||||
</component></ComponentConfig></ComponentConfigurations>
|
||||
<IndentHtml>False</IndentHtml>
|
||||
</PropertyGroup>
|
||||
<!-- There are no properties for these two groups but they need to appear in
|
||||
order for Visual Studio to perform the build. -->
|
||||
@@ -60,30 +125,36 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<SiteMap Include="doc.sitemap" />
|
||||
<Content Include="donate.gif" />
|
||||
</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" />
|
||||
<None Include="CustomCreationConverter.aml" />
|
||||
<None Include="ContractResolver.aml" />
|
||||
<None Include="ConvertingJSONandXML.aml" />
|
||||
<None Include="ReadingWritingJSON.aml" />
|
||||
<None Include="SelectToken.aml" />
|
||||
<None Include="LINQtoJSON.aml" />
|
||||
<None Include="SerializingJSONFragments.aml" />
|
||||
<None Include="ReducingSerializedJSONSize.aml" />
|
||||
<None Include="DatesInJSON.aml" />
|
||||
<None Include="SerializingCollections.aml" />
|
||||
<None Include="PreserveObjectReferences.aml" />
|
||||
<None Include="SerializationErrorHandling.aml" />
|
||||
<None Include="SerializationCallbacks.aml" />
|
||||
<None Include="SerializationAttributes.aml" />
|
||||
<None Include="SerializationGuide.aml" />
|
||||
<None Include="SerializationSettings.aml" />
|
||||
<None Include="SerializingJSON.aml" />
|
||||
<None Include="Introduction.aml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ContentLayout Include="doc.content" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Image Include="logo.jpg">
|
||||
<ImageId>logo</ImageId>
|
||||
<AlternateText>logo</AlternateText>
|
||||
</Image>
|
||||
</ItemGroup>
|
||||
<!-- Import the SHFB build targets -->
|
||||
<Import Project="$(SHFBROOT)\SandcastleHelpFileBuilder.targets" />
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.3 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
-439
@@ -1,439 +0,0 @@
|
||||
/* page style */
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
background-color: #FFFFFF;
|
||||
padding: 0;
|
||||
font-size: 8.5pt;
|
||||
font-family: verdana, arial, sans-serif;
|
||||
color: #000000;
|
||||
}
|
||||
|
||||
html>body {
|
||||
margin: 0;
|
||||
background-color: #FFFFFF;
|
||||
padding: 0;
|
||||
font-size: 8.5pt;
|
||||
font-family: verdana, arial, sans-serif;
|
||||
color: #000000;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
table {
|
||||
/* this is a trick to force tables to inherit the body font size */
|
||||
font-size: 100%;
|
||||
}
|
||||
|
||||
/* non-scrolling (control) region style */
|
||||
|
||||
div#control {
|
||||
margin: 0;
|
||||
background-color: #D4DFFF;
|
||||
padding: 4px;
|
||||
width: 100%;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
z-index: 2;
|
||||
}
|
||||
|
||||
span.productTitle {
|
||||
font-size: 80%;
|
||||
}
|
||||
|
||||
span.topicTitle {
|
||||
font-size: 140%;
|
||||
font-weight: bold;
|
||||
color: #003399;
|
||||
}
|
||||
|
||||
span#chickenFeet {
|
||||
float: left;
|
||||
}
|
||||
|
||||
span#languageFilter {
|
||||
float: right;
|
||||
height: 1px;
|
||||
max-height: 1px;
|
||||
vertical-align: bottom;
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
/* scrolling (content) region style */
|
||||
|
||||
div#main
|
||||
{
|
||||
clear: both;
|
||||
margin: 0;
|
||||
padding: 1em;
|
||||
width: 100%;
|
||||
z-index: 1;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
/* sections */
|
||||
|
||||
div#header {
|
||||
font-size: 80%;
|
||||
color: #666666;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
|
||||
div.summary {
|
||||
margin-top: 0em;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
div.section {
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
div.sectionTitle {
|
||||
display: inline;
|
||||
font-size: 120%;
|
||||
font-weight: bold;
|
||||
color: #003399;
|
||||
}
|
||||
|
||||
div.sectionContent {
|
||||
margin-top: 0.2em;
|
||||
}
|
||||
|
||||
span.subsectionTitle {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
div#footer {
|
||||
margin-top: 1em;
|
||||
border-top: thin solid #003399;
|
||||
padding-top: 0.5em;
|
||||
}
|
||||
|
||||
div#footer p {
|
||||
margin-top: 0.2em;
|
||||
margin-bottom: 0.2em;
|
||||
}
|
||||
|
||||
/* authored content (block) */
|
||||
|
||||
p {
|
||||
margin-top: 1em;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
dl, ul, ol {
|
||||
margin-top: 0.5em;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
|
||||
pre {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: "Andale Mono", "Courier New", Courier, monospace;
|
||||
}
|
||||
|
||||
table.authoredTable {
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
table.authoredTable th {
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.authoredTable td {
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-width: 1px;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
div.alert {
|
||||
border: 1px solid #C8CDDE;
|
||||
background: #F7F7FF;
|
||||
}
|
||||
|
||||
div.media {
|
||||
text-align: center;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
|
||||
/* authored content (inline) */
|
||||
|
||||
span.keyword {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
span.code {
|
||||
font-family: "Andale Mono", "Courier New", Courier, monospace;
|
||||
font-size: 110%;
|
||||
color: #000066;
|
||||
}
|
||||
|
||||
/* auto-generated controls */
|
||||
|
||||
div.langTabs {
|
||||
/*width: 100%;*/
|
||||
}
|
||||
|
||||
div.langTab {
|
||||
float: left;
|
||||
width: 15%;
|
||||
border-top: 1px solid #C8CDDE;
|
||||
border-left: 1px solid #C8CDDE;
|
||||
border-right: 1px solid #C8CDDE;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
div.activeLangTab {
|
||||
float: left;
|
||||
width: 15%;
|
||||
border-top: 1px solid #C8CDDE;
|
||||
border-left: 1px solid #C8CDDE;
|
||||
border-right: 1px solid #C8CDDE;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.members {
|
||||
/* table-layout: fixed; */
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
table.members th.iconColumn {
|
||||
width: 60px;
|
||||
}
|
||||
|
||||
table.members th.nameColumn {
|
||||
width: 40%;
|
||||
}
|
||||
|
||||
table.members th.descriptionColumn {
|
||||
width: 60%;
|
||||
}
|
||||
|
||||
table.members th {
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.members td {
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-width: 1px;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
vertical-align: top;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
table.exceptions {
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
|
||||
table.exceptions th.exceptionNameColumn {
|
||||
width: 40%;
|
||||
}
|
||||
|
||||
table.exceptions th.exceptionConditionColumn {
|
||||
width: 60%;
|
||||
}
|
||||
|
||||
table.exceptions th {
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.exceptions td {
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-width: 1px;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
table.permissions {
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
|
||||
table.permissions th.permissionNameColumn {
|
||||
width: 40%;
|
||||
}
|
||||
|
||||
table.permissions th.permissionConditionColumn {
|
||||
width: 60%;
|
||||
}
|
||||
|
||||
table.permissions th {
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-width: 1px;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.permissions td {
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: #C8CDDE;
|
||||
border-bottom-width: 1px;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
span.obsolete {
|
||||
color: red;
|
||||
}
|
||||
|
||||
span.cs {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
span.vb {
|
||||
display: none;
|
||||
}
|
||||
|
||||
span.cpp {
|
||||
display: none;
|
||||
}
|
||||
|
||||
span.nu
|
||||
{
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* syntax styling */
|
||||
|
||||
div.code span.identifier {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
div.code span.keyword {
|
||||
color: green;
|
||||
}
|
||||
|
||||
div.code span.parameter {
|
||||
font-style: italic;
|
||||
color: purple;
|
||||
}
|
||||
|
||||
div.code span.literal {
|
||||
color: purple;
|
||||
}
|
||||
|
||||
div.code span.comment {
|
||||
color: red;
|
||||
}
|
||||
|
||||
span.foreignPhrase {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
span.placeholder {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
span.parameter
|
||||
{
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
span.typeparameter
|
||||
{
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
a {
|
||||
color: blue;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
MSHelp\:link {
|
||||
color: blue;
|
||||
hoverColor: #3366ff;
|
||||
}
|
||||
|
||||
span.nolink {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
span.selflink {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
table.filter {
|
||||
table-layout: fixed;
|
||||
}
|
||||
|
||||
tr.tabs td.tab {
|
||||
width: 10em;
|
||||
background: #F7F7FF;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: normal;
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
tr.tabs td.activeTab {
|
||||
width: 10em;
|
||||
background: #EFEFF7;
|
||||
padding: 0.2em;
|
||||
text-align: left;
|
||||
color: #000066;
|
||||
font-weight: bold;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
td.line {
|
||||
background: #EFEFF7;
|
||||
}
|
||||
|
||||
dt {
|
||||
font-weight: bold;
|
||||
margin-top: 10px;
|
||||
margin-left: 10px;
|
||||
}
|
||||
@@ -303,5 +303,16 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(1.1m, orderedValues[1]);
|
||||
Assert.AreEqual(1.2d, orderedValues[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteSingle()
|
||||
{
|
||||
float f = 5.2f;
|
||||
JValue value = new JValue(f);
|
||||
|
||||
string json = value.ToString(Formatting.None);
|
||||
|
||||
Assert.AreEqual("5.2", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,6 +116,10 @@
|
||||
<Compile Include="Converters\ObjectIdConverterTests.cs" />
|
||||
<Compile Include="Converters\StringEnumConverterTests.cs" />
|
||||
<Compile Include="Converters\VersionConverterTests.cs" />
|
||||
<Compile Include="Documentation\ConvertingJsonAndXmlTests.cs" />
|
||||
<Compile Include="Documentation\ReadingAndWritingJsonTests.cs" />
|
||||
<Compile Include="Documentation\LinqToJsonTests.cs" />
|
||||
<Compile Include="Documentation\SerializationTests.cs" />
|
||||
<Compile Include="FileSystemEntityModel.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
|
||||
@@ -72,5 +72,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.7.15115")]
|
||||
[assembly: AssemblyFileVersion("4.5.7.15121")]
|
||||
#endif
|
||||
|
||||
@@ -464,7 +464,7 @@ namespace Newtonsoft.Json.Bson
|
||||
/// <summary>
|
||||
/// Writes a <see cref="T:Byte[]"/> value that represents a BSON object id.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="value">The Object ID value to write.</param>
|
||||
public void WriteObjectId(byte[] value)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(value, "value");
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// <summary>
|
||||
/// Create a custom object
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="T">The object type to convert.</typeparam>
|
||||
public abstract class CustomCreationConverter<T> : JsonConverter
|
||||
{
|
||||
/// <summary>
|
||||
@@ -70,7 +70,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// Creates an object which will then be populated by the serializer.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <returns></returns>
|
||||
/// <returns>The created object.</returns>
|
||||
public abstract T Create(Type objectType);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -37,9 +37,6 @@ using System.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts an <see cref="Enum"/> to and from its name string value.
|
||||
/// </summary>
|
||||
/// <summary>
|
||||
/// Converts an <see cref="Enum"/> to and from its name string value.
|
||||
/// </summary>
|
||||
|
||||
@@ -1167,6 +1167,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <summary>
|
||||
/// Creates the specified .NET type from the <see cref="JToken"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The object type that the token will be deserialized to.</typeparam>
|
||||
/// <returns>The new object created from the JSON value.</returns>
|
||||
public T ToObject<T>()
|
||||
{
|
||||
@@ -1176,6 +1177,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <summary>
|
||||
/// Creates the specified .NET type from the <see cref="JToken"/> using the specified <see cref="JsonSerializer"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The object type that the token will be deserialized to.</typeparam>
|
||||
/// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used when creating the object.</param>
|
||||
/// <returns>The new object created from the JSON value.</returns>
|
||||
public T ToObject<T>(JsonSerializer jsonSerializer)
|
||||
|
||||
@@ -85,6 +85,15 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JValue"/> class with the given value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
public JValue(float value)
|
||||
: this(value, JTokenType.Float)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JValue"/> class with the given value.
|
||||
/// </summary>
|
||||
@@ -531,7 +540,10 @@ namespace Newtonsoft.Json.Linq
|
||||
writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture));
|
||||
return;
|
||||
case JTokenType.Float:
|
||||
writer.WriteValue(Convert.ToDouble(_value, CultureInfo.InvariantCulture));
|
||||
if (_value is float)
|
||||
writer.WriteValue(_value);
|
||||
else
|
||||
writer.WriteValue(Convert.ToDouble(_value, CultureInfo.InvariantCulture));
|
||||
return;
|
||||
case JTokenType.String:
|
||||
writer.WriteValue((_value != null) ? _value.ToString() : null);
|
||||
|
||||
@@ -90,7 +90,7 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.7.15115")]
|
||||
[assembly: AssemblyFileVersion("4.5.7.15121")]
|
||||
#endif
|
||||
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
@@ -486,7 +486,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// Resolves the default <see cref="JsonConverter" /> for the contract.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <returns></returns>
|
||||
/// <returns>The contract's default <see cref="JsonConverter" />.</returns>
|
||||
protected virtual JsonConverter ResolveContractConverter(Type objectType)
|
||||
{
|
||||
return JsonTypeReflector.GetJsonConverter(objectType.GetCustomAttributeProvider(), objectType);
|
||||
|
||||
@@ -28,5 +28,6 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// <summary>
|
||||
/// Represents a method that constructs an object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The object type to create.</typeparam>
|
||||
public delegate object ObjectConstructor<T>(params object[] args);
|
||||
}
|
||||
Reference in New Issue
Block a user