public async Task WriteOidAsync() { 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 }; await writer.WriteStartObjectAsync(); await writer.WritePropertyNameAsync("_oid"); writer.WriteObjectId(oid); await writer.WriteEndObjectAsync(); string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("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.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.StartObject, reader.TokenType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.Bytes, reader.TokenType); CollectionAssert.AreEquivalent(oid, (byte[])reader.Value); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.EndObject, reader.TokenType); }
public async Task WriteBytesAsync() { byte[] data = Encoding.UTF8.GetBytes("Hello world!"); MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); await writer.WriteStartArrayAsync(); await writer.WriteValueAsync("a"); await writer.WriteValueAsync("b"); await writer.WriteValueAsync(data); await writer.WriteEndArrayAsync(); await writer.FlushAsync(); 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.AreEqual(expected, bson); BsonReader reader = new BsonReader(new MemoryStream(ms.ToArray())); reader.ReadRootValueAsArray = true; await reader.ReadAsync(); await reader.ReadAsync(); await reader.ReadAsync(); await reader.ReadAsync(); Assert.AreEqual(JsonToken.Bytes, reader.TokenType); CollectionAssert.AreEquivalent(data, (byte[])reader.Value); }
public async Task WriteBigIntegerAsync() { BigInteger i = BigInteger.Parse("1999999999999999999999999999999999999999999999999999999999990"); MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); await writer.WriteStartObjectAsync(); await writer.WritePropertyNameAsync("Blah"); await writer.WriteValueAsync(i); await writer.WriteEndObjectAsync(); string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("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.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.StartObject, reader.TokenType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.Bytes, reader.TokenType); CollectionAssert.AreEqual(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.AreEqual(i, new BigInteger((byte[])reader.Value)); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.EndObject, reader.TokenType); Assert.IsFalse(await reader.ReadAsync()); }
public async Task WriteDateTimesAsync() { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); writer.DateTimeKindHandling = DateTimeKind.Unspecified; await writer.WriteStartArrayAsync(); await writer.WriteValueAsync(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc)); await writer.WriteValueAsync(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Local)); await writer.WriteValueAsync( new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Unspecified) ); await writer.WriteEndArrayAsync(); ms.Seek(0, SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); reader.ReadRootValueAsArray = true; reader.DateTimeKindHandling = DateTimeKind.Utc; Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.StartArray, reader.TokenType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.Date, reader.TokenType); Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.Date, reader.TokenType); Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.Date, reader.TokenType); Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.IsFalse(await reader.ReadAsync()); }
public async Task WriteReadEmptyAndNullStringsAsync() { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); await writer.WriteStartArrayAsync(); await writer.WriteValueAsync("Content!"); await writer.WriteValueAsync(""); await writer.WriteValueAsync((string)null); await writer.WriteEndArrayAsync(); ms.Seek(0, SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); reader.ReadRootValueAsArray = true; Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.StartArray, reader.TokenType); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.String, reader.TokenType); Assert.AreEqual("Content!", reader.Value); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.String, reader.TokenType); Assert.AreEqual("", reader.Value); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.Null, reader.TokenType); Assert.AreEqual(null, reader.Value); Assert.IsTrue(await reader.ReadAsync()); Assert.AreEqual(JsonToken.EndArray, reader.TokenType); Assert.IsFalse(await reader.ReadAsync()); }