/tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs
C# | 1647 lines | 1504 code | 124 blank | 19 comment | 11 complexity | 9ab72abac967c362318b98f93e0f67a3 MD5 | raw file
Possible License(s): Apache-2.0
Large files files are truncated, but you can click here to view the full file
- /* Copyright 2010-present MongoDB Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using FluentAssertions;
- using MongoDB.Bson.IO;
- using MongoDB.Bson.Serialization;
- using MongoDB.Bson.TestHelpers;
- using MongoDB.Bson.TestHelpers.XunitExtensions;
- using Xunit;
- namespace MongoDB.Bson.Tests.IO
- {
- public class JsonReaderTests
- {
- private IBsonReader _bsonReader;
- [Theory]
- [ParameterAttributeData]
- public void JsonReader_should_support_reading_multiple_documents(
- [Range(0, 3)]
- int numberOfDocuments)
- {
- var document = new BsonDocument("x", 1);
- var json = document.ToJson();
- var input = Enumerable.Repeat(json, numberOfDocuments).Aggregate("", (a, j) => a + j);
- var expectedResult = Enumerable.Repeat(document, numberOfDocuments);
- using (var jsonReader = new JsonReader(input))
- {
- var result = new List<BsonDocument>();
- jsonReader.State.Should().Be(BsonReaderState.Initial);
- while (!jsonReader.IsAtEndOfFile())
- {
- jsonReader.ReadStartDocument();
- var name = jsonReader.ReadName();
- var value = jsonReader.ReadInt32();
- jsonReader.ReadEndDocument();
- jsonReader.State.Should().Be(BsonReaderState.Initial);
- var resultDocument = new BsonDocument(name, value);
- result.Add(resultDocument);
- }
- result.Should().Equal(expectedResult);
- }
- }
- [Fact]
- public void TestArrayEmpty()
- {
- var json = "[]";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
- _bsonReader.ReadStartArray();
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReadEndArray();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
- }
- [Fact]
- public void TestArrayOneElement()
- {
- var json = "[1]";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
- _bsonReader.ReadStartArray();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal(1, _bsonReader.ReadInt32());
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReadEndArray();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
- }
- [Fact]
- public void TestArrayTwoElements()
- {
- var json = "[1, 2]";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
- _bsonReader.ReadStartArray();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal(1, _bsonReader.ReadInt32());
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal(2, _bsonReader.ReadInt32());
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReadEndArray();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
- }
- [Theory]
- [InlineData("{ $binary : \"AQ==\", $type : 0 }", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("{ $binary : \"AQ==\", $type : 128 }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
- [InlineData("{ $binary : \"AQ==\", $type : \"0\" }", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("{ $binary : \"AQ==\", $type : \"00\" }", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("{ $binary : \"AQ==\", $type : \"80\" }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"00\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"80\" } }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
- [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("{ $binary : { subType : \"00\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("{ $binary : { subType : \"80\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
- [InlineData("BinData(0, \"AQ==\")", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("BinData(128, \"AQ==\")", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
- [InlineData("HexData(0, \"01\")", new byte[] { 1 }, BsonBinarySubType.Binary)]
- [InlineData("HexData(128, \"01\")", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
- public void TestBinaryData(string json, byte[] expectedBytes, BsonBinarySubType expectedSubType)
- {
- using (var reader = new JsonReader(json))
- {
- var result = reader.ReadBinaryData();
- result.Should().Be(new BsonBinaryData(expectedBytes, expectedSubType));
- reader.State.Should().Be(BsonReaderState.Initial);
- reader.IsAtEndOfFile().Should().BeTrue();
- }
- }
- [Theory]
- [InlineData("{ $binary")]
- [InlineData("{ $binary :")]
- [InlineData("{ $binary : \"AQ==\"")]
- [InlineData("{ $binary : \"AQ==\",")]
- [InlineData("{ $binary : \"AQ==\", $type")]
- [InlineData("{ $binary : \"AQ==\", $type :")]
- [InlineData("{ $binary : \"AQ==\", $type : 0")]
- [InlineData("{ $binary : {")]
- [InlineData("{ $binary : { base64")]
- [InlineData("{ $binary : { base64 :")]
- [InlineData("{ $binary : { base64 : \"AQ==\"")]
- [InlineData("{ $binary : { base64 : \"AQ==\",")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType :")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\"")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" }")]
- [InlineData("{ $binary : { subType")]
- [InlineData("{ $binary : { subType :")]
- [InlineData("{ $binary : { subType : \"0\"")]
- [InlineData("{ $binary : { subType : \"0\",")]
- [InlineData("{ $binary : { subType : \"0\", base64")]
- [InlineData("{ $binary : { subType : \"0\", base64 :")]
- [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\"")]
- [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" }")]
- [InlineData("BinData")]
- [InlineData("BinData(")]
- [InlineData("BinData(0")]
- [InlineData("BinData(0,")]
- [InlineData("BinData(0, \"AQ==\"")]
- [InlineData("HexData")]
- [InlineData("HexData(")]
- [InlineData("HexData(0")]
- [InlineData("HexData(0,")]
- [InlineData("HexData(0, \"01\"")]
- public void TestBinaryDataEndOfFile(string json)
- {
- using (var reader = new JsonReader(json))
- {
- var exception = Record.Exception(() => reader.ReadBinaryData());
- exception.Should().BeOfType<FormatException>();
- }
- }
- [Theory]
- [InlineData("{ $binary [")]
- [InlineData("{ $binary : [")]
- [InlineData("{ $binary : \"AQ==\" [")]
- [InlineData("{ $binary : \"AQ==\", [")]
- [InlineData("{ $binary : \"AQ==\", $type [")]
- [InlineData("{ $binary : \"AQ==\", $type : [")]
- [InlineData("{ $binary : \"AQ==\", $type : 0 [")]
- [InlineData("{ $binary : { [")]
- [InlineData("{ $binary : { base64 [")]
- [InlineData("{ $binary : { base64 : [")]
- [InlineData("{ $binary : { base64 : \"AQ==\" [")]
- [InlineData("{ $binary : { base64 : \"AQ==\", [")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType [")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : [")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"\" [")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" [")]
- [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" } [")]
- [InlineData("{ $binary : { subType [")]
- [InlineData("{ $binary : { subType : [")]
- [InlineData("{ $binary : { subType : \"\" [")]
- [InlineData("{ $binary : { subType : \"0\" [")]
- [InlineData("{ $binary : { subType : \"0\", [")]
- [InlineData("{ $binary : { subType : \"0\", base64 [")]
- [InlineData("{ $binary : { subType : \"0\", base64 : [")]
- [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" [")]
- [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" } [")]
- [InlineData("BinData [")]
- [InlineData("BinData( [")]
- [InlineData("BinData(0 [")]
- [InlineData("BinData(0, [")]
- [InlineData("BinData(0, \"AQ==\" [")]
- [InlineData("HexData [")]
- [InlineData("HexData( [")]
- [InlineData("HexData(0 [")]
- [InlineData("HexData(0, [")]
- [InlineData("HexData(0, \"01\" [")]
- public void TestBinaryDataInvalidToken(string json)
- {
- using (var reader = new JsonReader(json))
- {
- var exception = Record.Exception(() => reader.ReadBinaryData());
- exception.Should().BeOfType<FormatException>();
- }
- }
- [Fact]
- public void TestBookmark()
- {
- var json = "{ \"x\" : 1, \"y\" : 2 }";
- using (_bsonReader = new JsonReader(json))
- {
- // do everything twice returning to bookmark in between
- var bookmark = _bsonReader.GetBookmark();
- Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
- _bsonReader.ReturnToBookmark(bookmark);
- Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
- bookmark = _bsonReader.GetBookmark();
- _bsonReader.ReadStartDocument();
- _bsonReader.ReturnToBookmark(bookmark);
- _bsonReader.ReadStartDocument();
- bookmark = _bsonReader.GetBookmark();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- _bsonReader.ReturnToBookmark(bookmark);
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- bookmark = _bsonReader.GetBookmark();
- Assert.Equal("x", _bsonReader.ReadName());
- _bsonReader.ReturnToBookmark(bookmark);
- Assert.Equal("x", _bsonReader.ReadName());
- bookmark = _bsonReader.GetBookmark();
- Assert.Equal(1, _bsonReader.ReadInt32());
- _bsonReader.ReturnToBookmark(bookmark);
- Assert.Equal(1, _bsonReader.ReadInt32());
- bookmark = _bsonReader.GetBookmark();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- _bsonReader.ReturnToBookmark(bookmark);
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- bookmark = _bsonReader.GetBookmark();
- Assert.Equal("y", _bsonReader.ReadName());
- _bsonReader.ReturnToBookmark(bookmark);
- Assert.Equal("y", _bsonReader.ReadName());
- bookmark = _bsonReader.GetBookmark();
- Assert.Equal(2, _bsonReader.ReadInt32());
- _bsonReader.ReturnToBookmark(bookmark);
- Assert.Equal(2, _bsonReader.ReadInt32());
- bookmark = _bsonReader.GetBookmark();
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReturnToBookmark(bookmark);
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- bookmark = _bsonReader.GetBookmark();
- _bsonReader.ReadEndDocument();
- _bsonReader.ReturnToBookmark(bookmark);
- _bsonReader.ReadEndDocument();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
- }
- [Fact]
- public void TestBooleanFalse()
- {
- var json = "false";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Boolean, _bsonReader.ReadBsonType());
- Assert.Equal(false, _bsonReader.ReadBoolean());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<bool>(json).ToJson());
- }
- [Fact]
- public void TestBooleanTrue()
- {
- var json = "true";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Boolean, _bsonReader.ReadBsonType());
- Assert.Equal(true, _bsonReader.ReadBoolean());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<bool>(json).ToJson());
- }
- [Theory]
- [InlineData("{ $date : 0 }", 0L)]
- [InlineData("{ $date : -9223372036854775808 }", -9223372036854775808L)]
- [InlineData("{ $date : 9223372036854775807 }", 9223372036854775807L)]
- [InlineData("{ $date : { $numberLong : 0 } }", 0L)]
- [InlineData("{ $date : { $numberLong : -9223372036854775808 } }", -9223372036854775808L)]
- [InlineData("{ $date : { $numberLong : 9223372036854775807 } }", 9223372036854775807L)]
- [InlineData("{ $date : { $numberLong : \"0\" } }", 0L)]
- [InlineData("{ $date : { $numberLong : \"-9223372036854775808\" } }", -9223372036854775808L)]
- [InlineData("{ $date : { $numberLong : \"9223372036854775807\" } }", 9223372036854775807L)]
- [InlineData("{ $date : \"1970-01-01T00:00:00Z\" }", 0L)]
- [InlineData("{ $date : \"0001-01-01T00:00:00Z\" }", -62135596800000L)]
- [InlineData("{ $date : \"1970-01-01T00:00:00.000Z\" }", 0L)]
- [InlineData("{ $date : \"0001-01-01T00:00:00.000Z\" }", -62135596800000L)]
- [InlineData("{ $date : \"9999-12-31T23:59:59.999Z\" }", 253402300799999L)]
- [InlineData("new Date(0)", 0L)]
- [InlineData("new Date(9223372036854775807)", 9223372036854775807L)]
- [InlineData("new Date(-9223372036854775808)", -9223372036854775808L)]
- [InlineData("ISODate(\"1970-01-01T00:00:00Z\")", 0L)]
- [InlineData("ISODate(\"0001-01-01T00:00:00Z\")", -62135596800000L)]
- [InlineData("ISODate(\"1970-01-01T00:00:00.000Z\")", 0L)]
- [InlineData("ISODate(\"0001-01-01T00:00:00.000Z\")", -62135596800000L)]
- [InlineData("ISODate(\"9999-12-31T23:59:59.999Z\")", 253402300799999L)]
- public void TestDateTime(string json, long expectedResult)
- {
- using (var reader = new JsonReader(json))
- {
- var result = reader.ReadDateTime();
- result.Should().Be(expectedResult);
- reader.State.Should().Be(BsonReaderState.Initial);
- reader.IsAtEndOfFile().Should().BeTrue();
- }
- }
- [Fact]
- public void TestDateTimeMinBson()
- {
- var json = "new Date(-9223372036854775808)";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
- Assert.Equal(-9223372036854775808, _bsonReader.ReadDateTime());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson());
- }
- [Fact]
- public void TestDateTimeMaxBson()
- {
- var json = "new Date(9223372036854775807)";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
- Assert.Equal(9223372036854775807, _bsonReader.ReadDateTime());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson());
- }
- [Theory]
- [InlineData("ISODate(\"1970\")", 0L, "ISODate(\"1970-01-01T00:00:00Z\")")]
- [InlineData("ISODate(\"1970-01\")", 0L, "ISODate(\"1970-01-01T00:00:00Z\")")]
- [InlineData("ISODate(\"1970-01-02\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01:02\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01:02.003\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
- [InlineData("ISODate(\"1970-01-02T00Z\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01Z\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01:02Z\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01:02.003Z\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
- [InlineData("ISODate(\"1970-01-02T00+00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01+00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01:02+00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01:02.003+00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
- [InlineData("ISODate(\"1970-01-02T00+00:00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01+00:00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01:02+00:00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
- [InlineData("ISODate(\"1970-01-02T00:01:02.003+00:00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
- [InlineData("ISODate(\"19700102\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"19700102T00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"19700102T0001\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
- [InlineData("ISODate(\"19700102T000102\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
- [InlineData("ISODate(\"19700102T000102.003\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
- [InlineData("ISODate(\"19700102T00Z\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"19700102T0001Z\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
- [InlineData("ISODate(\"19700102T000102Z\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
- [InlineData("ISODate(\"19700102T000102.003Z\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
- [InlineData("ISODate(\"19700102T00+00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"19700102T0001+00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
- [InlineData("ISODate(\"19700102T000102+00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
- [InlineData("ISODate(\"19700102T000102.003+00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
- [InlineData("ISODate(\"19700102T00+00:00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
- [InlineData("ISODate(\"19700102T0001+00:00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
- [InlineData("ISODate(\"19700102T000102+00:00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
- [InlineData("ISODate(\"19700102T000102.003+00:00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
- public void TestDateTimeShell(string json, long expectedResult, string canonicalJson)
- {
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
- Assert.Equal(expectedResult, _bsonReader.ReadDateTime());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell };
- Assert.Equal(canonicalJson, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
- }
- [Fact]
- public void TestDateTimeStrict()
- {
- var json = "{ \"$date\" : 0 }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
- Assert.Equal(0, _bsonReader.ReadDateTime());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- #pragma warning disable 618
- var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
- #pragma warning restore 618
- Assert.Equal(json, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
- }
- [Fact]
- public void TestDateTimeStrictIso8601()
- {
- var json = "{ \"$date\" : \"1970-01-01T00:00:00Z\" }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
- Assert.Equal(0, _bsonReader.ReadDateTime());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var expected = "{ \"$date\" : 0 }"; // it's still not ISO8601 on the way out
- #pragma warning disable 618
- var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
- #pragma warning restore 618
- Assert.Equal(expected, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
- }
- [Theory]
- [InlineData("{ $date: { \"$numberLong\": \"1552949630483\" } }", 1552949630483L)]
- [InlineData("{ $date: { $numberLong: \"1552949630483\" } }", 1552949630483L)]
- public void TestDateTimeWithNumberLong(string json, long expectedResult)
- {
- using (var reader = new JsonReader(json))
- {
- var result = reader.ReadDateTime();
- result.Should().Be(expectedResult);
- reader.State.Should().Be(BsonReaderState.Initial);
- reader.IsAtEndOfFile().Should().BeTrue();
- }
- }
- [Theory]
- [InlineData("NumberDecimal(1)", "1", "NumberDecimal(\"1\")")]
- [InlineData("NumberDecimal(2147483648)", "2147483648", "NumberDecimal(\"2147483648\")")]
- [InlineData("NumberDecimal(\"1.5\")", "1.5", "NumberDecimal(\"1.5\")")]
- public void TestDecimal128Constructor(string json, string expectedValueString, string expectedJson)
- {
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Decimal128, _bsonReader.ReadBsonType());
- Assert.Equal(Decimal128.Parse(expectedValueString), _bsonReader.ReadDecimal128());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(expectedJson, BsonSerializer.Deserialize<BsonDecimal128>(json).ToJson());
- }
- [Theory]
- [InlineData("{ $numberDecimal : 1 }", "1", "NumberDecimal(\"1\")")]
- [InlineData("{ $numberDecimal : 2147483648 }", "2147483648", "NumberDecimal(\"2147483648\")")]
- [InlineData("{ $numberDecimal : \"1.5\" }", "1.5", "NumberDecimal(\"1.5\")")]
- [InlineData("{ $numberDecimal : \"Infinity\" }", "Infinity", "NumberDecimal(\"Infinity\")")]
- [InlineData("{ $numberDecimal : \"-Infinity\" }", "-Infinity", "NumberDecimal(\"-Infinity\")")]
- [InlineData("{ $numberDecimal : \"NaN\" }", "NaN", "NumberDecimal(\"NaN\")")]
- public void TestDecimal128ExtendedJson(string json, string expectedValueString, string expectedJson)
- {
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Decimal128, _bsonReader.ReadBsonType());
- Assert.Equal(Decimal128.Parse(expectedValueString), _bsonReader.ReadDecimal128());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(expectedJson, BsonSerializer.Deserialize<BsonDecimal128>(json).ToJson());
- }
- [Fact]
- public void TestDocumentEmpty()
- {
- var json = "{ }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
- _bsonReader.ReadStartDocument();
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReadEndDocument();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
- }
- [Fact]
- public void TestDocumentNested()
- {
- var json = "{ \"a\" : { \"x\" : 1 }, \"y\" : 2 }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
- _bsonReader.ReadStartDocument();
- Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
- Assert.Equal("a", _bsonReader.ReadName());
- _bsonReader.ReadStartDocument();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal("x", _bsonReader.ReadName());
- Assert.Equal(1, _bsonReader.ReadInt32());
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReadEndDocument();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal("y", _bsonReader.ReadName());
- Assert.Equal(2, _bsonReader.ReadInt32());
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReadEndDocument();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
- }
- [Fact]
- public void TestDocumentOneElement()
- {
- var json = "{ \"x\" : 1 }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
- _bsonReader.ReadStartDocument();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal("x", _bsonReader.ReadName());
- Assert.Equal(1, _bsonReader.ReadInt32());
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReadEndDocument();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
- }
- [Fact]
- public void TestDocumentTwoElements()
- {
- var json = "{ \"x\" : 1, \"y\" : 2 }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
- _bsonReader.ReadStartDocument();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal("x", _bsonReader.ReadName());
- Assert.Equal(1, _bsonReader.ReadInt32());
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal("y", _bsonReader.ReadName());
- Assert.Equal(2, _bsonReader.ReadInt32());
- Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
- _bsonReader.ReadEndDocument();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
- }
- [Theory]
- [InlineData("1.0", 1.0)]
- [InlineData("-1.0", -1.0)]
- [InlineData("1.5", 1.5)]
- [InlineData("{ $numberDouble : 1 }", 1.0)]
- [InlineData("{ $numberDouble : 1.0 }", 1.0)]
- [InlineData("{ $numberDouble : \"1\" }", 1.0)]
- [InlineData("{ $numberDouble : \"-1\" }", -1.0)]
- [InlineData("{ $numberDouble : \"1.5\" }", 1.5)]
- [InlineData("{ $numberDouble : \"Infinity\" }", double.PositiveInfinity)]
- [InlineData("{ $numberDouble : \"-Infinity\" }", double.NegativeInfinity)]
- [InlineData("{ $numberDouble : \"NaN\" }", double.NaN)]
- public void TestDouble(string json, double expectedValue)
- {
- using (var reader = new JsonReader(json))
- {
- reader.ReadBsonType().Should().Be(BsonType.Double);
- reader.ReadDouble().Should().Be(expectedValue);
- reader.State.Should().Be(BsonReaderState.Initial);
- reader.IsAtEndOfFile().Should().BeTrue();
- }
- }
- [Theory]
- [InlineData("{ $numberDouble")]
- [InlineData("{ $numberDouble :")]
- [InlineData("{ $numberDouble : \"1\"")]
- public void TestDoubleEndOfFile(string json)
- {
- using (var reader = new JsonReader(json))
- {
- var exception = Record.Exception(() => reader.ReadDouble());
- exception.Should().BeOfType<FormatException>();
- }
- }
- [Theory]
- [InlineData("{ $numberDouble [")]
- [InlineData("{ $numberDouble : [")]
- [InlineData("{ $numberDouble : \"1\" [")]
- public void TestDoubleInvalidToken(string json)
- {
- using (var reader = new JsonReader(json))
- {
- var exception = Record.Exception(() => reader.ReadDouble());
- exception.Should().BeOfType<FormatException>();
- }
- }
- [Fact]
- public void TestDoubleRoundTrip()
- {
- var json = "1.5";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Double, _bsonReader.ReadBsonType());
- Assert.Equal(1.5, _bsonReader.ReadDouble());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<double>(json).ToJson());
- }
- [Theory]
- [ParameterAttributeData]
- [ResetGuidModeAfterTest]
- public void TestGuid(
- [ClassValues(typeof(GuidModeValues))] GuidMode mode)
- {
- mode.Set();
- #pragma warning disable 618
- var guid = new Guid("B5F21E0C2A0D42D6AD03D827008D8AB6");
- var json = "CSUUID(\"B5F21E0C2A0D42D6AD03D827008D8AB6\")";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType());
- var binaryData = _bsonReader.ReadBinaryData();
- Assert.True(binaryData.Bytes.SequenceEqual(guid.ToByteArray()));
- Assert.Equal(BsonBinarySubType.UuidLegacy, binaryData.SubType);
- if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
- {
- Assert.Equal(GuidRepresentation.CSharpLegacy, binaryData.GuidRepresentation);
- }
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var guidRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? BsonDefaults.GuidRepresentation : GuidRepresentation.Unspecified;
- if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && guidRepresentation != GuidRepresentation.Unspecified)
- {
- string expected;
- switch (guidRepresentation)
- {
- case GuidRepresentation.CSharpLegacy: expected = "CSUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
- case GuidRepresentation.JavaLegacy: expected = "JUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
- case GuidRepresentation.PythonLegacy: expected = "PYUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
- case GuidRepresentation.Standard: expected = "UUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
- default: throw new Exception("Unexpected GuidRepresentation.");
- }
- Assert.Equal(expected, BsonSerializer.Deserialize<Guid>(json).ToJson(new JsonWriterSettings()));
- }
- else
- {
- var exception = Record.Exception(() => guid.ToJson(new JsonWriterSettings()));
- exception.Should().BeOfType<BsonSerializationException>();
- }
- #pragma warning restore 618
- }
- [Fact]
- public void TestHexData()
- {
- var expectedBytes = new byte[] { 0x01, 0x23 };
- var json = "HexData(0, \"123\")";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType());
- var bytes = _bsonReader.ReadBytes();
- Assert.True(expectedBytes.SequenceEqual(bytes));
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var expectedJson = "new BinData(0, \"ASM=\")";
- Assert.Equal(expectedJson, BsonSerializer.Deserialize<byte[]>(json).ToJson());
- }
- [Fact]
- public void TestInt32()
- {
- var json = "123";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal(123, _bsonReader.ReadInt32());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<int>(json).ToJson());
- }
- [Theory]
- [InlineData("{ $numberInt : 1 }", 1)]
- [InlineData("{ $numberInt : -2147483648 }", -2147483648)]
- [InlineData("{ $numberInt : 2147483647 }", 2147483647)]
- [InlineData("{ $numberInt : \"1\" }", 1)]
- [InlineData("{ $numberInt : \"-2147483648\" }", -2147483648)]
- [InlineData("{ $numberInt : \"2147483647\" }", 2147483647)]
- public void TestInt32ExtendedJson(string json, int expectedResult)
- {
- using (var reader = new JsonReader(json))
- {
- var result = reader.ReadInt32();
- result.Should().Be(expectedResult);
- reader.State.Should().Be(BsonReaderState.Initial);
- reader.IsAtEndOfFile().Should().BeTrue();
- }
- }
- [Theory]
- // truncated input
- [InlineData("{ $numberInt")]
- [InlineData("{ $numberInt :")]
- [InlineData("{ $numberInt : 1")]
- // invalid extended json
- [InlineData("{ $numberInt ,")]
- [InlineData("{ $numberInt : \"abc\"")]
- [InlineData("{ $numberInt : 1,")]
- public void TestInt32ExtendedJsonInvalid(string json)
- {
- using (var reader = new JsonReader(json))
- {
- var execption = Record.Exception(() => reader.ReadInt32());
- execption.Should().BeOfType<FormatException>();
- }
- }
- [Theory]
- [InlineData("Number(123)")]
- [InlineData("NumberInt(123)")]
- public void TestInt32Constructor(string json)
- {
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal(123, _bsonReader.ReadInt32());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var canonicalJson = "123";
- Assert.Equal(canonicalJson, BsonSerializer.Deserialize<int>(new StringReader(json)).ToJson());
- }
- [Theory]
- [InlineData("{ $numberLong: 1 }", 1L)]
- [InlineData("{ $numberLong: -9223372036854775808 }", -9223372036854775808L)]
- [InlineData("{ $numberLong: 9223372036854775807 }", 9223372036854775807L)]
- [InlineData("{ $numberLong: \"1\" }", 1L)]
- [InlineData("{ $numberLong: \"-9223372036854775808\" }", -9223372036854775808L)]
- [InlineData("{ $numberLong: \"9223372036854775807\" }", 9223372036854775807L)]
- [InlineData("NumberLong(1)", 1L)]
- [InlineData("NumberLong(-9223372036854775808)", -9223372036854775808L)]
- [InlineData("NumberLong(9223372036854775807)", 9223372036854775807L)]
- [InlineData("NumberLong(\"1\")", 1L)]
- [InlineData("NumberLong(\"-9223372036854775808\")", -9223372036854775808L)]
- [InlineData("NumberLong(\"9223372036854775807\")", 9223372036854775807L)]
- public void TestInt64(string json, long expectedResult)
- {
- using (var reader = new JsonReader(json))
- {
- var result = reader.ReadInt64();
- result.Should().Be(expectedResult);
- reader.State.Should().Be(BsonReaderState.Initial);
- reader.IsAtEndOfFile().Should().BeTrue();
- }
- }
- [Fact]
- public void TestInt64ConstructorQuoted()
- {
- var json = "NumberLong(\"123456789012\")";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
- Assert.Equal(123456789012, _bsonReader.ReadInt64());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<long>(json).ToJson());
- }
- [Fact]
- public void TestInt64ConstructorUnqutoed()
- {
- var json = "NumberLong(123)";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
- Assert.Equal(123, _bsonReader.ReadInt64());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<long>(json).ToJson());
- }
- [Fact]
- public void TestIsAtEndOfFileWithTwoArrays()
- {
- var json = "[1,2][1,2]";
- using (var jsonReader = new JsonReader(json))
- {
- var count = 0;
- jsonReader.State.Should().Be(BsonReaderState.Initial);
- while (!jsonReader.IsAtEndOfFile())
- {
- var array = BsonSerializer.Deserialize<BsonArray>(jsonReader);
- jsonReader.State.Should().Be(BsonReaderState.Initial);
- var expected = new BsonArray { 1, 2 };
- Assert.Equal(expected, array);
- count += 1;
- }
- Assert.Equal(2, count);
- }
- }
- [Fact]
- public void TestIsAtEndOfFileWithTwoDocuments()
- {
- var json = "{x:1}{x:1}";
- using (var jsonReader = new JsonReader(json))
- {
- var count = 0;
- jsonReader.State.Should().Be(BsonReaderState.Initial);
- while (!jsonReader.IsAtEndOfFile())
- {
- var document = BsonSerializer.Deserialize<BsonDocument>(jsonReader);
- jsonReader.State.Should().Be(BsonReaderState.Initial);
- var expected = new BsonDocument("x", 1);
- Assert.Equal(expected, document);
- count += 1;
- }
- Assert.Equal(2, count);
- }
- }
- [Fact]
- public void TestInt64ExtendedJson()
- {
- var json = "{ \"$numberLong\" : \"123\" }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
- Assert.Equal(123, _bsonReader.ReadInt64());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var canonicalJson = "NumberLong(123)";
- Assert.Equal(canonicalJson, BsonSerializer.Deserialize<long>(new StringReader(json)).ToJson());
- }
- [Fact]
- public void TestJavaScript()
- {
- string json = "{ \"$code\" : \"function f() { return 1; }\" }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.JavaScript, _bsonReader.ReadBsonType());
- Assert.Equal("function f() { return 1; }", _bsonReader.ReadJavaScript());
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScript>(json).ToJson());
- }
- [Fact]
- public void TestJavaScriptWithScope()
- {
- string json = "{ \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.JavaScriptWithScope, _bsonReader.ReadBsonType());
- Assert.Equal("function f() { return n; }", _bsonReader.ReadJavaScriptWithScope());
- _bsonReader.ReadStartDocument();
- Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
- Assert.Equal("n", _bsonReader.ReadName());
- Assert.Equal(1, _bsonReader.ReadInt32());
- _bsonReader.ReadEndDocument();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScriptWithScope>(json).ToJson());
- }
- [Theory]
- [InlineData("{ $maxKey : 1 }")]
- [InlineData("MaxKey")]
- public void TestMaxKey(string json)
- {
- using (var reader = new JsonReader(json))
- {
- reader.ReadMaxKey();
- reader.State.Should().Be(BsonReaderState.Initial);
- reader.IsAtEndOfFile().Should().BeTrue();
- }
- }
- [Fact]
- public void TestMaxKeyExtendedJson()
- {
- var json = "{ \"$maxkey\" : 1 }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
- _bsonReader.ReadMaxKey();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var canonicalJson = "MaxKey";
- Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
- }
- [Fact]
- public void TestMaxKeyExtendedJsonWithCapitalK()
- {
- var json = "{ \"$maxKey\" : 1 }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
- _bsonReader.ReadMaxKey();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var canonicalJson = "MaxKey";
- Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
- }
- [Fact]
- public void TestMaxKeyKeyword()
- {
- var json = "MaxKey";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
- _bsonReader.ReadMaxKey();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
- }
- [Theory]
- [InlineData("{ $minKey : 1 }")]
- [InlineData("MinKey")]
- public void TestMinKey(string json)
- {
- using (var reader = new JsonReader(json))
- {
- reader.ReadMinKey();
- reader.State.Should().Be(BsonReaderState.Initial);
- reader.IsAtEndOfFile().Should().BeTrue();
- }
- }
- [Fact]
- public void TestMinKeyExtendedJson()
- {
- var json = "{ \"$minkey\" : 1 }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
- _bsonReader.ReadMinKey();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var canonicalJson = "MinKey";
- Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
- }
- [Fact]
- public void TestMinKeyExtendedJsonWithCapitalK()
- {
- var json = "{ \"$minKey\" : 1 }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
- _bsonReader.ReadMinKey();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- var canonicalJson = "MinKey";
- Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
- }
- [Fact]
- public void TestMinKeyKeyword()
- {
- var json = "MinKey";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
- _bsonReader.ReadMinKey();
- Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
- Assert.True(_bsonReader.IsAtEndOfFile());
- }
- Assert.Equal(json, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
- }
- [Fact]
- public void TestNestedArray()
- {
- var json = "{ \"a\" : [1, 2] }";
- using (_bsonReader = new JsonReader(json))
- {
- Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
- _bsonReader.ReadStart…
Large files files are truncated, but you can click here to view the full file