Пример #1
0
        private string WriteAndReadString(string val){
            var buf = Encoding.UTF8.GetBytes(val + '\0');

            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());
            return reader.ReadString();
        }
Пример #2
0
        public void TestReadBigDocument()
        {
            MemoryStream ms = new MemoryStream();
            var writer = new BsonWriter(ms, new BsonDocumentDescriptor());

            Document expected = new Document();
            expected.Append("str", "test")
                .Append("int", 45)
                .Append("long", (long)46)
                .Append("num", 4.5)
                .Append("date",DateTime.Today)
                .Append("_id", new OidGenerator().Generate())
                .Append("code", new Code("return 1;"))
                .Append("subdoc", new Document().Append("a",1).Append("b",2))
                .Append("array", new String[]{"a","b","c","d"})
                .Append("codewscope", new CodeWScope("return 2;", new Document().Append("c",1)))
                .Append("binary", new Binary(new byte[]{0,1,2,3}))
                .Append("regex", new MongoRegex("[A-Z]"))
                .Append("minkey", MongoMinKey.Value)
                .Append("maxkey", MongoMaxKey.Value)
                .Append("symbol", new MongoSymbol("symbol"))
            ;
            writer.WriteObject(expected);
            writer.Flush();
            ms.Seek(0,SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms, new BsonDocumentBuilder());
            Document doc = reader.Read();

            Assert.IsNotNull(doc);
        }
Пример #3
0
 protected Document Deserialize(string base64, BsonReaderSettings settings)
 {
     using(var mem = new MemoryStream(Convert.FromBase64String(base64)))
     {
         var reader = new BsonReader(mem, settings);
         return (Document)reader.ReadObject();
     }
 }
Пример #4
0
        public void TestReadEmptyDocument(){
            var buf = HexToBytes("0500000000");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var doc = (Document)reader.ReadObject();

            Assert.IsNotNull(doc);
        }
Пример #5
0
 private string WriteAndReadLenString(string val){
     var ms = new MemoryStream();
     var bs = new BsonWriter(ms, new BsonDocumentDescriptor());
     var w = new BinaryWriter(ms);
     var byteCount = bs.CalculateSize(val, false);
     w.Write(byteCount);
     bs.Write(val, false);
     ms.Seek(0, SeekOrigin.Begin);
     var reader = new BsonReader(ms, new BsonDocumentBuilder());
     return reader.ReadLengthString();
 }
Пример #6
0
        public void TestReadDocWithDocs(){
            //            Document doc = new Document().Append("a", new Document().Append("b", new Document().Append("c",new Document())));
            //            Console.WriteLine(ConvertDocToHex(doc));
            var buf = HexToBytes("1D000000036100150000000362000D0000000363000500000000000000");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var doc = (Document)reader.ReadObject();
            Assert.IsNotNull(doc, "Document was null");
            Assert.AreEqual(buf.Length, reader.Position);
            Assert.IsTrue(doc.ContainsKey("a"));
        }
Пример #7
0
        public void TestRoundTrip(){
            var idoc = new Document{{"b", new Binary(new[]{(byte)1, (byte)2})}};

            var stream = new MemoryStream();
            var writer = new BsonWriter(stream, new BsonDocumentDescriptor());
            writer.WriteObject(idoc);

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new BsonReader(stream,new BsonDocumentBuilder());
            var odoc = reader.Read();

            Assert.AreEqual(idoc.ToString(), odoc.ToString());
        }
Пример #8
0
        public void TestBinaryRead(){
            const string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400";

            var data = DecodeHex(hex);
            var inmem = new MemoryStream(data);
            var inreader = new BsonReader(inmem,new BsonDocumentBuilder());
            var indoc = inreader.Read();

            var outmem = new MemoryStream();
            var outwriter = new BsonWriter(outmem, new BsonDocumentDescriptor());
            outwriter.WriteObject(indoc);
            var outdata = outmem.ToArray();
            var outhex = BitConverter.ToString(outdata);
            outhex = outhex.Replace("-", "");

            Assert.AreEqual(hex, outhex.ToLower());
        }
Пример #9
0
 private static void DoDecode(byte[] buff)
 {
     var ms = new MemoryStream(buff);
     for(var i = 0; i < perTrial; i++)
     {
         var reader = new BsonReader(ms, new BsonDocumentBuilder());
         reader.Read();
         ms.Seek(0, SeekOrigin.Begin);
     }
 }
Пример #10
0
        /// <summary>
        /// Reads a BsonDocument from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <returns>A BsonDocument.</returns>
        public static new BsonDocument ReadFrom(BsonReader bsonReader)
        {
            BsonDocument document = new BsonDocument();

            return((BsonDocument)document.Deserialize(bsonReader, typeof(BsonDocument), null));
        }
Пример #11
0
        public void TestReadString()
        {
            var buf = HexToBytes("7465737400");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var s = reader.ReadString();
            Assert.AreEqual("test", s);
            Assert.AreEqual(4, Encoding.UTF8.GetByteCount(s));
        }
Пример #12
0
        public void TestReadSimpleDocument()
        {
            var buf = HexToBytes("1400000002746573740005000000746573740000");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var doc = reader.Read();

            Assert.IsNotNull(doc, "Document was null");
            Assert.IsTrue(doc.ContainsKey("test"));
            Assert.AreEqual("test", doc["test"]);
        }
Пример #13
0
        public void TestReadMultiElementDocument()
        {
            var buf = HexToBytes("2D000000075F6964004A753AD8FAC16EA58B290351016100000000000000F03F02620005000000746573740000");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var doc = (Document)reader.ReadObject();

            Assert.IsNotNull(doc, "Document was null");
            Assert.IsTrue(doc.ContainsKey("_id"));
            Assert.IsTrue(doc.ContainsKey("a"));
            Assert.IsTrue(doc.ContainsKey("b"));
            Assert.AreEqual("4a753ad8fac16ea58b290351", (doc["_id"]).ToString());
            Assert.AreEqual(1, Convert.ToInt32(doc["a"]));
            Assert.AreEqual("test", doc["b"]);
        }
        protected Document WriteAndRead(Document source){
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms, new BsonDocumentDescriptor());

            writer.WriteObject(source);
            writer.Flush();
            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms,new BsonDocumentBuilder());
            return reader.Read();
        }   
 public static new BsonDocument ReadFrom(BsonReader bsonReader)
 {
     return(BsonSerializer.Deserialize <BsonDocument>(bsonReader));
 }
Пример #16
0
 public static new BsonArray ReadFrom(BsonReader bsonReader)
 {
     return(BsonSerializer.Deserialize <BsonArray>(bsonReader));
 }
 /// <summary>
 /// Deserialize is an invalid operation for BsonDocumentWrapper.
 /// </summary>
 /// <param name="bsonReader">Not applicable.</param>
 /// <param name="nominalType">Not applicable.</param>
 /// <param name="options">Not applicable.</param>
 /// <returns>Not applicable.</returns>
 public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
 {
     throw new NotSupportedException();
 }