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);
		}
		public void Format_HelloWorld_ReturnsDocument()
		{
			// input from example at http://bsonspec.org/#/specification
			var input = new[]
			{
				ModelGrammar.TokenObjectBegin("Object"),
				ModelGrammar.TokenProperty("hello"),
				ModelGrammar.TokenPrimitive("world"),
				ModelGrammar.TokenObjectEnd
			};

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

			var formatter = new BsonWriter.BsonFormatter();
			var actual = formatter.Format(input);

			Assert.Equal(expected, actual);
		}
		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);
		}
		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);
		}
		public void Format_ArrayAsProperty_ReturnsDocument()
		{
			// input from example at http://bsonspec.org/#/specification
			var input = new[]
		    {
				ModelGrammar.TokenObjectBeginUnnamed,
				ModelGrammar.TokenProperty("BSON"),
				ModelGrammar.TokenArrayBeginUnnamed,
				ModelGrammar.TokenPrimitive("awesome"),
				ModelGrammar.TokenPrimitive(5.05),
				ModelGrammar.TokenPrimitive(1986),
				ModelGrammar.TokenArrayEnd,
				ModelGrammar.TokenObjectEnd
		    };

			// Encoding doesn't seem to like control chars
			//var expected = 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 expected = 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 formatter = new BsonWriter.BsonFormatter();
			var actual = formatter.Format(input);

		    Assert.Equal(expected, actual);
		}
		public void Format_EmptySequence_ReturnsEmptyByteArray()
		{
			var expected = new byte[0];
			var input = new Token<ModelTokenType>[0];

			var formatter = new BsonWriter.BsonFormatter();
			var actual = formatter.Format(input);

			Assert.Equal(expected, actual);
		}
		public void Format_NullTokens_ThrowsArgumentNullException()
		{
			var input = (IEnumerable<Token<ModelTokenType>>)null;

			var formatter = new BsonWriter.BsonFormatter();

			ArgumentNullException ex = Assert.Throws<ArgumentNullException>(
				delegate()
				{
					var actual = formatter.Format(input);
				});

			Assert.Equal("tokens", ex.ParamName);
		}
		public void Format_NullStream_ThrowsArgumentNullException()
		{
			var input = new Token<ModelTokenType>[0];

			var formatter = new BsonWriter.BsonFormatter();

			ArgumentNullException ex = Assert.Throws<ArgumentNullException>(
				delegate()
				{
					formatter.Format(input, null);
				});

			Assert.Equal("stream", ex.ParamName);
		}
		public void Format_ObjectOneNamespacedProperty_CorrectlyIgnoresNamespace()
		{
			var input = new[]
			{
				ModelGrammar.TokenObjectBeginUnnamed,
				ModelGrammar.TokenProperty(new DataName("key", null, "http://json.org")),
				ModelGrammar.TokenPrimitive("value"),
				ModelGrammar.TokenObjectEnd
			};

			var expected = Encoding.UTF8.GetBytes(
				"\x14\x00\x00\x00\x02key\x00"+
				"\x06\x00\x00\x00value\x00\x00");

			var formatter = new BsonWriter.BsonFormatter();
			var actual = formatter.Format(input);

			Assert.Equal(expected, actual);
		}
示例#10
0
		public void Format_BooleanValue_ReturnsDocument()
		{
			// input from example at http://codebetter.com/blogs/karlseguin/archive/2010/03/05/bson-serialization.aspx
			var input = new[]
			{
				ModelGrammar.TokenObjectBegin("Object"),
				ModelGrammar.TokenProperty("valid"),
				ModelGrammar.TokenTrue,
				ModelGrammar.TokenObjectEnd
			};

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

			var formatter = new BsonWriter.BsonFormatter();
			var actual = formatter.Format(input);

			Assert.Equal(expected, actual);
		}