To manually read and write JSON Json.NET provides the JsonReader and JsonWriter classes.
JsonTextReader and JsonTextWriter
JsonReader and JsonWriter are low level classes and used internally by Json.NET. To quickly work with JSON either the serializer - Serializing and Deserializing JSON or using LINQ to JSON is recommended. |
JsonTextReader and JsonTextWriter 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.
1StringBuilder sb = new StringBuilder(); 2StringWriter sw = new StringWriter(sb); 3 4using (JsonWriter writer = new JsonTextWriter(sw)) 5{ 6 writer.Formatting = Formatting.Indented; 7 8 writer.WriteStartObject(); 9 writer.WritePropertyName("CPU"); 10 writer.WriteValue("Intel"); 11 writer.WritePropertyName("PSU"); 12 writer.WriteValue("500W"); 13 writer.WritePropertyName("Drives"); 14 writer.WriteStartArray(); 15 writer.WriteValue("DVD read/writer"); 16 writer.WriteComment("(broken)"); 17 writer.WriteValue("500 gigabyte hard drive"); 18 writer.WriteValue("200 gigabype hard drive"); 19 writer.WriteEnd(); 20 writer.WriteEndObject(); 21} 22 23// { 24// "CPU": "Intel", 25// "PSU": "500W", 26// "Drives": [ 27// "DVD read/writer" 28// /*(broken)*/, 29// "500 gigabyte hard drive", 30// "200 gigabype hard drive" 31// ] 32// }
JsonTextReader has settings on it for reading different date formats and time zones, and the culture used when reading text values.
1string json = @"{ 2 'CPU': 'Intel', 3 'PSU': '500W', 4 'Drives': [ 5 'DVD read/writer' 6 /*(broken)*/, 7 '500 gigabyte hard drive', 8 '200 gigabype hard drive' 9 ] 10}"; 11 12JsonTextReader reader = new JsonTextReader(new StringReader(json)); 13while (reader.Read()) 14{ 15 if (reader.Value != null) 16 Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value); 17 else 18 Console.WriteLine("Token: {0}", reader.TokenType); 19} 20 21// Token: StartObject 22// Token: PropertyName, Value: CPU 23// Token: String, Value: Intel 24// Token: PropertyName, Value: PSU 25// Token: String, Value: 500W 26// Token: PropertyName, Value: Drives 27// Token: StartArray 28// Token: String, Value: DVD read/writer 29// Token: Comment, Value: (broken) 30// Token: String, Value: 500 gigabyte hard drive 31// Token: String, Value: 200 gigabype hard drive 32// Token: EndArray 33// Token: EndObject
JTokenReader and JTokenWriter
JTokenReader and JTokenWriter read and write LINQ to JSON objects. They are located in the Newtonsoft.Json.Linq 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.
1JObject o = new JObject( 2 new JProperty("Name", "John Smith"), 3 new JProperty("BirthDate", new DateTime(1983, 3, 20)) 4 ); 5 6JsonSerializer serializer = new JsonSerializer(); 7Person p = (Person)serializer.Deserialize(new JTokenReader(o), typeof(Person)); 8 9Console.WriteLine(p.Name); 10// John Smith