-Added reading and writing binary JSON (BSON) support - work in progress
-Added BsonReader, BsonWriter -Added support for reading and writing byte arrays to JsonReader, JsonWriter and LINQ to JSON classes -Added ReadAsBytes to JsonReader -Changed Silverlight build serializer to look for a TypeConverter -Changed .NET build to not rely on System.ComponentModel.DataAnnotations assembly to support .NET 4.0 client profile -Remove DateTimeOffset stand-in from Compact Framework build -Fixed .NET 2.0 build to run on environments without .NET 2.0 SP1 -Fixed deserialization to always use a new value created from a JsonConverter -Fixed XmlNodeConverter to support converting comments in JSON
This commit is contained in:
@@ -11,8 +11,6 @@ using System.IO;
|
||||
using System.Collections;
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
using System.Web.UI;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
@@ -596,7 +594,7 @@ Parameter name: arrayIndex")]
|
||||
Assert.AreEqual(p4, l[1]);
|
||||
}
|
||||
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
#if !PocketPC && !SILVERLIGHT && !NET20
|
||||
[Test]
|
||||
public void PropertyChanging()
|
||||
{
|
||||
|
||||
@@ -35,6 +35,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
public class JTokenReaderTest : TestFixtureBase
|
||||
{
|
||||
#if !PocketPC && !NET20
|
||||
[Test]
|
||||
public void YahooFinance()
|
||||
{
|
||||
@@ -109,6 +110,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsFalse(jsonReader.Read());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void ReadLineInfo()
|
||||
@@ -194,5 +196,56 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(false, lineInfo.HasLineInfo());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadBytes()
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes("Hello world!");
|
||||
|
||||
JObject o =
|
||||
new JObject(
|
||||
new JProperty("Test1", data)
|
||||
);
|
||||
|
||||
using (JTokenReader jsonReader = new JTokenReader(o))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test1", jsonReader.Value);
|
||||
|
||||
byte[] readBytes = jsonReader.ReadAsBytes();
|
||||
Assert.AreEqual(data, readBytes);
|
||||
|
||||
Assert.IsTrue(jsonReader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);
|
||||
|
||||
Assert.IsFalse(jsonReader.Read());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Error reading bytes. Expected bytes but got Integer.")]
|
||||
public void ReadBytesFailure()
|
||||
{
|
||||
JObject o =
|
||||
new JObject(
|
||||
new JProperty("Test1", 1)
|
||||
);
|
||||
|
||||
using (JTokenReader jsonReader = new JTokenReader(o))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
|
||||
Assert.AreEqual("Test1", jsonReader.Value);
|
||||
|
||||
jsonReader.ReadAsBytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -196,7 +196,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
public void Casting()
|
||||
{
|
||||
Assert.AreEqual(new DateTime(2000, 12, 20), (DateTime)new JValue(new DateTime(2000, 12, 20)));
|
||||
#if !PocketPC
|
||||
#if !PocketPC && !NET20
|
||||
Assert.AreEqual(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero), (DateTimeOffset)new JValue(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)));
|
||||
Assert.AreEqual(null, (DateTimeOffset?)new JValue((DateTimeOffset?)null));
|
||||
Assert.AreEqual(null, (DateTimeOffset?)(JValue)null);
|
||||
@@ -240,13 +240,16 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(5f, (float)(new JValue(5L)));
|
||||
Assert.AreEqual(5f, (float)(new JValue(5m)));
|
||||
Assert.AreEqual(5f, (float?)(new JValue(5m)));
|
||||
|
||||
byte[] data = new byte[0];
|
||||
Assert.AreEqual(data, (byte[])(new JValue(data)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImplicitCastingTo()
|
||||
{
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(new DateTime(2000, 12, 20)), (JValue)new DateTime(2000, 12, 20)));
|
||||
#if !PocketPC
|
||||
#if !PocketPC && !NET20
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)), (JValue)new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue((DateTimeOffset?)null), (JValue)(DateTimeOffset?)null));
|
||||
#endif
|
||||
@@ -269,6 +272,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(ushort.MaxValue), (JValue)ushort.MaxValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(11.1f), (JValue)11.1f));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(float.MinValue), (JValue)float.MinValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(double.MinValue), (JValue)double.MinValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(uint.MaxValue), (JValue)uint.MaxValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(ulong.MaxValue), (JValue)ulong.MaxValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(ulong.MinValue), (JValue)ulong.MinValue));
|
||||
@@ -281,7 +285,13 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(double.MaxValue), (JValue)(double?)double.MaxValue));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(double?)null));
|
||||
|
||||
Assert.IsFalse(JToken.DeepEquals(new JValue(true), (JValue)(bool?)null));
|
||||
Assert.IsFalse(JToken.DeepEquals(new JValue((object)null), (JValue)(object)null));
|
||||
|
||||
byte[] emptyData = new byte[0];
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(emptyData), (JValue)emptyData));
|
||||
Assert.IsFalse(JToken.DeepEquals(new JValue(emptyData), (JValue)new byte[1]));
|
||||
Assert.IsTrue(JToken.DeepEquals(new JValue(Encoding.UTF8.GetBytes("Hi")), (JValue)Encoding.UTF8.GetBytes("Hi")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -39,6 +39,8 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
[Test]
|
||||
public void ValueFormatting()
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes("Hello world.");
|
||||
|
||||
JToken root;
|
||||
using (JTokenWriter jsonWriter = new JTokenWriter())
|
||||
{
|
||||
@@ -55,13 +57,14 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
jsonWriter.WriteValue("This is a string.");
|
||||
jsonWriter.WriteNull();
|
||||
jsonWriter.WriteUndefined();
|
||||
jsonWriter.WriteValue(data);
|
||||
jsonWriter.WriteEndArray();
|
||||
|
||||
root = jsonWriter.Token;
|
||||
}
|
||||
|
||||
Assert.IsInstanceOfType(typeof(JArray), root);
|
||||
Assert.AreEqual(12, root.Children().Count());
|
||||
Assert.AreEqual(13, 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]);
|
||||
@@ -73,6 +76,8 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(string.Empty, (string)root[8]);
|
||||
Assert.AreEqual("This is a string.", (string)root[9]);
|
||||
Assert.AreEqual(null, ((JValue)root[10]).Value);
|
||||
Assert.AreEqual(null, ((JValue)root[11]).Value);
|
||||
Assert.AreEqual(data, (byte[])root[12]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -100,6 +105,9 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
jsonWriter.WriteValue("DVD read/writer");
|
||||
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteValue(new byte[0]);
|
||||
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
|
||||
|
||||
jsonWriter.WriteEnd();
|
||||
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
|
||||
|
||||
|
||||
@@ -60,6 +60,12 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
v.Value = DBNull.Value;
|
||||
Assert.AreEqual(DBNull.Value, v.Value);
|
||||
Assert.AreEqual(JTokenType.Null, v.Type);
|
||||
|
||||
byte[] data = new byte[0];
|
||||
v.Value = data;
|
||||
|
||||
Assert.AreEqual(data, v.Value);
|
||||
Assert.AreEqual(JTokenType.Bytes, v.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -545,6 +545,7 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(null, c["purple"]);
|
||||
}
|
||||
|
||||
#if !PocketPC && !NET20
|
||||
[Test]
|
||||
public void ToStringJsonConverter()
|
||||
{
|
||||
@@ -597,6 +598,7 @@ keyword such as type of business.""
|
||||
|
||||
Assert.AreEqual(4, jsonWriter.Token.Children().Count());
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void FromObject()
|
||||
@@ -683,9 +685,8 @@ keyword such as type of business.""
|
||||
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)
|
||||
new JProperty("Test2", "Test2Value"),
|
||||
new JProperty("Test3", null)
|
||||
);
|
||||
|
||||
enumerable = o.AsJEnumerable();
|
||||
|
||||
Reference in New Issue
Block a user