/// <summary> /// Writes the body. /// </summary> /// <param name="writer">The writer.</param> protected override void WriteBody(BsonWriter writer){ writer.WriteValue(BsonType.Integer, 0); writer.Write(FullCollectionName, false); writer.WriteValue(BsonType.Integer, Flags); writer.WriteObject(Selector); writer.WriteObject(Document); }
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); }
protected string Serialize(Document document, BsonWriterSettings settings) { using(var mem = new MemoryStream()) { var writer = new BsonWriter(mem, settings); writer.WriteObject(document); writer.Flush(); return Convert.ToBase64String(mem.ToArray()); } }
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()); }
public void TestNullsDontThrowExceptions() { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); var doc = new Document().Add("n", null); try { writer.WriteObject(doc); } catch(NullReferenceException) { Assert.Fail("Null Reference Exception was thrown on trying to serialize a null value"); } }
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()); }
private static void RunDecodeTest(string name, Document doc) { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); writer.WriteObject(doc); var buff = ms.ToArray(); var lowest = TimeSpan.MaxValue; for(var i = 0; i < trials; i++) { var ret = TimeDecode(buff); if(ret < lowest) lowest = ret; } var opsSec = (int)(perTrial/lowest.TotalSeconds); Console.Out.WriteLine(String.Format("{0}{1} {2}", name + new string('.', 55 - name.Length), opsSec, lowest)); }
private static void DoEncode(Document doc) { var ms = new MemoryStream(); for(var i = 0; i < perTrial; i++) { var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); writer.WriteObject(doc); ms.Seek(0, SeekOrigin.Begin); } }
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 void TestWritingTooLargeDocument() { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); var b = new Binary(new byte[BsonInfo.MaxDocumentSize]); var big = new Document().Add("x", b); writer.WriteObject(big); }
public void TestWriteDocument() { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); const string expected = "1400000002746573740005000000746573740000"; var doc = new Document().Add("test", "test"); writer.WriteObject(doc); var hexdump = BitConverter.ToString(ms.ToArray()); hexdump = hexdump.Replace("-", ""); Assert.AreEqual(expected, hexdump); }
/// <summary> /// Writes the body. /// </summary> /// <param name="writer">The writer.</param> protected override void WriteBody(BsonWriter writer){ writer.WriteValue(BsonType.Integer, (int)Options); writer.Write(FullCollectionName, false); writer.WriteValue(BsonType.Integer, NumberToSkip); writer.WriteValue(BsonType.Integer, NumberToReturn); writer.WriteObject(Query); if(ReturnFieldSelector != null) writer.WriteObject(ReturnFieldSelector); }
static void Test3() { //test litebson Document doc = new Document(); doc.Add("first_name", "test_firstname"); doc.Add("last_name", "test_lastname"); BsonWriterSettings setting = new BsonWriterSettings(); byte[] outputBuffer; using (MemoryStream ms = new MemoryStream()) { BsonWriter writer = new BsonWriter(ms, setting); writer.WriteObject(doc); outputBuffer = ms.ToArray(); ms.Close(); } }
/// <summary> /// Writes out a header and the chunk of documents. /// </summary> /// <param name = "stream"></param> /// <param name = "chunk"></param> protected void WriteChunk(Stream stream, MessageChunk chunk){ WriteHeader(new BinaryWriter(stream), chunk.Size); var writer = new BsonWriter(stream, _bsonWriterSettings); writer.WriteValue(BsonType.Integer, 0); writer.Write(FullCollectionName, false); foreach(var document in chunk.Documents) writer.WriteObject(document); writer.Flush(); }