-Fixed ToString culture inconsistence when serializing dictionary key
-Documentation
This commit is contained in:
@@ -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, #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,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'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 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;">@"{</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,125 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Serializing Partial JSON Fragments</title>
|
||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
||||
<link href="custom.css" rel="stylesheet" type="text/css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div id="control">
|
||||
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
||||
<span class="topicTitle">Serializing Partial JSON Fragments</span></div>
|
||||
|
||||
<div id="content">
|
||||
<span style="color: DarkGray"> </span>
|
||||
<p>Often when working with large JSON documents you're only interested in a small fragment of information.
|
||||
This scenario can be annoying when you want to serialize that Json.NET into .NET
|
||||
objects because you have to define .NET classes for the entire JSON result.</p>
|
||||
<p>With Json.NET it is easy to get around this problem. Using LINQ to JSON you can
|
||||
extract the pieces of JSON you want to serialize before passing them to the Json.NET serializer.</p>
|
||||
|
||||
<div class="overflowpanel">
|
||||
<div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SearchResult</span></pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Title { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Content { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Url { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="overflowpanel">
|
||||
<div class="code">
|
||||
|
||||
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
||||
<pre style="margin: 0px;"><span style="color: blue;">string</span> googleSearchText = <span style="color: #a31515;">@"{</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseData"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""results"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""GsearchResultClass"": ""GwebSearch"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""unescapedUrl"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://en.wikipedia.org/wiki/Paris_Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""visibleUrl"": ""en.wikipedia.org"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cacheUrl"": ""http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""title"": ""<b>Paris Hilton</b> - Wikipedia, the free encyclopedia"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""titleNoFormatting"": ""Paris Hilton - Wikipedia, the free encyclopedia"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""content"": ""[1] In 2006, she released her debut album...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""GsearchResultClass"": ""GwebSearch"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""unescapedUrl"": ""http://www.imdb.com/name/nm0385296/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.imdb.com/name/nm0385296/"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""visibleUrl"": ""www.imdb.com"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cacheUrl"": ""http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""title"": ""<b>Paris Hilton</b>"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""titleNoFormatting"": ""Paris Hilton"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""content"": ""Self: Zoolander. Socialite <b>Paris Hilton</b>...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""cursor"": {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""pages"": [</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""0"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 1</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""4"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 2</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""8"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 3</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""start"": ""12"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""label"": 4</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ],</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""estimatedResultCount"": ""59600000"",</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""currentPageIndex"": 0,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""moreResultsUrl"": ""http://www.google.com/search?oe=utf8&ie=utf8...""</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseDetails"": null,</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;"> ""responseStatus"": 200</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> googleSearch = <span style="color: #2b91af;">JObject</span>.Parse(googleSearchText);</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// get JSON result objects into a list</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span><<span style="color: #2b91af;">JToken</span>> results = googleSearch[<span style="color: #a31515;">"responseData"</span>][<span style="color: #a31515;">"results"</span>].Children().ToList();</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// serialize JSON results into .NET objects</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: #2b91af;">IList</span><<span style="color: #2b91af;">SearchResult</span>> searchResults = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span><<span style="color: #2b91af;">SearchResult</span>>();</pre>
|
||||
<pre style="margin: 0px;"><span style="color: blue;">foreach</span> (<span style="color: #2b91af;">JToken</span> result <span style="color: blue;">in</span> results)</pre>
|
||||
<pre style="margin: 0px;">{</pre>
|
||||
<pre style="margin: 0px;"> <span style="color: #2b91af;">SearchResult</span> searchResult = <span style="color: #2b91af;">JsonConvert</span>.DeserializeObject<<span style="color: #2b91af;">SearchResult</span>>(result.ToString());</pre>
|
||||
<pre style="margin: 0px;"> searchResults.Add(searchResult);</pre>
|
||||
<pre style="margin: 0px;">}</pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Title = <b>Paris Hilton</b> - Wikipedia, the free encyclopedia</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Content = [1] In 2006, she released her debut album...</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Url = http://en.wikipedia.org/wiki/Paris_Hilton</span></pre>
|
||||
<pre style="margin: 0px;"> </pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Title = <b>Paris Hilton</b></span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...</span></pre>
|
||||
<pre style="margin: 0px;"><span style="color: green;">// Url = http://www.imdb.com/name/nm0385296/</span></pre>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="footer"></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -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 <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Linq.ComponentModel" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True" xmlns="">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Utilities" isDocumented="False" xmlns="" /></NamespaceSummaries>
|
||||
<NamespaceSummaryItem name="(global)" isDocumented="False" />
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json" isDocumented="True">The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Converters" isDocumented="True">The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Linq" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Linq.ComponentModel" isDocumented="True">The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Schema" isDocumented="True">The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Serialization" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON.</NamespaceSummaryItem>
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Utilities" isDocumented="False" />
|
||||
<NamespaceSummaryItem name="Newtonsoft.Json.Bson" isDocumented="True">The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement BSON.</NamespaceSummaryItem>
|
||||
</NamespaceSummaries>
|
||||
</PropertyGroup>
|
||||
<!-- There are no properties for these two groups but they need to appear in
|
||||
order for Visual Studio to perform the build. -->
|
||||
@@ -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
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user