public void DeserializeLargeBsonObject()
        {
            byte[] data = System.IO.File.ReadAllBytes(@"SpaceShipV2.bson");

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            JObject o = (JObject)JToken.ReadFrom(reader);

            Assert.Equal("1", (string)o["$id"]);
        }
        public void Deserialize()
        {
            byte[] bson = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");

            JsonSerializer serializer = new JsonSerializer();

            BsonReader reader = new BsonReader(new MemoryStream(bson));
            ObjectIdTestClass c = serializer.Deserialize<ObjectIdTestClass>(reader);

            Assert.Equal(c.Id.Value, HexToBytes("4ABBED9D1D8B0F0218000001"));
            Assert.Equal(c.Test, "1234£56");
        }
        public void Example()
        {
            #region Usage
            byte[] data = Convert.FromBase64String("MQAAAAJOYW1lAA8AAABNb3ZpZSBQcmVtaWVyZQAJU3RhcnREYXRlAMDgKWE8AQAAAA==");

            MemoryStream ms = new MemoryStream(data);
            using (BsonReader reader = new BsonReader(ms))
            {
                JsonSerializer serializer = new JsonSerializer();

                Event e = serializer.Deserialize<Event>(reader);

                Console.WriteLine(e.Name);
                // Movie Premiere
            }
            #endregion
        }
        public void Example()
        {
            #region Usage
            byte[] data = Convert.FromBase64String("KQAAAAJuYW1lMQAHAAAAdmFsdWUxAAJuYW1lMgAHAAAAdmFsdWUyAAA=");
            MemoryStream ms = new MemoryStream(data);

            JObject o;
            using (BsonReader reader = new BsonReader(ms))
            {
                o = (JObject)JToken.ReadFrom(reader);
            }

            string value = (string)o["name1"];

            Console.WriteLine(value);
            // value1
            #endregion
        }
        public void Example()
        {
            //IList<Event> e = new List<Event>
            //  {
            //    new Event {StartDate = new DateTime(2013, 3, 31), Name = "Easter"}
            //  };
            //MemoryStream ms1 = new MemoryStream();
            //using (BsonWriter writer = new BsonWriter(ms1))
            //{
            //  JsonSerializer serializer = new JsonSerializer();

            //  serializer.Serialize(writer, e);
            //}

            //string ss = Convert.ToBase64String(ms1.ToArray());
            //Console.WriteLine(ss);

            #region Usage
            string s = "MQAAAAMwACkAAAACTmFtZQAHAAAARWFzdGVyAAlTdGFydERhdGUAgDf0uj0BAAAAAA==";
            byte[] data = Convert.FromBase64String(s);

            MemoryStream ms = new MemoryStream(data);
            using (BsonReader reader = new BsonReader(ms))
            {
                reader.ReadRootValueAsArray = true;

                JsonSerializer serializer = new JsonSerializer();

                IList<Event> events = serializer.Deserialize<IList<Event>>(reader);

                Console.WriteLine(events.Count);
                // 1

                Console.WriteLine(events[0].Name);
                // Easter
            }
            #endregion
        }
        public void WriteBytes()
        {
            byte[] data = Encoding.UTF8.GetBytes("Hello world!");

            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);
            writer.WriteStartArray();
            writer.WriteValue("a");
            writer.WriteValue("b");
            writer.WriteValue(data);
            writer.WriteEndArray();

            writer.Flush();

            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.Equal(expected, bson);

            BsonReader reader = new BsonReader(new MemoryStream(ms.ToArray()));
            reader.ReadRootValueAsArray = true;
            reader.Read();
            reader.Read();
            reader.Read();
            reader.Read();
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(data, (byte[])reader.Value);
        }
        public void ConvertRegexWithAllOptionsBson()
        {
            Regex regex = new Regex(
                "/",
                RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture);

            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);
            JsonSerializer serializer = new JsonSerializer();
            serializer.Converters.Add(new RegexConverter());

            serializer.Serialize(writer, new RegexTestClass { Regex = regex });

            string expected = "14-00-00-00-0B-52-65-67-65-78-00-2F-00-69-6D-73-75-78-00-00";
            string bson = BytesToHex(ms.ToArray());

            Assert.Equal(expected, bson);

            ms.Seek(0, SeekOrigin.Begin);
            BsonReader reader = new BsonReader(ms);
            serializer.Converters.Add(new RegexConverter());

            RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);

            Assert.Equal("/", c.Regex.ToString());
            Assert.Equal(RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture, c.Regex.Options);
        }
        public void ReadCode()
        {
            string hexdoc = "1A-00-00-00-0D-63-6F-64-65-00-0B-00-00-00-49-20-61-6D-20-63-6F-64-65-21-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("code", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal(@"I am code!", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
        public void WriteBigInteger()
        {
            BigInteger i = BigInteger.Parse("1999999999999999999999999999999999999999999999999999999999990");

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

            writer.WriteStartObject();
            writer.WritePropertyName("Blah");
            writer.WriteValue(i);
            writer.WriteEndObject();

            string bson = BytesToHex(ms.ToArray());
            Assert.Equal("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.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);
            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(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.Equal(i, new BigInteger((byte[])reader.Value));

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
        }
Esempio n. 10
0
        public void WriteDateTimes()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);
            writer.DateTimeKindHandling = DateTimeKind.Unspecified;

            writer.WriteStartArray();
            writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc));
            writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Local));
            writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Unspecified));
            writer.WriteEndArray();

            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            reader.ReadRootValueAsArray = true;
            reader.DateTimeKindHandling = DateTimeKind.Utc;

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Date, reader.TokenType);
            Assert.Equal(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Date, reader.TokenType);
            Assert.Equal(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Date, reader.TokenType);
            Assert.Equal(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
        }
Esempio n. 11
0
        public void SerializeEmptyAndNullStrings()
        {
            Product p = new Product();
            p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
            p.Name = null;
            p.Price = 9.95m;
            p.Sizes = new[] { "Small", "", null };

            MemoryStream ms = new MemoryStream();
            JsonSerializer serializer = new JsonSerializer();

            BsonWriter writer = new BsonWriter(ms);
            serializer.Serialize(writer, p);

            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            Product deserializedProduct = serializer.Deserialize<Product>(reader);

            Console.WriteLine(deserializedProduct.Name);

            Assert.Equal(null, deserializedProduct.Name);
            Assert.Equal(9.95m, deserializedProduct.Price);
            Assert.Equal(3, deserializedProduct.Sizes.Length);
            Assert.Equal("Small", deserializedProduct.Sizes[0]);
            Assert.Equal("", deserializedProduct.Sizes[1]);
            Assert.Equal(null, deserializedProduct.Sizes[2]);
        }
Esempio n. 12
0
        public void Example()
        {
            Product p = new Product();
            p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
            p.Name = "Carlos' Spicy Wieners";
            p.Price = 9.95m;
            p.Sizes = new[] { "Small", "Medium", "Large" };

            MemoryStream ms = new MemoryStream();
            JsonSerializer serializer = new JsonSerializer();

            // serialize product to BSON
            BsonWriter writer = new BsonWriter(ms);
            serializer.Serialize(writer, p);

            Console.WriteLine(BitConverter.ToString(ms.ToArray()));
            // 7C-00-00-00-02-4E-61-6D-65-00-16-00-00-00-43-61-72-6C-
            // 6F-73-27-20-53-70-69-63-79-20-57-69-65-6E-65-72-73-00-
            // 09-45-78-70-69-72-79-44-61-74-65-00-E0-51-BD-76-20-01-
            // 00-00-01-50-72-69-63-65-00-66-66-66-66-66-E6-23-40-04-
            // 53-69-7A-65-73-00-2D-00-00-00-02-30-00-06-00-00-00-53-
            // 6D-61-6C-6C-00-02-31-00-07-00-00-00-4D-65-64-69-75-6D-
            // 00-02-32-00-06-00-00-00-4C-61-72-67-65-00-00-00


            ms.Seek(0, SeekOrigin.Begin);

            // deserialize product from BSON
            BsonReader reader = new BsonReader(ms);
            Product deserializedProduct = serializer.Deserialize<Product>(reader);

            Console.WriteLine(deserializedProduct.Name);
            // Carlos' Spicy Wieners


            Assert.Equal("Carlos' Spicy Wieners", deserializedProduct.Name);
            Assert.Equal(9.95m, deserializedProduct.Price);
            Assert.Equal(3, deserializedProduct.Sizes.Length);
        }
Esempio n. 13
0
        public void ReadLargeStrings()
        {
            string bson =
                "4E-02-00-00-02-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-22-01-00-00-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-00";

            BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson)));

            StringBuilder largeStringBuilder = new StringBuilder();
            for (int i = 0; i < 100; i++)
            {
                if (i > 0)
                    largeStringBuilder.Append("-");

                largeStringBuilder.Append(i.ToString(CultureInfo.InvariantCulture));
            }
            string largeString = largeStringBuilder.ToString();

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal(largeString, reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal(largeString, reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
Esempio n. 14
0
 public void ReadEndOfStream()
 {
     BsonReader reader = new BsonReader(new MemoryStream());
     Assert.False(reader.Read());
 }
Esempio n. 15
0
        public void ReadCodeWScope()
        {
            string hexdoc = "75-00-00-00-0F-63-6F-64-65-57-69-74-68-53-63-6F-70-65-00-61-00-00-00-35-00-00-00-66-6F-72-20-28-69-6E-74-20-69-20-3D-20-30-3B-20-69-20-3C-20-31-30-30-30-3B-20-69-2B-2B-29-0D-0A-7B-0D-0A-20-20-61-6C-65-72-74-28-61-72-67-31-29-3B-0D-0A-7D-00-24-00-00-00-02-61-72-67-31-00-15-00-00-00-4A-73-6F-6E-2E-4E-45-54-20-69-73-20-61-77-65-73-6F-6D-65-2E-00-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("codeWithScope", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("$code", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("for (int i = 0; i < 1000; i++)\r\n{\r\n  alert(arg1);\r\n}", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("$scope", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("arg1", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("Json.NET is awesome.", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
Esempio n. 16
0
        public void ReadReference()
        {
            string hexdoc = "1E-00-00-00-0C-6F-69-64-00-04-00-00-00-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("oid", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("$ref", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("oid", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("$id", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, (byte[])reader.Value);
            Assert.Equal(typeof(byte[]), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
Esempio n. 17
0
        public void ReadLong()
        {
            string hexdoc = "13-00-00-00-12-6C-6F-6E-67-00-FF-FF-FF-FF-FF-FF-FF-7F-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("long", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Integer, reader.TokenType);
            Assert.Equal(long.MaxValue, reader.Value);
            Assert.Equal(typeof(long), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
Esempio n. 18
0
        public void ReadUndefined()
        {
            string hexdoc = "10-00-00-00-06-75-6E-64-65-66-69-6E-65-64-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("undefined", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Undefined, reader.TokenType);
            Assert.Equal(null, reader.Value);
            Assert.Equal(null, reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
Esempio n. 19
0
        public void Bson_SupportMultipleContent()
        {
            MemoryStream myStream = new MemoryStream();
            BsonWriter writer = new BsonWriter(myStream);
            JsonSerializer serializer = new JsonSerializer();
            MyTest tst1 = new MyTest
            {
                TimeStamp = new DateTime(2000, 12, 20, 12, 59, 59, DateTimeKind.Utc),
                UserName = "******"
            };
            MyTest tst2 = new MyTest
            {
                TimeStamp = new DateTime(2010, 12, 20, 12, 59, 59, DateTimeKind.Utc),
                UserName = "******"
            };
            serializer.Serialize(writer, tst1);
            serializer.Serialize(writer, tst2);

            myStream.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(myStream)
            {
                SupportMultipleContent = true,
                DateTimeKindHandling = DateTimeKind.Utc
            };

            MyTest tst1A = serializer.Deserialize<MyTest>(reader);

            reader.Read();

            MyTest tst2A = serializer.Deserialize<MyTest>(reader);

            Assert.Equal(tst1.UserName, tst1A.UserName);
            Assert.Equal(tst1.TimeStamp, tst1A.TimeStamp);

            Assert.Equal(tst2.UserName, tst2A.UserName);
            Assert.Equal(tst2.TimeStamp, tst2A.TimeStamp);
        }
Esempio n. 20
0
        public void WriteSerializedStore()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            Store s1 = new Store();
            s1.Color = StoreColor.White;
            s1.Cost = 999.59m;
            s1.Employees = int.MaxValue - 1;
            s1.Open = true;
            s1.product.Add(new Product
            {
                ExpiryDate = new DateTime(2000, 9, 28, 3, 59, 58, DateTimeKind.Local),
                Name = "BSON!",
                Price = -0.1m,
                Sizes = new[] { "First", "Second" }
            });
            s1.Establised = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);

            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(writer, s1);

            ms.Seek(0, SeekOrigin.Begin);
            BsonReader reader = new BsonReader(ms);
            Store s2 = (Store)serializer.Deserialize(reader, typeof(Store));

            Assert.NotEqual(s1, s2);
            Assert.Equal(s1.Color, s2.Color);
            Assert.Equal(s1.Cost, s2.Cost);
            Assert.Equal(s1.Employees, s2.Employees);
            Assert.Equal(s1.Escape, s2.Escape);
            Assert.Equal(s1.Establised, s2.Establised);
            Assert.Equal(s1.Mottos.Count, s2.Mottos.Count);
            Assert.Equal(s1.Mottos.First(), s2.Mottos.First());
            Assert.Equal(s1.Mottos.Last(), s2.Mottos.Last());
            Assert.Equal(s1.Open, s2.Open);
            Assert.Equal(s1.product.Count, s2.product.Count);
            Assert.Equal(s1.RoomsPerFloor.Length, s2.RoomsPerFloor.Length);
            Assert.Equal(s1.Symbol, s2.Symbol);
            Assert.Equal(s1.Width, s2.Width);

            MemoryStream ms1 = new MemoryStream();
            BsonWriter writer1 = new BsonWriter(ms1);

            serializer.Serialize(writer1, s1);

            Assert.Equal(ms.ToArray(), ms1.ToArray());

            string s = JsonConvert.SerializeObject(s1);
            byte[] textData = Encoding.UTF8.GetBytes(s);

            int l1 = textData.Length;
            int l2 = ms.ToArray().Length;

            Console.WriteLine(l1);
            Console.WriteLine(l2);
        }
Esempio n. 21
0
        public void SerializeGoogleGeoCode()
        {
            string json = @"{
  ""name"": ""1600 Amphitheatre Parkway, Mountain View, CA, USA"",
  ""Status"": {
    ""code"": 200,
    ""request"": ""geocode""
  },
  ""Placemark"": [
    {
      ""address"": ""1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA"",
      ""AddressDetails"": {
        ""Country"": {
          ""CountryNameCode"": ""US"",
          ""AdministrativeArea"": {
            ""AdministrativeAreaName"": ""CA"",
            ""SubAdministrativeArea"": {
              ""SubAdministrativeAreaName"": ""Santa Clara"",
              ""Locality"": {
                ""LocalityName"": ""Mountain View"",
                ""Thoroughfare"": {
                  ""ThoroughfareName"": ""1600 Amphitheatre Pkwy""
                },
                ""PostalCode"": {
                  ""PostalCodeNumber"": ""94043""
                }
              }
            }
          }
        },
        ""Accuracy"": 8
      },
      ""Point"": {
        ""coordinates"": [-122.083739, 37.423021, 0]
      }
    }
  ]
}";

            GoogleMapGeocoderStructure jsonGoogleMapGeocoder = JsonConvert.DeserializeObject<GoogleMapGeocoderStructure>(json);

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

            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(writer, jsonGoogleMapGeocoder);

            ms.Seek(0, SeekOrigin.Begin);
            BsonReader reader = new BsonReader(ms);
            GoogleMapGeocoderStructure bsonGoogleMapGeocoder = (GoogleMapGeocoderStructure)serializer.Deserialize(reader, typeof(GoogleMapGeocoderStructure));

            Assert.NotNull(bsonGoogleMapGeocoder);
            Assert.Equal("1600 Amphitheatre Parkway, Mountain View, CA, USA", bsonGoogleMapGeocoder.Name);
            Assert.Equal("200", bsonGoogleMapGeocoder.Status.Code);
            Assert.Equal("geocode", bsonGoogleMapGeocoder.Status.Request);

            IList<Placemark> placemarks = bsonGoogleMapGeocoder.Placemark;
            Assert.NotNull(placemarks);
            Assert.Equal(1, placemarks.Count);

            Placemark placemark = placemarks[0];
            Assert.Equal("1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", placemark.Address);
            Assert.Equal(8, placemark.AddressDetails.Accuracy);
            Assert.Equal("US", placemark.AddressDetails.Country.CountryNameCode);
            Assert.Equal("CA", placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName);
            Assert.Equal("Santa Clara", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName);
            Assert.Equal("Mountain View", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName);
            Assert.Equal("1600 Amphitheatre Pkwy", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName);
            Assert.Equal("94043", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
            Assert.Equal(-122.083739m, placemark.Point.Coordinates[0]);
            Assert.Equal(37.423021m, placemark.Point.Coordinates[1]);
            Assert.Equal(0m, placemark.Point.Coordinates[2]);
        }
Esempio n. 22
0
        public void ReadEmptyStrings()
        {
            string bson = "0C-00-00-00-02-00-01-00-00-00-00-00";

            BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson)));

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
Esempio n. 23
0
        public void WriteOid()
        {
            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 };

            writer.WriteStartObject();
            writer.WritePropertyName("_oid");
            writer.WriteObjectId(oid);
            writer.WriteEndObject();

            string bson = BytesToHex(ms.ToArray());
            Assert.Equal("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.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Bytes, reader.TokenType);
            Assert.Equal(oid, (byte[])reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);
        }
Esempio n. 24
0
        public void WriteAndReadEmptyListsAndDictionaries()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            writer.WriteStartObject();
            writer.WritePropertyName("Arguments");
            writer.WriteStartObject();
            writer.WriteEndObject();
            writer.WritePropertyName("List");
            writer.WriteStartArray();
            writer.WriteEndArray();
            writer.WriteEndObject();

            string bson = BitConverter.ToString(ms.ToArray());

            Assert.Equal("20-00-00-00-03-41-72-67-75-6D-65-6E-74-73-00-05-00-00-00-00-04-4C-69-73-74-00-05-00-00-00-00-00", bson);

            BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson)));

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("Arguments", reader.Value.ToString());

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("List", reader.Value.ToString());

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }
Esempio n. 25
0
        public void WriteReadEmptyAndNullStrings()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            writer.WriteStartArray();
            writer.WriteValue("Content!");
            writer.WriteValue("");
            writer.WriteValue((string)null);
            writer.WriteEndArray();

            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            reader.ReadRootValueAsArray = true;

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartArray, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("Content!", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal("", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.Null, reader.TokenType);
            Assert.Equal(null, reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndArray, reader.TokenType);

            Assert.False(reader.Read());
        }
        public void DeserializeFromBson()
        {
            MemoryStream ms = new MemoryStream(HexToBytes("13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00"));
            BsonReader reader = new BsonReader(ms);
            JsonSerializer serializer = new JsonSerializer();
            serializer.Converters.Add(new RegexConverter());

            RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);

            Assert.Equal("abc", c.Regex.ToString());
            Assert.Equal(RegexOptions.IgnoreCase, c.Regex.Options);
        }
Esempio n. 27
0
        public void WriteStringReadGuid()
        {
            StringTestClass c = new StringTestClass();
            c.AGuid = new Guid("af45dccf-df13-44fe-82be-6212c09eda84").ToString();

            MemoryStream ms = new MemoryStream();
            JsonSerializer serializer = new JsonSerializer();

            BsonWriter writer = new BsonWriter(ms);

            serializer.Serialize(writer, c);

            ms.Seek(0, SeekOrigin.Begin);
            BsonReader reader = new BsonReader(ms);

            GuidTestClass c2 = serializer.Deserialize<GuidTestClass>(reader);

            Assert.Equal(c.AGuid, c2.AGuid.ToString());
        }
        public void ConvertEmptyRegexBson()
        {
            Regex regex = new Regex(string.Empty);

            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);
            JsonSerializer serializer = new JsonSerializer();
            serializer.Converters.Add(new RegexConverter());

            serializer.Serialize(writer, new RegexTestClass { Regex = regex });

            ms.Seek(0, SeekOrigin.Begin);
            BsonReader reader = new BsonReader(ms);
            serializer.Converters.Add(new RegexConverter());

            RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);

            Assert.Equal("", c.Regex.ToString());
            Assert.Equal(RegexOptions.None, c.Regex.Options);
        }
Esempio n. 29
0
        public void DateTimeKindHandling()
        {
            DateTime value = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);

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

            writer.WriteStartObject();
            writer.WritePropertyName("DateTime");
            writer.WriteValue(value);
            writer.WriteEndObject();

            byte[] bson = ms.ToArray();

            JObject o;
            BsonReader reader;

            reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Utc);
            o = (JObject)JToken.ReadFrom(reader);
            Assert.Equal(value, (DateTime)o["DateTime"]);

            reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Local);
            o = (JObject)JToken.ReadFrom(reader);
            Assert.Equal(value.ToLocalTime(), (DateTime)o["DateTime"]);

            reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Unspecified);
            o = (JObject)JToken.ReadFrom(reader);
            Assert.Equal(DateTime.SpecifyKind(value, DateTimeKind.Unspecified), (DateTime)o["DateTime"]);
        }
Esempio n. 30
0
        public void ReadRegex()
        {
            string hexdoc = "15-00-00-00-0B-72-65-67-65-78-00-74-65-73-74-00-67-69-6D-00-00";

            byte[] data = HexToBytes(hexdoc);

            MemoryStream ms = new MemoryStream(data);
            BsonReader reader = new BsonReader(ms);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);
            Assert.Equal("regex", reader.Value);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.String, reader.TokenType);
            Assert.Equal(@"/test/gim", reader.Value);
            Assert.Equal(typeof(string), reader.ValueType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.EndObject, reader.TokenType);

            Assert.False(reader.Read());
            Assert.Equal(JsonToken.None, reader.TokenType);
        }