File: ReadingWritingJSON.html

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (87 lines) | stat: -rw-r--r-- 6,616 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<html>
  
  <head>
    <title>Reading and Writing JSON</title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    <link href="custom.css" rel="stylesheet" type="text/css" />
  </head>
  
  <body>
    
    <div id="control">
      <span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
        <span class="topicTitle">Reading and Writing JSON</span></div>

    <div id="content">
      <span style="color: DarkGray"> </span>
    
	<p>To manually read and write JSON Json.NET provides the <a href="./html/T_Newtonsoft_Json_JsonReader.htm">JsonReader</a> and <a href="./html/T_Newtonsoft_Json_JsonWriter.htm">JsonWriter</a> classes.</p>

<h3>JsonTextReader and JsonTextWriter</h3>
<p><a href="./html/T_Newtonsoft_Json_JsonTextReader.htm">JsonTextReader</a> and <a href="./html/T_Newtonsoft_Json_JsonTextWriter.htm">JsonTextWriter</a> are used to read and write JSON text. The JsonTextWriter has a number of settings on it to control how JSON is formatted when it is written. These options include formatting, indention character, indent count and quote character.</p>

<div class="overflowpanel"> <div class="code">

<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: #2b91af;">StringBuilder</span> sb = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringBuilder</span>();</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">StringWriter</span> sw = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringWriter</span>(sb);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> (<span style="color: #2b91af;">JsonWriter</span> jsonWriter = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonTextWriter</span>(sw))</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.Formatting = <span style="color: #2b91af;">Formatting</span>.Indented;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteStartObject();</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WritePropertyName(<span style="color: #a31515;">"CPU"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"Intel"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WritePropertyName(<span style="color: #a31515;">"PSU"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"500W"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WritePropertyName(<span style="color: #a31515;">"Drives"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteStartArray();</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"DVD read/writer"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteComment(<span style="color: #a31515;">"(broken)"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"500 gigabyte hard drive"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteValue(<span style="color: #a31515;">"200 gigabype hard drive"</span>);</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteEnd();</pre>
<pre style="margin: 0px;">&nbsp; jsonWriter.WriteEndObject();</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;  "CPU": "Intel",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;  "PSU": "500W",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;  "Drives": [</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp;  "DVD read/writer"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp;  /*(broken)*/,</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp;  "500 gigabyte hard drive",</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp; &nbsp;  "200 gigabype hard drive"</span></pre>
<pre style="margin: 0px;"><span style="color: green;">//&nbsp;  ]</span></pre>
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
</div>

</div></div>

<h3>JTokenReader and JTokenWriter</h3>
<p><a href="./html/T_Newtonsoft_Json_Linq_JTokenReader.htm">JTokenReader</a> and <a href="./html/T_Newtonsoft_Json_Linq_JTokenWriter.htm">JTokenWriter</a> read and write LINQ to JSON objects. They are located in the <a href="./html/N_Newtonsoft_Json_Linq.htm">Newtonsoft.Json.Linq</a> namespace. These objects allow you to use LINQ to JSON objects with objects that read and write JSON such as the JsonSerializer. For example you can deserialize from a LINQ to JSON object into a regular .NET object and vice versa.</p>

<div class="overflowpanel"> <div class="code">

<div style="font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: #2b91af;">JObject</span> o = <span style="color: blue;">new</span> <span style="color: #2b91af;">JObject</span>(</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"Name"</span>, <span style="color: #a31515;">"John Smith"</span>),</pre>
<pre style="margin: 0px;">&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">JProperty</span>(<span style="color: #a31515;">"BirthDate"</span>, <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(1983, 3, 20))</pre>
<pre style="margin: 0px;">&nbsp; );</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: blue;">new</span> <span style="color: #2b91af;">JsonSerializer</span>();</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Person</span> p = (<span style="color: #2b91af;">Person</span>)serializer.Deserialize(<span style="color: blue;">new</span> <span style="color: #2b91af;">JTokenReader</span>(o), <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Person</span>));</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">Console</span>.WriteLine(p.Name);</pre>
<pre style="margin: 0px;"><span style="color: green;">// John Smith</span></pre>
</div>

</div></div>

      <div id="footer"></div>      
    </div>

  </body>

</html>