2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
26
#if !(NET35 || NET20 || PORTABLE)
28
using System.Collections.Generic;
29
using System.ComponentModel;
33
using System.Runtime.Serialization;
35
using Newtonsoft.Json.Converters;
36
using Newtonsoft.Json.Linq;
38
using NUnit.Framework;
40
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
41
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
42
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
44
using Newtonsoft.Json.Serialization;
45
using Newtonsoft.Json.Tests.TestObjects;
46
using Newtonsoft.Json.Utilities;
47
using System.Globalization;
48
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
50
namespace Newtonsoft.Json.Tests.Documentation
53
public class ReadingAndWritingJsonTests : TestFixtureBase
55
public void ReadingAndWritingJsonText()
57
#region ReadingAndWritingJsonText
58
StringBuilder sb = new StringBuilder();
59
StringWriter sw = new StringWriter(sb);
61
using (JsonWriter writer = new JsonTextWriter(sw))
63
writer.Formatting = Formatting.Indented;
65
writer.WriteStartObject();
66
writer.WritePropertyName("CPU");
67
writer.WriteValue("Intel");
68
writer.WritePropertyName("PSU");
69
writer.WriteValue("500W");
70
writer.WritePropertyName("Drives");
71
writer.WriteStartArray();
72
writer.WriteValue("DVD read/writer");
73
writer.WriteComment("(broken)");
74
writer.WriteValue("500 gigabyte hard drive");
75
writer.WriteValue("200 gigabype hard drive");
77
writer.WriteEndObject();
86
// "500 gigabyte hard drive",
87
// "200 gigabype hard drive"
94
public void ReadingJsonText()
96
#region ReadingJsonText
103
'500 gigabyte hard drive',
104
'200 gigabype hard drive'
108
JsonTextReader reader = new JsonTextReader(new StringReader(json));
109
while (reader.Read())
111
if (reader.Value != null)
112
Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
114
Console.WriteLine("Token: {0}", reader.TokenType);
117
// Token: StartObject
118
// Token: PropertyName, Value: CPU
119
// Token: String, Value: Intel
120
// Token: PropertyName, Value: PSU
121
// Token: String, Value: 500W
122
// Token: PropertyName, Value: Drives
124
// Token: String, Value: DVD read/writer
125
// Token: Comment, Value: (broken)
126
// Token: String, Value: 500 gigabyte hard drive
127
// Token: String, Value: 200 gigabype hard drive
133
public void ReadingAndWritingJsonLinq()
135
#region ReadingAndWritingJsonLinq
136
JObject o = new JObject(
137
new JProperty("Name", "John Smith"),
138
new JProperty("BirthDate", new DateTime(1983, 3, 20))
141
JsonSerializer serializer = new JsonSerializer();
142
Person p = (Person)serializer.Deserialize(new JTokenReader(o), typeof(Person));
144
Console.WriteLine(p.Name);
b'\\ No newline at end of file'