Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2009-12-07 02:10:14 +0300
committerJamesNK <james@newtonking.com>2009-12-07 02:10:14 +0300
commitf1a9993723e8bcb3ec61456a17ff4c47ed2326b2 (patch)
tree122dc00f49d2f59b0f908c35b01790a772a6b479 /Src/Newtonsoft.Json.Tests/Bson
parent7d845d1f135dfe27aa35950e344171e06d0543fd (diff)
-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
Diffstat (limited to 'Src/Newtonsoft.Json.Tests/Bson')
-rw-r--r--Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs180
-rw-r--r--Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs112
2 files changed, 292 insertions, 0 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs
new file mode 100644
index 0000000..95880fd
--- /dev/null
+++ b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs
@@ -0,0 +1,180 @@
+#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.Linq;
+using System.Text;
+using NUnit.Framework;
+using Newtonsoft.Json.Bson;
+using System.IO;
+using Newtonsoft.Json.Utilities;
+
+namespace Newtonsoft.Json.Tests.Bson
+{
+ public class BsonReaderTests : TestFixtureBase
+ {
+ [Test]
+ public void ReadSingleObject()
+ {
+ byte[] data = MiscellaneousUtils.HexToBytes("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00");
+ MemoryStream ms = new MemoryStream(data);
+ BsonReader reader = new BsonReader(ms);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
+ Assert.AreEqual("Blah", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.Integer, reader.TokenType);
+ Assert.AreEqual(1, reader.Value);
+ Assert.AreEqual(typeof(long), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
+
+ Assert.IsFalse(reader.Read());
+ }
+
+ [Test]
+ public void ReadObjectBsonFromSite()
+ {
+ byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
+
+ MemoryStream ms = new MemoryStream(data);
+ BsonReader reader = new BsonReader(ms);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
+ Assert.AreEqual("0", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.String, reader.TokenType);
+ Assert.AreEqual("a", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
+ Assert.AreEqual("1", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.String, reader.TokenType);
+ Assert.AreEqual("b", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
+ Assert.AreEqual("2", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.String, reader.TokenType);
+ Assert.AreEqual("c", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
+
+ Assert.IsFalse(reader.Read());
+ }
+
+ [Test]
+ public void ReadArrayBsonFromSite()
+ {
+ byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00");
+
+ MemoryStream ms = new MemoryStream(data);
+ BsonReader reader = new BsonReader(ms, true);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.String, reader.TokenType);
+ Assert.AreEqual("a", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.String, reader.TokenType);
+ Assert.AreEqual("b", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.String, reader.TokenType);
+ Assert.AreEqual("c", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
+
+ Assert.IsFalse(reader.Read());
+ }
+
+ [Test]
+ public void ReadBytes()
+ {
+ byte[] data = MiscellaneousUtils.HexToBytes("2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00");
+
+ MemoryStream ms = new MemoryStream(data);
+ BsonReader reader = new BsonReader(ms, true);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.String, reader.TokenType);
+ Assert.AreEqual("a", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.String, reader.TokenType);
+ Assert.AreEqual("b", reader.Value);
+ Assert.AreEqual(typeof(string), reader.ValueType);
+
+ byte[] encodedStringData = reader.ReadAsBytes();
+ Assert.IsNotNull(encodedStringData);
+ Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
+ Assert.AreEqual(encodedStringData, reader.Value);
+ Assert.AreEqual(typeof(byte[]), reader.ValueType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
+
+ Assert.IsFalse(reader.Read());
+
+ string decodedString = Encoding.UTF8.GetString(encodedStringData);
+ Assert.AreEqual("Hello world!", decodedString);
+ }
+ }
+} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
new file mode 100644
index 0000000..f3ce880
--- /dev/null
+++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
@@ -0,0 +1,112 @@
+#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.Linq;
+using System.Text;
+using NUnit.Framework;
+using Newtonsoft.Json.Bson;
+using System.IO;
+using Newtonsoft.Json.Utilities;
+
+namespace Newtonsoft.Json.Tests.Bson
+{
+ public class BsonWriterTests : TestFixtureBase
+ {
+ [Test]
+ public void WriteSingleObject()
+ {
+ MemoryStream ms = new MemoryStream();
+ BsonWriter writer = new BsonWriter(ms);
+
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+ writer.WriteValue(1);
+ writer.WriteEndObject();
+ writer.Flush();
+
+ string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
+ Assert.AreEqual("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00", bson);
+ }
+
+ [Test]
+ [ExpectedException(typeof(JsonWriterException), ExpectedMessage = "Cannot flush BsonWriter until JSON is complete.")]
+ public void FlushInsideObject()
+ {
+ MemoryStream ms = new MemoryStream();
+ BsonWriter writer = new BsonWriter(ms);
+
+ writer.WriteStartObject();
+ writer.WritePropertyName("Blah");
+ writer.WriteValue(1);
+ writer.Flush();
+ }
+
+ [Test]
+ public void WriteArrayBsonFromSite()
+ {
+ MemoryStream ms = new MemoryStream();
+ BsonWriter writer = new BsonWriter(ms);
+ writer.WriteStartArray();
+ writer.WriteValue("a");
+ writer.WriteValue("b");
+ writer.WriteValue("c");
+ writer.WriteEndArray();
+
+ writer.Flush();
+
+ ms.Seek(0, SeekOrigin.Begin);
+
+ string expected = "20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00";
+ string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
+
+ Assert.AreEqual(expected, bson);
+ }
+
+ [Test]
+ public void WriteBytes()
+ {
+ byte[] data = Encoding.UTF8.GetBytes("Hello world!");
+
+ MemoryStream ms = new MemoryStream();
+ BsonWriter writer = new BsonWriter(ms);
+ writer.WriteStartArray();
+ writer.WriteValue("a");
+ writer.WriteValue("b");
+ writer.WriteValue(data);
+ writer.WriteEndArray();
+
+ writer.Flush();
+
+ ms.Seek(0, SeekOrigin.Begin);
+
+ string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
+ string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
+
+ Assert.AreEqual(expected, bson);
+ }
+ }
+} \ No newline at end of file