Ejemplo n.º 1
0
        public async Task WriteRawValueAsync()
        {
            await ExceptionAssert.ThrowsAsync <JsonWriterException>(async() =>
            {
                MemoryStream ms       = new MemoryStream();
                BsonDataWriter writer = new BsonDataWriter(ms);

                await writer.WriteStartArrayAsync();
                await writer.WriteRawValueAsync("fail");
            }, "Cannot write raw JSON as BSON. Path ''.");
        }
Ejemplo n.º 2
0
        public async Task WriteDoubleAsync()
        {
            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(ms);

            await writer.WriteStartArrayAsync();

            await writer.WriteValueAsync(99.99d);

            await writer.WriteEndAsync();

            string bson = BytesToHex(ms.ToArray());

            Assert.AreEqual("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00", bson);
        }
Ejemplo n.º 3
0
        public async Task DateTimeZoneHandlingAsync()
        {
            MemoryStream ms     = new MemoryStream();
            JsonWriter   writer = new BsonDataWriter(ms)
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            };

            await writer.WriteStartArrayAsync();

            await writer.WriteValueAsync(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified));

            await writer.WriteEndArrayAsync();

            Assert.AreEqual("10-00-00-00-09-30-00-C8-88-07-6B-DC-00-00-00-00", (BitConverter.ToString(ms.ToArray())));
        }
Ejemplo n.º 4
0
        public async Task WriteGuidAsync()
        {
            Guid g = new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705");

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

            await writer.WriteStartArrayAsync();

            await writer.WriteValueAsync(g);

            await writer.WriteEndAsync();

            string bson = BytesToHex(ms.ToArray());

            Assert.AreEqual("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", bson);
        }
Ejemplo n.º 5
0
        public async Task WriteDateTimesAsync()
        {
            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(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);

            BsonDataReader reader = new BsonDataReader(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());
        }
Ejemplo n.º 6
0
        public async Task WriteValuesAsync()
        {
            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(ms);

            await writer.WriteStartArrayAsync();

            await writer.WriteValueAsync(long.MaxValue);

            await writer.WriteValueAsync((ulong)long.MaxValue);

            await writer.WriteValueAsync(int.MaxValue);

            await writer.WriteValueAsync((uint)int.MaxValue);

            await writer.WriteValueAsync(byte.MaxValue);

            await writer.WriteValueAsync(sbyte.MaxValue);

            await writer.WriteValueAsync('a');

            await writer.WriteValueAsync(decimal.MaxValue);

            await writer.WriteValueAsync(double.MaxValue);

            await writer.WriteValueAsync(float.MaxValue);

            await writer.WriteValueAsync(true);

            await writer.WriteValueAsync(new byte[] { 0, 1, 2, 3, 4 });

            await writer.WriteValueAsync(new DateTimeOffset(2000, 12, 29, 12, 30, 0, TimeSpan.Zero));

            await writer.WriteValueAsync(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc));

            await writer.WriteEndAsync();

            string bson = BytesToHex(ms.ToArray());

            Assert.AreEqual("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-00-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", bson);
        }
Ejemplo n.º 7
0
        public async Task WriteReadEmptyAndNullStringsAsync()
        {
            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(ms);

            await writer.WriteStartArrayAsync();

            await writer.WriteValueAsync("Content!");

            await writer.WriteValueAsync("");

            await writer.WriteValueAsync((string)null);

            await writer.WriteEndArrayAsync();

            ms.Seek(0, SeekOrigin.Begin);

            BsonDataReader reader = new BsonDataReader(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());
        }
Ejemplo n.º 8
0
        public async Task WriteNestedArrayAsync()
        {
            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(ms);
            await writer.WriteStartObjectAsync();

            await writer.WritePropertyNameAsync("_id");

            await writer.WriteValueAsync(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"));

            await writer.WritePropertyNameAsync("a");

            await writer.WriteStartArrayAsync();

            for (int i = 1; i <= 8; i++)
            {
                double value = (i != 5)
                    ? Convert.ToDouble(i)
                    : 5.78960446186581E+77d;

                await writer.WriteValueAsync(value);
            }
            await writer.WriteEndArrayAsync();

            await writer.WritePropertyNameAsync("b");

            await writer.WriteValueAsync("test");

            await writer.WriteEndObjectAsync();

            await writer.FlushAsync();

            ms.Seek(0, SeekOrigin.Begin);

            string expected = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-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";
            string bson     = BytesToHex(ms.ToArray());

            Assert.AreEqual(expected, bson);
        }
Ejemplo n.º 9
0
        public async Task WriteBytesAsync()
        {
            byte[] data = Encoding.UTF8.GetBytes("Hello world!");

            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(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);

            BsonDataReader reader = new BsonDataReader(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);
        }
Ejemplo n.º 10
0
        public async Task WriteArrayBsonFromSiteAsync()
        {
            MemoryStream   ms     = new MemoryStream();
            BsonDataWriter writer = new BsonDataWriter(ms);
            await writer.WriteStartArrayAsync();

            await writer.WriteValueAsync("a");

            await writer.WriteValueAsync("b");

            await writer.WriteValueAsync("c");

            await writer.WriteEndArrayAsync();

            await writer.FlushAsync();

            ms.Seek(0, SeekOrigin.Begin);

            string expected = "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";
            string bson     = BytesToHex(ms.ToArray());

            Assert.AreEqual(expected, bson);
        }