public void CanRoundTripStackOverflowData()
        {
            var doc =
                @"{
""AboutMe"": ""<p>I'm the Director for Research and Development for <a href=\""http://www.prophoenix.com\"" rel=\""nofollow\"">ProPhoenix</a>, a public safety software company.  This position allows me to investigate new and existing technologies and incorporate them into our product line, with the end goal being to help public safety agencies to do their jobs more effeciently and safely.</p>\r\n\r\n<p>I'm an advocate for PowerShell, as I believe it encourages administrative best practices and allows developers to provide additional access to their applications, without needing to explicity write code for each administrative feature.  Part of my advocacy for PowerShell includes <a href=\""http://blog.usepowershell.com\"" rel=\""nofollow\"">my blog</a>, appearances on various podcasts, and acting as a Community Director for <a href=\""http://powershellcommunity.org\"" rel=\""nofollow\"">PowerShellCommunity.Org</a></p>\r\n\r\n<p>I’m also a co-host of Mind of Root (a weekly audio podcast about systems administration, tech news, and topics).</p>\r\n"",
""WebsiteUrl"": ""http://blog.usepowershell.com""
}";
            JObject parsed = JObject.Parse(doc);
            var memoryStream = new MemoryStream();
            var bsonWriter = new BsonWriter(memoryStream);
            parsed.WriteTo(bsonWriter);
            bsonWriter.Flush();
            memoryStream.Position = 0;

            BsonReader reader = new BsonReader(memoryStream);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("AboutMe", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("<p>I'm the Director for Research and Development for <a href=\"http://www.prophoenix.com\" rel=\"nofollow\">ProPhoenix</a>, a public safety software company.  This position allows me to investigate new and existing technologies and incorporate them into our product line, with the end goal being to help public safety agencies to do their jobs more effeciently and safely.</p>\r\n\r\n<p>I'm an advocate for PowerShell, as I believe it encourages administrative best practices and allows developers to provide additional access to their applications, without needing to explicity write code for each administrative feature.  Part of my advocacy for PowerShell includes <a href=\"http://blog.usepowershell.com\" rel=\"nofollow\">my blog</a>, appearances on various podcasts, and acting as a Community Director for <a href=\"http://powershellcommunity.org\" rel=\"nofollow\">PowerShellCommunity.Org</a></p>\r\n\r\n<p>I’m also a co-host of Mind of Root (a weekly audio podcast about systems administration, tech news, and topics).</p>\r\n", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("WebsiteUrl", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("http://blog.usepowershell.com", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void WriteOid()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            byte[] oid = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

            writer.WriteStartObject();
            writer.WritePropertyName("_oid");
            writer.WriteObjectId(oid);
            writer.WriteEndObject();

            string bson = BytesToHex(ms.ToArray());
            Assert.Equal("17-00-00-00-07-5F-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00", bson);

            ms.Seek(0, SeekOrigin.Begin);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(oid, (byte[])reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);
        }
        public void WriteDateTimes()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);
            writer.DateTimeKindHandling = DateTimeKind.Unspecified;

            writer.WriteStartArray();
            writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc));
            writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Local));
            writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Unspecified));
            writer.WriteEndArray();

            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            reader.ReadRootValueAsArray = true;
            reader.DateTimeKindHandling = DateTimeKind.Utc;

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Date, reader.TokenType);
            Assert.Equal(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Date, reader.TokenType);
            Assert.Equal(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Date, reader.TokenType);
            Assert.Equal(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
        }
        public void ReadLargeStrings()
        {
            string bson =
                "4E-02-00-00-02-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-22-01-00-00-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-00";

            BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson)));

            StringBuilder largeStringBuilder = new StringBuilder();
            for (int i = 0; i < 100; i++)
            {
                if (i > 0)
                    largeStringBuilder.Append("-");

                largeStringBuilder.Append(i.ToString(CultureInfo.InvariantCulture));
            }
            string largeString = largeStringBuilder.ToString();

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal(largeString, reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal(largeString, reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void WriteAndReadEmptyListsAndDictionaries()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            writer.WriteStartObject();
            writer.WritePropertyName("Arguments");
            writer.WriteStartObject();
            writer.WriteEndObject();
            writer.WritePropertyName("List");
            writer.WriteStartArray();
            writer.WriteEndArray();
            writer.WriteEndObject();

            string bson = BitConverter.ToString(ms.ToArray());

            Assert.Equal("20-00-00-00-03-41-72-67-75-6D-65-6E-74-73-00-05-00-00-00-00-04-4C-69-73-74-00-05-00-00-00-00-00", bson);

            BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson)));

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("Arguments", reader.Value.ToString());

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("List", reader.Value.ToString());

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadLong()
        {
            string hexdoc = "13-00-00-00-12-6C-6F-6E-67-00-FF-FF-FF-FF-FF-FF-FF-7F-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("long", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal(long.MaxValue, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadCodeWScope()
        {
            string hexdoc = "75-00-00-00-0F-63-6F-64-65-57-69-74-68-53-63-6F-70-65-00-61-00-00-00-35-00-00-00-66-6F-72-20-28-69-6E-74-20-69-20-3D-20-30-3B-20-69-20-3C-20-31-30-30-30-3B-20-69-2B-2B-29-0D-0A-7B-0D-0A-20-20-61-6C-65-72-74-28-61-72-67-31-29-3B-0D-0A-7D-00-24-00-00-00-02-61-72-67-31-00-15-00-00-00-4A-73-6F-6E-2E-4E-45-54-20-69-73-20-61-77-65-73-6F-6D-65-2E-00-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("codeWithScope", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("$code", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("for (int i = 0; i < 1000; i++)\r\n{\r\n  alert(arg1);\r\n}", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("$scope", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("arg1", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("Json.NET is awesome.", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadValues()
        {
            byte[] data = HexToBytes("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-02-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00");
            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

			reader.ReadRootValueAsArray = true;
            reader.DateTimeKindHandling = DateTimeKind.Utc;

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal(long.MaxValue, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal(long.MaxValue, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal((long)int.MaxValue, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal((long)int.MaxValue, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal((long)byte.MaxValue, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal((long)sbyte.MaxValue, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("a", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Float, reader.TokenType);
            Assert.Equal((double)decimal.MaxValue, reader.Value);
            Assert.Equal(typeof(double), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Float, reader.TokenType);
            Assert.Equal((double)double.MaxValue, reader.Value);
            Assert.Equal(typeof(double), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Float, reader.TokenType);
            Assert.Equal((double)float.MaxValue, reader.Value);
            Assert.Equal(typeof(double), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Boolean, reader.TokenType);
            Assert.Equal(true, reader.Value);
            Assert.Equal(typeof(bool), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, (byte[])reader.Value);
            Assert.Equal(typeof(byte[]), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Date, reader.TokenType);
            Assert.Equal(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc), reader.Value);
            Assert.Equal(typeof(DateTime), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Date, reader.TokenType);
            Assert.Equal(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc), reader.Value);
            Assert.Equal(typeof(DateTime), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadObjectBsonFromSite()
        {
            byte[] data = 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.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("0", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("a", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("1", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("b", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("2", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("c", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadGuid_Bytes()
        {
            byte[] data = HexToBytes("1D-00-00-00-05-30-00-10-00-00-00-04-D7-EE-21-D8-5C-4B-C9-43-8A-C2-69-28-E5-79-B7-05-00");

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);
            reader.ReadRootValueAsArray = true;

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Guid g = new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705");

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(g, reader.Value);
            Assert.Equal(typeof(Guid), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);

            ms = new MemoryStream(data);
            reader = new BsonReader(ms);
            reader.ReadRootValueAsArray = true;

            JsonSerializer serializer = new JsonSerializer();
            IList<Guid> l = serializer.Deserialize<IList<Guid>>(reader);

            Assert.Equal(1, l.Count);
            Assert.Equal(g, l[0]);
        }
        public void ReadDouble_Decimal()
        {
            byte[] data = HexToBytes("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00");

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);
            reader.FloatParseHandling = FloatParseHandling.Decimal;
            reader.ReadRootValueAsArray = true;

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Float, reader.TokenType);
            Assert.Equal(99.99m, reader.Value);
            Assert.Equal(typeof(decimal), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void GuidsShouldBeProperlyDeserialised_AsBytes_ReadAhead()
        {
            Guid g = new Guid("822C0CE6-CC42-4753-A3C3-26F0684A4B88");

            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);
            writer.WriteStartObject();
            writer.WritePropertyName("TheGuid");
            writer.WriteValue(g);
            writer.WriteEndObject();
            writer.Flush();

            byte[] bytes = ms.ToArray();

            BsonReader reader = new BsonReader(new MemoryStream(bytes));
            Assert.True(reader.Read());
            Assert.True(reader.Read());

            Assert.Equal(g.ToByteArray(), reader.ReadAsBytes());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(typeof(byte[]), reader.ValueType);
            Assert.Equal(g.ToByteArray(), (byte[])reader.Value);

            Assert.True(reader.Read());
            Assert.False(reader.Read());

            JsonSerializer serializer = new JsonSerializer();
            serializer.MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead;
            BytesTestClass b = serializer.Deserialize<BytesTestClass>(new BsonReader(new MemoryStream(bytes)));
            Assert.Equal(g.ToByteArray(), b.TheGuid);
        }
        public void ReadGuid_Text()
        {
            byte[] data = HexToBytes("31-00-00-00-02-30-00-25-00-00-00-64-38-32-31-65-65-64-37-2D-34-62-35-63-2D-34-33-63-39-2D-38-61-63-32-2D-36-39-32-38-65-35-37-39-62-37-30-35-00-00");

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);
            reader.ReadRootValueAsArray = true;

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("d821eed7-4b5c-43c9-8ac2-6928e579b705", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);

            ms = new MemoryStream(data);
            reader = new BsonReader(ms);
            reader.ReadRootValueAsArray = true;

            JsonSerializer serializer = new JsonSerializer();
            IList<Guid> l = serializer.Deserialize<IList<Guid>>(reader);

            Assert.Equal(1, l.Count);
            Assert.Equal(new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705"), l[0]);
        }
        public void MultibyteCharacterPropertyNamesAndStrings()
        {
            string json = @"{
  ""ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ"": ""ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ""
}";
            JObject parsed = JObject.Parse(json);
            var memoryStream = new MemoryStream();
            var bsonWriter = new BsonWriter(memoryStream);
            parsed.WriteTo(bsonWriter);
            bsonWriter.Flush();
            memoryStream.Position = 0;

            BsonReader reader = new BsonReader(memoryStream);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);
        }
        public void Bson_SupportMultipleContent()
        {
            MemoryStream myStream = new MemoryStream();
            BsonWriter writer = new BsonWriter(myStream);
            JsonSerializer serializer = new JsonSerializer();
            MyTest tst1 = new MyTest
            {
                TimeStamp = new DateTime(2000, 12, 20, 12, 59, 59, DateTimeKind.Utc),
                UserName = "******"
            };
            MyTest tst2 = new MyTest
            {
                TimeStamp = new DateTime(2010, 12, 20, 12, 59, 59, DateTimeKind.Utc),
                UserName = "******"
            };
            serializer.Serialize(writer, tst1);
            serializer.Serialize(writer, tst2);

            myStream.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(myStream)
            {
                SupportMultipleContent = true,
                DateTimeKindHandling = DateTimeKind.Utc
            };

            MyTest tst1A = serializer.Deserialize<MyTest>(reader);

            reader.Read();

            MyTest tst2A = serializer.Deserialize<MyTest>(reader);

            Assert.Equal(tst1.UserName, tst1A.UserName);
            Assert.Equal(tst1.TimeStamp, tst1A.TimeStamp);

            Assert.Equal(tst2.UserName, tst2A.UserName);
            Assert.Equal(tst2.TimeStamp, tst2A.TimeStamp);
        }
        public void ReadArrayBsonFromSite()
        {
            byte[] data = 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.Equal(false, reader.ReadRootValueAsArray);
            Assert.Equal(DateTimeKind.Local, reader.DateTimeKindHandling);

            reader.ReadRootValueAsArray = true;
            reader.DateTimeKindHandling = DateTimeKind.Utc;

            Assert.Equal(true, reader.ReadRootValueAsArray);
            Assert.Equal(DateTimeKind.Utc, reader.DateTimeKindHandling);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("a", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("b", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("c", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadUndefined()
        {
            string hexdoc = "10-00-00-00-06-75-6E-64-65-66-69-6E-65-64-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("undefined", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Undefined, reader.TokenType);
            Assert.Equal(null, reader.Value);
            Assert.Equal(null, reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadAsInt32BadString()
        {
            AssertException.Throws<JsonReaderException>(() =>
            {
                byte[] data = 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.Equal(false, reader.ReadRootValueAsArray);
                Assert.Equal(DateTimeKind.Local, reader.DateTimeKindHandling);

                reader.ReadRootValueAsArray = true;
                reader.DateTimeKindHandling = DateTimeKind.Utc;

                Assert.Equal(true, reader.ReadRootValueAsArray);
                Assert.Equal(DateTimeKind.Utc, reader.DateTimeKindHandling);

                Assert.True(reader.Read());
                Assert.Equal(JsonToken.StartArray, reader.TokenType);

                reader.ReadAsInt32();
            }, "Could not convert string to integer: a. Path '[0]'.");
        }
        public void ReadReference()
        {
            string hexdoc = "1E-00-00-00-0C-6F-69-64-00-04-00-00-00-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("oid", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("$ref", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("oid", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("$id", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, (byte[])reader.Value);
            Assert.Equal(typeof(byte[]), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadBytes()
        {
            byte[] data = 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, DateTimeKind.Utc);

            Assert.Equal(true, reader.ReadRootValueAsArray);
            Assert.Equal(DateTimeKind.Utc, reader.DateTimeKindHandling);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("a", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("b", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            byte[] encodedStringData = reader.ReadAsBytes();
            Assert.NotNull(encodedStringData);
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(encodedStringData, reader.Value);
            Assert.Equal(typeof(byte[]), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);

            string decodedString = Encoding.UTF8.GetString(encodedStringData, 0, encodedStringData.Length);
            Assert.Equal("Hello world!", decodedString);
        }
 public void ReadEndOfStream()
 {
     BsonReader reader = new BsonReader(new MemoryStream());
     Assert.False(reader.Read());
 }
        public void ReadOid()
        {
            byte[] data = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("_id", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(HexToBytes("4ABBED9D1D8B0F0218000001"), (byte[])reader.Value);
            Assert.Equal(typeof(byte[]), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("test", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("1234£56", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadEmptyStrings()
        {
            string bson = "0C-00-00-00-02-00-01-00-00-00-00-00";

            BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson)));

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void ReadNestedArray()
        {
            string hexdoc = "82-00-00-00-07-5F-69-64-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("_id", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"), (byte[])reader.Value);
            Assert.Equal(typeof(byte[]), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("a", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            for (int i = 1; i <= 8; i++)
            {
                Assert.True(reader.Read());
                Assert.Equal(JsonToken.Float, reader.TokenType);

                double value = (i != 5)
                    ? Convert.ToDouble(i)
                    : 5.78960446186581E+77d;

                Assert.Equal(value, reader.Value);
            }

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("b", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("test", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        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-00-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
            string bson = BytesToHex(ms.ToArray());

            Assert.Equal(expected, bson);

            BsonReader reader = new BsonReader(new MemoryStream(ms.ToArray()));
            reader.ReadRootValueAsArray = true;
            reader.Read();
            reader.Read();
            reader.Read();
            reader.Read();
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(data, (byte[])reader.Value);
        }
        public void ReadRegex()
        {
            string hexdoc = "15-00-00-00-0B-72-65-67-65-78-00-74-65-73-74-00-67-69-6D-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("regex", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal(@"/test/gim", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void WriteReadEmptyAndNullStrings()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            writer.WriteStartArray();
            writer.WriteValue("Content!");
            writer.WriteValue("");
            writer.WriteValue((string)null);
            writer.WriteEndArray();

            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            reader.ReadRootValueAsArray = true;

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("Content!", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Null, reader.TokenType);
            Assert.Equal(null, reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
        }
        public void ReadCode()
        {
            string hexdoc = "1A-00-00-00-0D-63-6F-64-65-00-0B-00-00-00-49-20-61-6D-20-63-6F-64-65-21-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("code", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal(@"I am code!", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void WriteBigInteger()
        {
            BigInteger i = BigInteger.Parse("1999999999999999999999999999999999999999999999999999999999990");

            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            writer.WriteStartObject();
            writer.WritePropertyName("Blah");
            writer.WriteValue(i);
            writer.WriteEndObject();

            string bson = BytesToHex(ms.ToArray());
            Assert.Equal("2A-00-00-00-05-42-6C-61-68-00-1A-00-00-00-00-F6-FF-FF-FF-FF-FF-FF-1F-B2-21-CB-28-59-84-C4-AE-03-8A-44-34-2F-4C-4E-9E-3E-01-00", bson);

            ms.Seek(0, SeekOrigin.Begin);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);
            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(new byte[] { 246, 255, 255, 255, 255, 255, 255, 31, 178, 33, 203, 40, 89, 132, 196, 174, 3, 138, 68, 52, 47, 76, 78, 158, 62, 1 }, (byte[])reader.Value);
            Assert.Equal(i, new BigInteger((byte[])reader.Value));

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
        }
        public void ReadSingleObject()
        {
            byte[] data = 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.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("Blah", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal(1L, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }