-Fixed ToString culture inconsistence when serializing dictionary key

-Documentation
This commit is contained in:
JamesNK
2009-12-08 22:27:13 +00:00
parent 52279e610b
commit 2e54bf69ab
12 changed files with 736 additions and 13 deletions
+146
View File
@@ -0,0 +1,146 @@
<html>
<head>
<title>Converting between JSON and XML</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<link href="custom.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="control">
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
<span class="topicTitle">Converting between JSON and XML</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>Json.NET supports converting JSON to XML and vice versa using the
<a href="./html/T_Newtonsoft_Json_Converters_XmlNodeConverter.htm">XmlNodeConverter</a>.</p>
<p>
Elements, attributes, text, comments, character data, processing instructions,
namespaces and the XML declaration are all preserved when converting between the
two. The only caveat is that it is possible to lose the order of differently
named nodes at the same level when they are grouped together into an array.</p>
<h3>Conversion Rules</h3>
<ul>
<li>Elements remain unchanged. </li>
<li>Attributes are prefixed with an @. </li>
<li>Single child text nodes are a value directly against an element, otherwise they
are accessed via #text. </li>
<li>The XML declaration and processing instructions are prefixed with ?. </li>
<li>Charater data, comments, whitespace and significate whitespace nodes are
accessed via #cdata-section,&nbsp;#comment, #whitespace and #significate-whitespace
respectively. </li>
<li>Multiple nodes with the same name at the same level are grouped together into an
array. </li>
<li>Empty elements are null.</li>
</ul>
<h3>SerializeXmlNode</h3>
<p>The JsonConvert has two helper methods for converting between JSON and XML. The first is <a href="./html/M_Newtonsoft_Json_JsonConvert_SerializeXmlNode.htm">SerializeXmlNode</a>. This method takes an
XmlNode and serializes it to JSON text.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">string</span> xml = <span style="color: #a31515;">@&quot;&lt;?xml version=&quot;&quot;1.0&quot;&quot; standalone=&quot;&quot;no&quot;&quot;?&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&lt;root&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;person id=&quot;&quot;1&quot;&quot;&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;name&gt;Alan&lt;/name&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;url&gt;http://www.google.com&lt;/url&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;/person&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;person id=&quot;&quot;2&quot;&quot;&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;name&gt;Louis&lt;/name&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;url&gt;http://www.yahoo.com&lt;/url&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &lt;/person&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&lt;/root&gt;&quot;</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">XmlDocument</span> doc = <span style="color: blue;">new</span> <span style="color: #2b91af;">XmlDocument</span>();</pre>
<pre style="margin: 0px;">doc.LoadXml(xml);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> jsonText = <span style="color: #2b91af;">JsonConvert</span>.SerializeXmlNode(doc);</pre>
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &quot;?xml&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &quot;@version&quot;: &quot;1.0&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &quot;@standalone&quot;: &quot;no&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &quot;root&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &quot;person&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;@id&quot;: &quot;1&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot;: &quot;Alan&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;url&quot;: &quot;http://www.google.com&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;@id&quot;: &quot;2&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot;: &quot;Louis&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; &nbsp; &quot;url&quot;: &quot;http://www.yahoo.com&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
</div>
</div>
</div>
<h3>DeserializeXmlNode</h3>
<p>The second helper method on JsonConvert is <a href="./html/M_Newtonsoft_Json_JsonConvert_DeserializeXmlNode.htm">DeserializeXmlNode</a>. This method takes
JSON text and deserializes it into a XmlNode.</p>
<p>Because valid XML must have one root element the JSON passed to
DeserializeXmlNode should have one property in the root JSON object. If the root
JSON object has multiple properties then the overload that also takes an element
name should be used. A root element with that name will be inserted into the
deserialized XmlNode.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #a31515;">@&quot;{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;?xml&quot;&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;@version&quot;&quot;: &quot;&quot;1.0&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;@standalone&quot;&quot;: &quot;&quot;no&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;root&quot;&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;person&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;@id&quot;&quot;: &quot;&quot;1&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;name&quot;&quot;: &quot;&quot;Alan&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;url&quot;&quot;: &quot;&quot;http://www.google.com&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;@id&quot;&quot;: &quot;&quot;2&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;name&quot;&quot;: &quot;&quot;Louis&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;url&quot;&quot;: &quot;&quot;http://www.yahoo.com&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">}&quot;</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">XmlDocument</span> doc = (<span style="color: #2b91af;">XmlDocument</span>)<span style="color: #2b91af;">JsonConvert</span>.DeserializeXmlNode(json);</pre>
<pre style="margin: 0px;"><span style="color: green;">// &lt;?xml version=&quot;1.0&quot; standalone=&quot;no&quot;?&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// &lt;root&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;person id=&quot;1&quot;&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;name&gt;Alan&lt;/name&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;url&gt;http://www.google.com&lt;/url&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;/person&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;person id=&quot;2&quot;&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;name&gt;Louis&lt;/name&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;url&gt;http://www.yahoo.com&lt;/url&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp; &lt;/person&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// &lt;/root&gt;</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
+106
View File
@@ -0,0 +1,106 @@
<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 much easier because the entire
query is defined in a string.</p>
<h3>SelectToken</h3>
<p>SelectToken is a method on JToken and takes a string path to a child token.
SelectToken returns the child token or a null reference if a token couldn&#39;t be
found at the path&#39;s location.</p>
<p>The
path is made up of property names and array indexes separated by periods. Array indexes
can use either square or round brackets. Both
of the following are valid paths and are equivilent to each other: <code>Manufacturers[0].Name</code>
and <code>Manufacturers(0).Name</code>.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> o = <span style="color: #2b91af;">JObject</span>.Parse(<span style="color: #a31515;">@&quot;{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;Stores&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;Lambton Quay&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;Willis Street&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ],</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;Manufacturers&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Acme Co&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Products&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Anvil&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 50</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Contoso&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;Products&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Elbow Grease&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 99.95</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Name&quot;&quot;: &quot;&quot;Headlight Fluid&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;Price&quot;&quot;: 4</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; ]</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">}&quot;</span>);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> name = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[0].Name&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Acme Co</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> productPrice = (<span style="color: blue;">decimal</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[0].Products[0].Price&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// 50</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">string</span> productName = (<span style="color: blue;">string</span>)o.SelectToken(<span style="color: #a31515;">&quot;Manufacturers[1].Products[0].Name&quot;</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// Elbow Grease</span></pre>
</div>
</div>
</div>
<h3>SelectToken with LINQ</h3>
<p>SelectToken can be used in combination with standard LINQ methods.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span>&lt;<span style="color: blue;">string</span>&gt; storeNames = o.SelectToken(<span style="color: #a31515;">&quot;Stores&quot;</span>).Select(s =&gt; (<span style="color: blue;">string</span>)s).ToList();</pre>
<pre style="margin: 0px;"><span style="color: green;">// Lambton Quay</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Willis Street</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span>&lt;<span style="color: blue;">string</span>&gt; firstProductNames = o[<span style="color: #a31515;">&quot;Manufacturers&quot;</span>].Select(m =&gt; (<span style="color: blue;">string</span>)m.SelectToken(<span style="color: #a31515;">&quot;Products[1].Name&quot;</span>)).ToList();</pre>
<pre style="margin: 0px;"><span style="color: green;">// null</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Headlight Fluid</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">decimal</span> totalPrice = o[<span style="color: #a31515;">&quot;Manufacturers&quot;</span>].Sum(m =&gt; (<span style="color: blue;">decimal</span>)m.SelectToken(<span style="color: #a31515;">&quot;Products[0].Price&quot;</span>));</pre>
<pre style="margin: 0px;"><span style="color: green;">// 149.95</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
+125
View File
@@ -0,0 +1,125 @@
<html>
<head>
<title>Serializing Partial JSON Fragments</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<link href="custom.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="control">
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
<span class="topicTitle">Serializing Partial JSON Fragments</span></div>
<div id="content">
<span style="color: DarkGray"> </span>
<p>Often when working with large JSON documents you're only interested in a small fragment of information.
This scenario can be annoying when you want to serialize that Json.NET into .NET
objects because you have to define .NET classes for the entire JSON result.</p>
<p>With Json.NET it is easy to get around this problem. Using LINQ to JSON you can
extract the pieces of JSON you want to serialize before passing them to the Json.NET serializer.</p>
<div class="overflowpanel">
<div class="code">
<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SearchResult</span></pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Title { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Content { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Url { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
</div>
<div class="overflowpanel">
<div class="code">
<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">string</span> googleSearchText = <span style="color: #a31515;">@&quot;{</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;responseData&quot;&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;results&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;GsearchResultClass&quot;&quot;: &quot;&quot;GwebSearch&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;unescapedUrl&quot;&quot;: &quot;&quot;http://en.wikipedia.org/wiki/Paris_Hilton&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;url&quot;&quot;: &quot;&quot;http://en.wikipedia.org/wiki/Paris_Hilton&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;visibleUrl&quot;&quot;: &quot;&quot;en.wikipedia.org&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;cacheUrl&quot;&quot;: &quot;&quot;http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;title&quot;&quot;: &quot;&quot;&lt;b&gt;Paris Hilton&lt;/b&gt; - Wikipedia, the free encyclopedia&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;titleNoFormatting&quot;&quot;: &quot;&quot;Paris Hilton - Wikipedia, the free encyclopedia&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;content&quot;&quot;: &quot;&quot;[1] In 2006, she released her debut album...&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;GsearchResultClass&quot;&quot;: &quot;&quot;GwebSearch&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;unescapedUrl&quot;&quot;: &quot;&quot;http://www.imdb.com/name/nm0385296/&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;url&quot;&quot;: &quot;&quot;http://www.imdb.com/name/nm0385296/&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;visibleUrl&quot;&quot;: &quot;&quot;www.imdb.com&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;cacheUrl&quot;&quot;: &quot;&quot;http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;title&quot;&quot;: &quot;&quot;&lt;b&gt;Paris Hilton&lt;/b&gt;&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;titleNoFormatting&quot;&quot;: &quot;&quot;Paris Hilton&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;content&quot;&quot;: &quot;&quot;Self: Zoolander. Socialite &lt;b&gt;Paris Hilton&lt;/b&gt;...&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; ],</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &quot;&quot;cursor&quot;&quot;: {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;pages&quot;&quot;: [</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;start&quot;&quot;: &quot;&quot;0&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;label&quot;&quot;: 1</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;start&quot;&quot;: &quot;&quot;4&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;label&quot;&quot;: 2</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;start&quot;&quot;: &quot;&quot;8&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;label&quot;&quot;: 3</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; {</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;start&quot;&quot;: &quot;&quot;12&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;label&quot;&quot;: 4</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; ],</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;estimatedResultCount&quot;&quot;: &quot;&quot;59600000&quot;&quot;,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;currentPageIndex&quot;&quot;: 0,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; &nbsp; &quot;&quot;moreResultsUrl&quot;&quot;: &quot;&quot;http://www.google.com/search?oe=utf8&amp;ie=utf8...&quot;&quot;</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &nbsp; }</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; },</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;responseDetails&quot;&quot;: null,</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">&nbsp; &quot;&quot;responseStatus&quot;&quot;: 200</span></pre>
<pre style="margin: 0px;"><span style="color: #a31515;">}&quot;</span>;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> googleSearch = <span style="color: #2b91af;">JObject</span>.Parse(googleSearchText);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// get JSON result objects into a list</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span>&lt;<span style="color: #2b91af;">JToken</span>&gt; results = googleSearch[<span style="color: #a31515;">&quot;responseData&quot;</span>][<span style="color: #a31515;">&quot;results&quot;</span>].Children().ToList();</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// serialize JSON results into .NET objects</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span>&lt;<span style="color: #2b91af;">SearchResult</span>&gt; searchResults = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">SearchResult</span>&gt;();</pre>
<pre style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: #2b91af;">JToken</span> result <span style="color: blue;">in</span> results)</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: #2b91af;">SearchResult</span> searchResult = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject&lt;<span style="color: #2b91af;">SearchResult</span>&gt;(result.ToString());</pre>
<pre style="margin: 0px;">&nbsp; searchResults.Add(searchResult);</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// Title = &lt;b&gt;Paris Hilton&lt;/b&gt; - Wikipedia, the free encyclopedia</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Content = [1] In 2006, she released her debut album...</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Url = http://en.wikipedia.org/wiki/Paris_Hilton</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// Title = &lt;b&gt;Paris Hilton&lt;/b&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Content = Self: Zoolander. Socialite &lt;b&gt;Paris Hilton&lt;/b&gt;...</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// Url = http://www.imdb.com/name/nm0385296/</span></pre>
</div>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
+17 -10
View File
@@ -36,17 +36,20 @@
<PresentationStyle>Prototype</PresentationStyle>
<NamingMethod>MemberName</NamingMethod>
<DocumentationSources>
<DocumentationSource sourceFile="..\..\..\..\..\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\bin\Release\Newtonsoft.Json.dll" xmlns="" />
<DocumentationSource sourceFile="..\..\..\..\..\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\bin\Release\Newtonsoft.Json.xml" xmlns="" /></DocumentationSources>
<DocumentationSource sourceFile="..\Newtonsoft.Json\bin\Release\Newtonsoft.Json.dll" />
<DocumentationSource sourceFile="..\Newtonsoft.Json\bin\Release\Newtonsoft.Json.xml" />
</DocumentationSources>
<NamespaceSummaries>
<NamespaceSummaryItem name="(global)" isDocumented="False" xmlns="" />
<NamespaceSummaryItem name="Newtonsoft.Json" isDocumented="True" xmlns="">The &lt;b&gt;Newtonsoft.Json&lt;/b&gt; namespace provides classes that are used to implement the core services of the framework.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True" xmlns="">The &lt;b&gt;Newtonsoft.Json.Converters&lt;/b&gt; namespace provides classes that inherit from &lt;a href="T_Newtonsoft_Json_JsonConverter.htm"&gt;JsonConverter&lt;/a&gt;.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True" xmlns="">The &lt;b&gt;Newtonsoft.Json.Linq&lt;/b&gt; namespace provides classes that are used to implement LINQ to JSON.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Linq.ComponentModel" isDocumented="True" xmlns="">The &lt;b&gt;Newtonsoft.Json.Linq.ComponentModel&lt;/b&gt; namespace provides classes for LINQ to JSON databinding.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True" xmlns="">The &lt;b&gt;Newtonsoft.Json.Schema&lt;/b&gt; namespace provides classes that are used to implement JSON schema.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True" xmlns="">The &lt;b&gt;Newtonsoft.Json.Linq&lt;/b&gt; namespace provides classes that are used when serializing and deserializing JSON.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Utilities" isDocumented="False" xmlns="" /></NamespaceSummaries>
<NamespaceSummaryItem name="(global)" isDocumented="False" />
<NamespaceSummaryItem name="Newtonsoft.Json" isDocumented="True">The &lt;b&gt;Newtonsoft.Json&lt;/b&gt; namespace provides classes that are used to implement the core services of the framework.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Converters&lt;/b&gt; namespace provides classes that inherit from &lt;a href="T_Newtonsoft_Json_JsonConverter.htm"&gt;JsonConverter&lt;/a&gt;.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Linq&lt;/b&gt; namespace provides classes that are used to implement LINQ to JSON.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Linq.ComponentModel" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Linq.ComponentModel&lt;/b&gt; namespace provides classes for LINQ to JSON databinding.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Schema&lt;/b&gt; namespace provides classes that are used to implement JSON schema.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Linq&lt;/b&gt; namespace provides classes that are used when serializing and deserializing JSON.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Newtonsoft.Json.Utilities" isDocumented="False" />
<NamespaceSummaryItem name="Newtonsoft.Json.Bson" isDocumented="True">The &lt;b&gt;Newtonsoft.Json.Linq&lt;/b&gt; namespace provides classes that are used to implement BSON.</NamespaceSummaryItem>
</NamespaceSummaries>
</PropertyGroup>
<!-- There are no properties for these two groups but they need to appear in
order for Visual Studio to perform the build. -->
@@ -61,6 +64,10 @@
<Content Include="CustomCreationConverter.html" />
<Content Include="ContractResolver.html" />
<Content Include="custom.css" />
<Content Include="ConvertingJSONandXML.html" />
<Content Include="SerializingJSONFragments.html" />
<Content Include="SelectToken.html" />
<Content Include="ReducingSerializedJSONSize.html" />
<Content Include="donate.gif" />
<Content Include="styles.css" />
<Content Include="ReadingWritingJSON.html" />
+5 -1
View File
@@ -11,7 +11,11 @@
<siteMapNode title="CustomCreationConverter" url="CustomCreationConverter.html" />
<siteMapNode title="Contract Resolvers" url="ContractResolver.html" />
<siteMapNode title="Reducing Serialized JSON Size" url="ReducingSerializedJSONSize.html" />
<siteMapNode title="Serializing Partial JSON Fragments" url="SerializingJSONFragments.html" />
</siteMapNode>
<siteMapNode title="LINQ to JSON" url="LINQtoJSON.html">
<siteMapNode title="Querying LINQ to JSON with SelectToken" url="SelectToken.html" />
</siteMapNode>
<siteMapNode title="LINQ to JSON" url="LINQtoJSON.html" />
<siteMapNode title="Reading and writing JSON" url="ReadingWritingJSON.html" />
<siteMapNode title="Converting between JSON and XML" url="ConvertingJSONandXML.html" />
</siteMap>
@@ -552,6 +552,124 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(@"<span class=""vevent""><a class=""url"" href=""http://www.web2con.com/"">Text</a><!-- Hi! --></span>", newDoc.InnerXml);
}
[Test]
public void SerializeExample()
{
string xml = @"<?xml version=""1.0"" standalone=""no""?>
<root>
<person id=""1"">
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id=""2"">
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
// {
// "?xml": {
// "@version": "1.0",
// "@standalone": "no"
// },
// "root": {
// "person": [
// {
// "@id": "1",
// "name": "Alan",
// "url": "http://www.google.com"
// },
// {
// "@id": "2",
// "name": "Louis",
// "url": "http://www.yahoo.com"
// }
// ]
// }
// }
// format
jsonText = JObject.Parse(jsonText).ToString();
Assert.AreEqual(@"{
""?xml"": {
""@version"": ""1.0"",
""@standalone"": ""no""
},
""root"": {
""person"": [
{
""@id"": ""1"",
""name"": ""Alan"",
""url"": ""http://www.google.com""
},
{
""@id"": ""2"",
""name"": ""Louis"",
""url"": ""http://www.yahoo.com""
}
]
}
}", jsonText);
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
}
[Test]
public void DeserializeExample()
{
string json = @"{
""?xml"": {
""@version"": ""1.0"",
""@standalone"": ""no""
},
""root"": {
""person"": [
{
""@id"": ""1"",
""name"": ""Alan"",
""url"": ""http://www.google.com""
},
{
""@id"": ""2"",
""name"": ""Louis"",
""url"": ""http://www.yahoo.com""
}
]
}
}";
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
// <?xml version="1.0" standalone="no"?>
// <root>
// <person id="1">
// <name>Alan</name>
// <url>http://www.google.com</url>
// </person>
// <person id="2">
// <name>Louis</name>
// <url>http://www.yahoo.com</url>
// </person>
// </root>
Assert.AreEqual(@"<?xml version=""1.0"" standalone=""no""?>
<root>
<person id=""1"">
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id=""2"">
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>".Replace(Environment.NewLine, string.Empty), doc.InnerXml);
}
}
}
#endif
@@ -415,5 +415,19 @@ Parameter name: index")]
Assert.AreEqual(3, (int)a[key]);
}
[Test]
public void ReplaceAll()
{
JArray a = new JArray(new [] { 1, 2, 3 });
Assert.AreEqual(3, a.Count);
Assert.AreEqual(1, (int)a[0]);
Assert.AreEqual(2, (int)a[1]);
Assert.AreEqual(3, (int)a[2]);
a.ReplaceAll(1);
Assert.AreEqual(1, a.Count);
Assert.AreEqual(1, (int)a[0]);
}
}
}
@@ -242,5 +242,72 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(JTokenType.Integer, t.Type);
Assert.AreEqual(3, (int)t);
}
[Test]
public void Example()
{
JObject o = JObject.Parse(@"{
""Stores"": [
""Lambton Quay"",
""Willis Street""
],
""Manufacturers"": [
{
""Name"": ""Acme Co"",
""Products"": [
{
""Name"": ""Anvil"",
""Price"": 50
}
]
},
{
""Name"": ""Contoso"",
""Products"": [
{
""Name"": ""Elbow Grease"",
""Price"": 99.95
},
{
""Name"": ""Headlight Fluid"",
""Price"": 4
}
]
}
]
}");
string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
// 50
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease
Assert.AreEqual("Acme Co", name);
Assert.AreEqual(50m, productPrice);
Assert.AreEqual("Elbow Grease", productName);
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
// Lambton Quay
// Willis Street
IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
// null
// Headlight Fluid
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
// 149.95
Assert.AreEqual(2, storeNames.Count);
Assert.AreEqual("Lambton Quay", storeNames[0]);
Assert.AreEqual("Willis Street", storeNames[1]);
Assert.AreEqual(2, firstProductNames.Count);
Assert.AreEqual(null, firstProductNames[0]);
Assert.AreEqual("Headlight Fluid", firstProductNames[1]);
Assert.AreEqual(149.95m, totalPrice);
}
}
}
@@ -1993,5 +1993,118 @@ keyword such as type of business.""
Assert.AreEqual(1, newFoo.Bars[1].Id);
Assert.AreEqual(2, newFoo.Bars[2].Id);
}
[Test]
public void SerializeGuidKeyedDictionary()
{
Dictionary<Guid, int> dictionary = new Dictionary<Guid, int>();
dictionary.Add(new Guid("F60EAEE0-AE47-488E-B330-59527B742D77"), 1);
dictionary.Add(new Guid("C2594C02-EBA1-426A-AA87-8DD8871350B0"), 2);
string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);
Assert.AreEqual(@"{
""f60eaee0-ae47-488e-b330-59527b742d77"": 1,
""c2594c02-eba1-426a-aa87-8dd8871350b0"": 2
}", json);
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Could not create string property name from type 'Newtonsoft.Json.Tests.TestObjects.Person'.")]
public void SerializePersonKeyedDictionary()
{
Dictionary<Person, int> dictionary = new Dictionary<Person, int>();
dictionary.Add(new Person { Name = "p1" }, 1);
dictionary.Add(new Person { Name = "p2" }, 2);
JsonConvert.SerializeObject(dictionary, Formatting.Indented);
}
public class SearchResult
{
public string Title { get; set; }
public string Content { get; set; }
public string Url { get; set; }
}
[Test]
public void SerializeFragment()
{
string googleSearchText = @"{
""responseData"": {
""results"": [
{
""GsearchResultClass"": ""GwebSearch"",
""unescapedUrl"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",
""url"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",
""visibleUrl"": ""en.wikipedia.org"",
""cacheUrl"": ""http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org"",
""title"": ""<b>Paris Hilton</b> - Wikipedia, the free encyclopedia"",
""titleNoFormatting"": ""Paris Hilton - Wikipedia, the free encyclopedia"",
""content"": ""[1] In 2006, she released her debut album...""
},
{
""GsearchResultClass"": ""GwebSearch"",
""unescapedUrl"": ""http://www.imdb.com/name/nm0385296/"",
""url"": ""http://www.imdb.com/name/nm0385296/"",
""visibleUrl"": ""www.imdb.com"",
""cacheUrl"": ""http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com"",
""title"": ""<b>Paris Hilton</b>"",
""titleNoFormatting"": ""Paris Hilton"",
""content"": ""Self: Zoolander. Socialite <b>Paris Hilton</b>...""
}
],
""cursor"": {
""pages"": [
{
""start"": ""0"",
""label"": 1
},
{
""start"": ""4"",
""label"": 2
},
{
""start"": ""8"",
""label"": 3
},
{
""start"": ""12"",
""label"": 4
}
],
""estimatedResultCount"": ""59600000"",
""currentPageIndex"": 0,
""moreResultsUrl"": ""http://www.google.com/search?oe=utf8&ie=utf8...""
}
},
""responseDetails"": null,
""responseStatus"": 200
}";
JObject googleSearch = JObject.Parse(googleSearchText);
// get JSON result objects into a list
IList<JToken> results = googleSearch["responseData"]["results"].Children().ToList();
// serialize JSON results into .NET objects
IList<SearchResult> searchResults = new List<SearchResult>();
foreach (JToken result in results)
{
SearchResult searchResult = JsonConvert.DeserializeObject<SearchResult>(result.ToString());
searchResults.Add(searchResult);
}
// Title = <b>Paris Hilton</b> - Wikipedia, the free encyclopedia
// Content = [1] In 2006, she released her debut album...
// Url = http://en.wikipedia.org/wiki/Paris_Hilton
// Title = <b>Paris Hilton</b>
// Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...
// Url = http://www.imdb.com/name/nm0385296/
Assert.AreEqual(2, searchResults.Count);
Assert.AreEqual("<b>Paris Hilton</b> - Wikipedia, the free encyclopedia", searchResults[0].Title);
Assert.AreEqual("<b>Paris Hilton</b>", searchResults[1].Title);
}
}
}
@@ -25,8 +25,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests
@@ -34,6 +36,14 @@ namespace Newtonsoft.Json.Tests
[TestFixture]
public abstract class TestFixtureBase
{
[TestFixtureSetUp]
protected void TestSetup()
{
//CultureInfo turkey = CultureInfo.CreateSpecificCulture("tr");
//Thread.CurrentThread.CurrentCulture = turkey;
//Thread.CurrentThread.CurrentUICulture = turkey;
}
//public string GetOffset(DateTime value)
//{
// TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime());
+2 -1
View File
@@ -17,7 +17,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Doc", "Doc", "{8F9E13D9-418
Doc\custom.css = Doc\custom.css
Doc\CustomCreationConverter.html = Doc\CustomCreationConverter.html
Doc\DatesInJSON.html = Doc\DatesInJSON.html
doc.shfbproj = doc.shfbproj
Doc\doc.sitemap = Doc\doc.sitemap
Doc\donate.gif = Doc\donate.gif
Doc\Introduction.html = Doc\Introduction.html
@@ -25,10 +24,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Doc", "Doc", "{8F9E13D9-418
Doc\PreserveObjectReferences.html = Doc\PreserveObjectReferences.html
Doc\ReadingWritingJSON.html = Doc\ReadingWritingJSON.html
Doc\ReducingSerializedJSONSize.html = Doc\ReducingSerializedJSONSize.html
Doc\SelectToken.html = Doc\SelectToken.html
Doc\SerializationAttributes.html = Doc\SerializationAttributes.html
Doc\SerializationErrorHandling.html = Doc\SerializationErrorHandling.html
Doc\SerializingCollections.html = Doc\SerializingCollections.html
Doc\SerializingJSON.html = Doc\SerializingJSON.html
Doc\SerializingJSONFragments.html = Doc\SerializingJSONFragments.html
Doc\styles.css = Doc\styles.css
EndProjectSection
EndProject
@@ -441,7 +441,7 @@ namespace Newtonsoft.Json.Serialization
foreach (DictionaryEntry entry in values)
{
string propertyName = entry.Key.ToString();
string propertyName = GetPropertyName(entry);
try
{
@@ -477,6 +477,18 @@ namespace Newtonsoft.Json.Serialization
contract.InvokeOnSerialized(values);
}
private string GetPropertyName(DictionaryEntry entry)
{
string propertyName;
if (entry.Key is IConvertible)
return Convert.ToString(entry.Key, CultureInfo.InvariantCulture);
else if (TryConvertToString(entry.Key, entry.Key.GetType(), out propertyName))
return propertyName;
throw new JsonSerializationException("Could not create string property name from type '{0}'.".FormatWith(CultureInfo.InvariantCulture, entry.Key.GetType()));
}
private void HandleError(JsonWriter writer, int initialDepth)
{
ClearErrorContext();