示例#1
0
        public void GetTokens_ArrayRoot_ReturnsDocument()
        {
            // BSON doesn't provide a way to know if the root element is an array

            var input = new byte[]
            {
                0x26, 0x00, 0x00, 0x00,
                0x02, (byte)'0', 0, 0x08, 0x00, 0x00, 0x00, (byte)'a', (byte)'w', (byte)'e', (byte)'s', (byte)'o', (byte)'m', (byte)'e', 0x00,
                0x01, (byte)'1', 0, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x14, 0x40,
                0x10, (byte)'2', 0, 0xC2, 0x07, 0x00, 0x00,
                0x00
            };

            var expected = new[]
            {
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty("0"),
                ModelGrammar.TokenPrimitive("awesome"),
                ModelGrammar.TokenProperty("1"),
                ModelGrammar.TokenPrimitive(5.05),
                ModelGrammar.TokenProperty("2"),
                ModelGrammar.TokenPrimitive(1986),
                ModelGrammar.TokenObjectEnd
            };

            var tokenizer = new BsonReader.BsonTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
示例#2
0
        public void ConvertJson2Bson_HelloWorld_RoundTripsJsonToBsonAndBack()
        {
            // input from example at http://bsonspec.org/#/specification
            var inputText =
                @"{
	""hello"" : ""world""
}";

            var expectedBinary = Encoding.UTF8.GetBytes(
                "\x16\x00\x00\x00\x02hello\x00" +
                "\x06\x00\x00\x00world\x00\x00");

            var expectedText = @"{""hello"":""world""}";

            var jsonTokenizer = new JsonReader.JsonTokenizer();
            var tokens1       = jsonTokenizer.GetTokens(inputText);

            var bsonFormatter = new BsonWriter.BsonFormatter();
            var actualBinary  = bsonFormatter.Format(tokens1);

            Assert.Equal(expectedBinary, actualBinary);

            var bsonTokenizer = new BsonReader.BsonTokenizer();
            var tokens2       = bsonTokenizer.GetTokens(actualBinary);

            var jsonFormatter = new JsonWriter.JsonFormatter(new DataWriterSettings {
                PrettyPrint = false
            });
            var actualText = jsonFormatter.Format(tokens2);

            Assert.Equal(expectedText, actualText);
        }
示例#3
0
        public void ConvertJson2Bson_BooleanValue_RoundTripsJsonToBsonAndBack()
        {
            // input from example at http://codebetter.com/blogs/karlseguin/archive/2010/03/05/bson-serialization.aspx
            var inputText = @"{valid:true}";

            var expectedBinary = new byte[]
            {
                13, 0, 0, 0, 8, 118, 97, 108, 105, 100, 0, 1, 0
            };

            var expectedText =
                @"{
	""valid"" : true
}";

            var jsonTokenizer = new JsonReader.JsonTokenizer();
            var tokens1       = jsonTokenizer.GetTokens(inputText);

            var bsonFormatter = new BsonWriter.BsonFormatter();
            var actualBinary  = bsonFormatter.Format(tokens1);

            Assert.Equal(expectedBinary, actualBinary);

            var bsonTokenizer = new BsonReader.BsonTokenizer();
            var tokens2       = bsonTokenizer.GetTokens(actualBinary);

            var jsonFormatter = new JsonWriter.JsonFormatter(new DataWriterSettings {
                PrettyPrint = true
            });
            var actualText = jsonFormatter.Format(tokens2);

            Assert.Equal(expectedText, actualText);
        }
示例#4
0
        public void GetTokens_EmptyStream_ReturnsEmptySequence()
        {
            var input    = Stream.Null;
            var expected = new Token <ModelTokenType> [0];

            var tokenizer = new BsonReader.BsonTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
示例#5
0
        public void GetTokens_NullStream_ThrowsArgumentNullException()
        {
            var input    = (Stream)null;
            var expected = new Token <ModelTokenType> [0];

            var tokenizer = new BsonReader.BsonTokenizer();

            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                var actual = tokenizer.GetTokens(input).ToArray();
            });

            Assert.Equal("stream", ex.ParamName);
        }
示例#6
0
        public void ConvertJson2Bson_ArrayAsProperty_RoundTripsJsonToBsonAndBack()
        {
            // input from example at http://bsonspec.org/#/specification
            var inputText =
                @"{
	""BSON"" : [
		""awesome"",
		5.05,
		1986
	]
}";

            var expectedBinary = new byte[]
            {
                0x31, 0x00, 0x00, 0x00,
                0x04, (byte)'B', (byte)'S', (byte)'O', (byte)'N', 0x00,
                0x26, 0x00, 0x00, 0x00,
                0x02, (byte)'0', 0, 0x08, 0x00, 0x00, 0x00, (byte)'a', (byte)'w', (byte)'e', (byte)'s', (byte)'o', (byte)'m', (byte)'e', 0x00,
                0x01, (byte)'1', 0, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x14, 0x40,
                0x10, (byte)'2', 0, 0xC2, 0x07, 0x00, 0x00,
                0x00,
                0x00
            };

            var expectedText = @"{""BSON"":[""awesome"",5.05,1986]}";

            var jsonTokenizer = new JsonReader.JsonTokenizer();
            var tokens1       = jsonTokenizer.GetTokens(inputText);

            var bsonFormatter = new BsonWriter.BsonFormatter();
            var actualBinary  = bsonFormatter.Format(tokens1);

            Assert.Equal(expectedBinary, actualBinary);

            var bsonTokenizer = new BsonReader.BsonTokenizer();
            var tokens2       = bsonTokenizer.GetTokens(actualBinary);

            var jsonFormatter = new JsonWriter.JsonFormatter(new DataWriterSettings {
                PrettyPrint = false
            });
            var actualText = jsonFormatter.Format(tokens2);

            Assert.Equal(expectedText, actualText);
        }
示例#7
0
        public void GetTokens_HelloWorld_ReturnsDocument()
        {
            // input from example at http://bsonspec.org/#/specification
            var input = Encoding.UTF8.GetBytes(
                "\x16\x00\x00\x00\x02hello\x00" +
                "\x06\x00\x00\x00world\x00\x00");

            var expected = new[]
            {
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty("hello"),
                ModelGrammar.TokenPrimitive("world"),
                ModelGrammar.TokenObjectEnd
            };

            var tokenizer = new BsonReader.BsonTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
示例#8
0
        public void GetTokens_ArrayAsProperty_ReturnsDocument()
        {
            // input from example at http://bsonspec.org/#/specification

            // Encoding doesn't seem to like control chars
            //var input = Encoding.UTF8.GetBytes(
            //    "1\x00\x00\x00\x04BSON\x00&\x00"+
            //    "\x00\x00\x020\x00\x08\x00\x00"+
            //    "\x00awesome\x00\x011\x00333333"+
            //    "\x14@\x102\x00\xc2\x07\x00\x00"+
            //    "\x00\x00");

            var input = new byte[]
            {
                0x31, 0x00, 0x00, 0x00,
                0x04, (byte)'B', (byte)'S', (byte)'O', (byte)'N', 0x00,
                0x26, 0x00, 0x00, 0x00,
                0x02, (byte)'0', 0, 0x08, 0x00, 0x00, 0x00, (byte)'a', (byte)'w', (byte)'e', (byte)'s', (byte)'o', (byte)'m', (byte)'e', 0x00,
                0x01, (byte)'1', 0, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x14, 0x40,
                0x10, (byte)'2', 0, 0xC2, 0x07, 0x00, 0x00,
                0x00,
                0x00
            };

            var expected = new[]
            {
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty("BSON"),
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenPrimitive("awesome"),
                ModelGrammar.TokenPrimitive(5.05),
                ModelGrammar.TokenPrimitive(1986),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenObjectEnd
            };

            var tokenizer = new BsonReader.BsonTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
示例#9
0
        public void GetTokens_BooleanValue_ReturnsDocument()
        {
            // input from example at http://codebetter.com/blogs/karlseguin/archive/2010/03/05/bson-serialization.aspx
            var input = new byte[]
            {
                13, 0, 0, 0, 8, 118, 97, 108, 105, 100, 0, 1, 0
            };

            var expected = new[]
            {
                ModelGrammar.TokenObjectBeginUnnamed,
                ModelGrammar.TokenProperty("valid"),
                ModelGrammar.TokenTrue,
                ModelGrammar.TokenObjectEnd
            };

            var tokenizer = new BsonReader.BsonTokenizer();
            var actual    = tokenizer.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }