-Reorganized examples
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples
|
||||
{
|
||||
public class ReadJsonWithJsonTextReader
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
string json = @"{
|
||||
'CPU': 'Intel',
|
||||
'PSU': '500W',
|
||||
'Drives': [
|
||||
'DVD read/writer'
|
||||
/*(broken)*/,
|
||||
'500 gigabyte hard drive',
|
||||
'200 gigabype hard drive'
|
||||
]
|
||||
}";
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.Value != null)
|
||||
Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
|
||||
else
|
||||
Console.WriteLine("Token: {0}", reader.TokenType);
|
||||
}
|
||||
|
||||
// Token: StartObject
|
||||
// Token: PropertyName, Value: CPU
|
||||
// Token: String, Value: Intel
|
||||
// Token: PropertyName, Value: PSU
|
||||
// Token: String, Value: 500W
|
||||
// Token: PropertyName, Value: Drives
|
||||
// Token: StartArray
|
||||
// Token: String, Value: DVD read/writer
|
||||
// Token: Comment, Value: (broken)
|
||||
// Token: String, Value: 500 gigabyte hard drive
|
||||
// Token: String, Value: 200 gigabype hard drive
|
||||
// Token: EndArray
|
||||
// Token: EndObject
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples
|
||||
{
|
||||
public class WriteJsonWithJsonTextWriter
|
||||
{
|
||||
public void Example()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter writer = new JsonTextWriter(sw))
|
||||
{
|
||||
writer.Formatting = Formatting.Indented;
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("CPU");
|
||||
writer.WriteValue("Intel");
|
||||
writer.WritePropertyName("PSU");
|
||||
writer.WriteValue("500W");
|
||||
writer.WritePropertyName("Drives");
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue("DVD read/writer");
|
||||
writer.WriteComment("(broken)");
|
||||
writer.WriteValue("500 gigabyte hard drive");
|
||||
writer.WriteValue("200 gigabype hard drive");
|
||||
writer.WriteEnd();
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
Console.WriteLine(sb.ToString());
|
||||
// {
|
||||
// "CPU": "Intel",
|
||||
// "PSU": "500W",
|
||||
// "Drives": [
|
||||
// "DVD read/writer"
|
||||
// /*(broken)*/,
|
||||
// "500 gigabyte hard drive",
|
||||
// "200 gigabype hard drive"
|
||||
// ]
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user