Tests
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
|
||||
namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
public class JsonTextReaderTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void YahooFinance()
|
||||
{
|
||||
string input = @"{
|
||||
""matches"" : [
|
||||
{""t"":""C"", ""n"":""Citigroup Inc."", ""e"":""NYSE"", ""id"":""662713""}
|
||||
,{""t"":""CHL"", ""n"":""China Mobile Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""660998""}
|
||||
,{""t"":""PTR"", ""n"":""PetroChina Company Limited (ADR)"", ""e"":""NYSE"", ""id"":""664536""}
|
||||
,{""t"":""RIO"", ""n"":""Companhia Vale do Rio Doce (ADR)"", ""e"":""NYSE"", ""id"":""671472""}
|
||||
,{""t"":""RIOPR"", ""n"":""Companhia Vale do Rio Doce (ADR)"", ""e"":""NYSE"", ""id"":""3512643""}
|
||||
,{""t"":""CSCO"", ""n"":""Cisco Systems, Inc."", ""e"":""NASDAQ"", ""id"":""99624""}
|
||||
,{""t"":""CVX"", ""n"":""Chevron Corporation"", ""e"":""NYSE"", ""id"":""667226""}
|
||||
,{""t"":""TM"", ""n"":""Toyota Motor Corporation (ADR)"", ""e"":""NYSE"", ""id"":""655880""}
|
||||
,{""t"":""JPM"", ""n"":""JPMorgan Chase \x26 Co."", ""e"":""NYSE"", ""id"":""665639""}
|
||||
,{""t"":""COP"", ""n"":""ConocoPhillips"", ""e"":""NYSE"", ""id"":""1691168""}
|
||||
,{""t"":""LFC"", ""n"":""China Life Insurance Company Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""688679""}
|
||||
,{""t"":""NOK"", ""n"":""Nokia Corporation (ADR)"", ""e"":""NYSE"", ""id"":""657729""}
|
||||
,{""t"":""KO"", ""n"":""The Coca-Cola Company"", ""e"":""NYSE"", ""id"":""6550""}
|
||||
,{""t"":""VZ"", ""n"":""Verizon Communications Inc."", ""e"":""NYSE"", ""id"":""664887""}
|
||||
,{""t"":""AMX"", ""n"":""America Movil S.A.B de C.V. (ADR)"", ""e"":""NYSE"", ""id"":""665834""}],
|
||||
""all"" : false
|
||||
}
|
||||
";
|
||||
|
||||
using (JsonReader jsonReader = new JsonTextReader(new StringReader(input)))
|
||||
{
|
||||
while (jsonReader.Read())
|
||||
{
|
||||
Console.WriteLine(jsonReader.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadingIndented()
|
||||
{
|
||||
string input = @"{
|
||||
CPU: 'Intel',
|
||||
Drives: [
|
||||
'DVD read/writer',
|
||||
""500 gigabyte hard drive""
|
||||
]
|
||||
}";
|
||||
|
||||
StringReader sr = new StringReader(input);
|
||||
|
||||
using (JsonReader jsonReader = new JsonTextReader(sr))
|
||||
{
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
|
||||
Assert.AreEqual(jsonReader.Value, "CPU");
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
Assert.AreEqual(jsonReader.Value, "Intel");
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
|
||||
Assert.AreEqual(jsonReader.Value, "Drives");
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
Assert.AreEqual(jsonReader.Value, "DVD read/writer");
|
||||
Assert.AreEqual(jsonReader.QuoteChar, '\'');
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
Assert.AreEqual(jsonReader.Value, "500 gigabyte hard drive");
|
||||
Assert.AreEqual(jsonReader.QuoteChar, '"');
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadingEscapedStrings()
|
||||
{
|
||||
string input = "{value:'Purple\\r \\n monkey\\'s:\\tdishwasher'}";
|
||||
|
||||
StringReader sr = new StringReader(input);
|
||||
|
||||
using (JsonReader jsonReader = new JsonTextReader(sr))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
Assert.AreEqual(jsonReader.Value, "Purple\r \n monkey's:\tdishwasher");
|
||||
Assert.AreEqual(jsonReader.QuoteChar, '\'');
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadNewlineLastCharacter()
|
||||
{
|
||||
string input = @"{
|
||||
CPU: 'Intel',
|
||||
Drives: [
|
||||
'DVD read/writer',
|
||||
""500 gigabyte hard drive""
|
||||
]
|
||||
}" + '\n';
|
||||
|
||||
object o = JavaScriptConvert.DeserializeObject(input);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteReadWrite()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(true);
|
||||
|
||||
jsonWriter.WriteStartObject();
|
||||
jsonWriter.WritePropertyName("integer");
|
||||
jsonWriter.WriteValue(99);
|
||||
jsonWriter.WritePropertyName("string");
|
||||
jsonWriter.WriteValue("how now brown cow?");
|
||||
jsonWriter.WritePropertyName("array");
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
jsonWriter.WriteValue(i);
|
||||
}
|
||||
|
||||
jsonWriter.WriteStartObject();
|
||||
jsonWriter.WritePropertyName("decimal");
|
||||
jsonWriter.WriteValue(990.00990099m);
|
||||
jsonWriter.WriteEndObject();
|
||||
|
||||
jsonWriter.WriteValue(5);
|
||||
jsonWriter.WriteEndArray();
|
||||
|
||||
jsonWriter.WriteEndObject();
|
||||
|
||||
jsonWriter.WriteValue("This is a string.");
|
||||
jsonWriter.WriteNull();
|
||||
jsonWriter.WriteNull();
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
|
||||
string json = sb.ToString();
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
|
||||
object jsonObject = serializer.Deserialize(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
sb = new StringBuilder();
|
||||
sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
serializer.Serialize(sw, jsonObject);
|
||||
}
|
||||
|
||||
Assert.AreEqual(json, sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Json;
|
||||
|
||||
namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
public class JsonTextWriterTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ValueFormatting()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue('@');
|
||||
jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'");
|
||||
jsonWriter.WriteValue(true);
|
||||
jsonWriter.WriteValue(10);
|
||||
jsonWriter.WriteValue(10.99);
|
||||
jsonWriter.WriteValue(0.99);
|
||||
jsonWriter.WriteValue(0.000000000000000001d);
|
||||
jsonWriter.WriteValue(0.000000000000000001m);
|
||||
jsonWriter.WriteValue(null);
|
||||
jsonWriter.WriteValue("This is a string.");
|
||||
jsonWriter.WriteNull();
|
||||
jsonWriter.WriteUndefined();
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
|
||||
string expected = @"[""@"",""\r\n\t\f\b?{\\r\\n\""'"",true,10,10.99,0.99,1E-18,0.000000000000000001,"""",""This is a string."",null,undefined]";
|
||||
string result = sb.ToString();
|
||||
|
||||
Console.WriteLine("ValueFormatting");
|
||||
Console.WriteLine(result);
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StringEscaping()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(@"""These pretzels are making me thirsty!""");
|
||||
jsonWriter.WriteValue("Jeff's house was burninated.");
|
||||
jsonWriter.WriteValue(@"1. You don't talk about fight club.
|
||||
2. You don't talk about fight club.");
|
||||
jsonWriter.WriteValue("35% of\t statistics\n are made\r up.");
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
|
||||
string expected = @"[""\""These pretzels are making me thirsty!\"""",""Jeff's house was burninated."",""1. You don't talk about fight club.\r\n2. You don't talk about fight club."",""35% of\t statistics\n are made\r up.""]";
|
||||
string result = sb.ToString();
|
||||
|
||||
Console.WriteLine("StringEscaping");
|
||||
Console.WriteLine(result);
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Indenting()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
jsonWriter.WriteStartObject();
|
||||
jsonWriter.WritePropertyName("CPU");
|
||||
jsonWriter.WriteValue("Intel");
|
||||
jsonWriter.WritePropertyName("PSU");
|
||||
jsonWriter.WriteValue("500W");
|
||||
jsonWriter.WritePropertyName("Drives");
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue("DVD read/writer");
|
||||
jsonWriter.WriteComment("(broken)");
|
||||
jsonWriter.WriteValue("500 gigabyte hard drive");
|
||||
jsonWriter.WriteValue("200 gigabype hard drive");
|
||||
jsonWriter.WriteEnd();
|
||||
jsonWriter.WriteEndObject();
|
||||
}
|
||||
|
||||
string expected = @"{
|
||||
""CPU"": ""Intel"",
|
||||
""PSU"": ""500W"",
|
||||
""Drives"": [
|
||||
""DVD read/writer""
|
||||
/*(broken)*/,
|
||||
""500 gigabyte hard drive"",
|
||||
""200 gigabype hard drive""
|
||||
]
|
||||
}";
|
||||
string result = sb.ToString();
|
||||
|
||||
Console.WriteLine("Indenting");
|
||||
Console.WriteLine(result);
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void State()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteStartObject();
|
||||
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WritePropertyName("CPU");
|
||||
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteValue("Intel");
|
||||
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WritePropertyName("Drives");
|
||||
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteValue("DVD read/writer");
|
||||
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteEnd();
|
||||
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteEndObject();
|
||||
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
public class JsonTokenReaderTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void YahooFinance()
|
||||
{
|
||||
JObject o =
|
||||
new JObject(
|
||||
new JProperty("Test1", new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc)),
|
||||
new JProperty("Test2", new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0))),
|
||||
new JProperty("Test3", "Test3Value"),
|
||||
new JProperty("Test4", null)
|
||||
);
|
||||
|
||||
using (JsonReader jsonReader = new JsonTokenReader(o))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test1", jsonReader.Value);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
|
||||
Assert.AreEqual(new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc), jsonReader.Value);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test2", jsonReader.Value);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
|
||||
Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test3", jsonReader.Value);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.String, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test3Value", jsonReader.Value);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test4", jsonReader.Value);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.Null, jsonReader.TokenType);
|
||||
Assert.AreEqual(null, jsonReader.Value);
|
||||
|
||||
Assert.IsTrue(jsonReader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);
|
||||
|
||||
Assert.IsFalse(jsonReader.Read());
|
||||
}
|
||||
|
||||
using (JsonReader jsonReader = new JsonTokenReader(o.Property("Test2")))
|
||||
{
|
||||
Assert.IsTrue(jsonReader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test2", jsonReader.Value);
|
||||
|
||||
Assert.IsTrue(jsonReader.Read());
|
||||
Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
|
||||
Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);
|
||||
|
||||
Assert.IsFalse(jsonReader.Read());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadingIndented()
|
||||
{
|
||||
string input = @"{
|
||||
CPU: 'Intel',
|
||||
Drives: [
|
||||
'DVD read/writer',
|
||||
""500 gigabyte hard drive""
|
||||
]
|
||||
}";
|
||||
|
||||
StringReader sr = new StringReader(input);
|
||||
|
||||
using (JsonReader jsonReader = new JsonTextReader(sr))
|
||||
{
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
|
||||
Assert.AreEqual(jsonReader.Value, "CPU");
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
Assert.AreEqual(jsonReader.Value, "Intel");
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
|
||||
Assert.AreEqual(jsonReader.Value, "Drives");
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
Assert.AreEqual(jsonReader.Value, "DVD read/writer");
|
||||
Assert.AreEqual(jsonReader.QuoteChar, '\'');
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
Assert.AreEqual(jsonReader.Value, "500 gigabyte hard drive");
|
||||
Assert.AreEqual(jsonReader.QuoteChar, '"');
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadingEscapedStrings()
|
||||
{
|
||||
string input = "{value:'Purple\\r \\n monkey\\'s:\\tdishwasher'}";
|
||||
|
||||
StringReader sr = new StringReader(input);
|
||||
|
||||
using (JsonReader jsonReader = new JsonTextReader(sr))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
Assert.AreEqual(jsonReader.Value, "Purple\r \n monkey's:\tdishwasher");
|
||||
Assert.AreEqual(jsonReader.QuoteChar, '\'');
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadNewlineLastCharacter()
|
||||
{
|
||||
string input = @"{
|
||||
CPU: 'Intel',
|
||||
Drives: [
|
||||
'DVD read/writer',
|
||||
""500 gigabyte hard drive""
|
||||
]
|
||||
}" + '\n';
|
||||
|
||||
object o = JavaScriptConvert.DeserializeObject(input);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteReadWrite()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(true);
|
||||
|
||||
jsonWriter.WriteStartObject();
|
||||
jsonWriter.WritePropertyName("integer");
|
||||
jsonWriter.WriteValue(99);
|
||||
jsonWriter.WritePropertyName("string");
|
||||
jsonWriter.WriteValue("how now brown cow?");
|
||||
jsonWriter.WritePropertyName("array");
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
jsonWriter.WriteValue(i);
|
||||
}
|
||||
|
||||
jsonWriter.WriteStartObject();
|
||||
jsonWriter.WritePropertyName("decimal");
|
||||
jsonWriter.WriteValue(990.00990099m);
|
||||
jsonWriter.WriteEndObject();
|
||||
|
||||
jsonWriter.WriteValue(5);
|
||||
jsonWriter.WriteEndArray();
|
||||
|
||||
jsonWriter.WriteEndObject();
|
||||
|
||||
jsonWriter.WriteValue("This is a string.");
|
||||
jsonWriter.WriteNull();
|
||||
jsonWriter.WriteNull();
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
|
||||
string json = sb.ToString();
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
|
||||
object jsonObject = serializer.Deserialize(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
sb = new StringBuilder();
|
||||
sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
serializer.Serialize(sw, jsonObject);
|
||||
}
|
||||
|
||||
Assert.AreEqual(json, sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
public class JsonTokenWriterTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ValueFormatting()
|
||||
{
|
||||
JContainer root;
|
||||
using (JsonTokenWriter jsonWriter = new JsonTokenWriter())
|
||||
{
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue('@');
|
||||
jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'");
|
||||
jsonWriter.WriteValue(true);
|
||||
jsonWriter.WriteValue(10);
|
||||
jsonWriter.WriteValue(10.99);
|
||||
jsonWriter.WriteValue(0.99);
|
||||
jsonWriter.WriteValue(0.000000000000000001d);
|
||||
jsonWriter.WriteValue(0.000000000000000001m);
|
||||
jsonWriter.WriteValue(null);
|
||||
jsonWriter.WriteValue("This is a string.");
|
||||
jsonWriter.WriteNull();
|
||||
jsonWriter.WriteUndefined();
|
||||
jsonWriter.WriteEndArray();
|
||||
|
||||
root = jsonWriter.Token;
|
||||
}
|
||||
|
||||
Assert.IsInstanceOfType(typeof(JArray), root);
|
||||
Assert.AreEqual(12, root.Children().Count());
|
||||
Assert.AreEqual("@", (string)root[0]);
|
||||
Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", (string)root[1]);
|
||||
Assert.AreEqual(true, (bool)root[2]);
|
||||
Assert.AreEqual(10, (int)root[3]);
|
||||
Assert.AreEqual(10.99, (double)root[4]);
|
||||
Assert.AreEqual(0.99, (double)root[5]);
|
||||
Assert.AreEqual(0.000000000000000001d, (double)root[6]);
|
||||
Assert.AreEqual(0.000000000000000001m, (decimal)root[7]);
|
||||
Assert.AreEqual(string.Empty, (string)root[8]);
|
||||
Assert.AreEqual("This is a string.", (string)root[9]);
|
||||
Assert.AreEqual(null, ((JValue)root[10]).Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void State()
|
||||
{
|
||||
using (JsonWriter jsonWriter = new JsonTokenWriter())
|
||||
{
|
||||
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteStartObject();
|
||||
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WritePropertyName("CPU");
|
||||
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteValue("Intel");
|
||||
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WritePropertyName("Drives");
|
||||
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteValue("DVD read/writer");
|
||||
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteEnd();
|
||||
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteEndObject();
|
||||
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,15 +66,15 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="JsonTokenReaderTest.cs" />
|
||||
<Compile Include="JsonTokenWriterTest.cs" />
|
||||
<Compile Include="DateTimeTestClass.cs" />
|
||||
<Compile Include="IsoDateTimeConverterTests.cs" />
|
||||
<Compile Include="JavaScriptConvertTest.cs" />
|
||||
<Compile Include="JavaScriptDateTimeConverterTests.cs" />
|
||||
<Compile Include="JsonTextReaderTest.cs" />
|
||||
<Compile Include="JsonSerializerTest.cs" />
|
||||
<Compile Include="JsonTextReaderTest.cs" />
|
||||
<Compile Include="JsonTextWriterTest.cs" />
|
||||
<Compile Include="JsonTokenReaderTest.cs" />
|
||||
<Compile Include="JsonTokenWriterTest.cs" />
|
||||
<Compile Include="LinqToJsonTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TestFixtureBase.cs" />
|
||||
|
||||
Reference in New Issue
Block a user