WriteStartDocument() public method

Writes the start of a BSON document to the writer.
public WriteStartDocument ( ) : void
return void
 public void TestOneNestedEmptyArray()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartDocument("nested");
         writer.WriteStartArray("a");
         writer.WriteEndArray();
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'nested' : { 'a' : [] } }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
        public static BsonValue SerializeValue(this ISerializationExpression field, object value)
        {
            Ensure.IsNotNull(field, nameof(field));

            var tempDocument = new BsonDocument();
            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("value");
                field.Serializer.Serialize(context, value);
                bsonWriter.WriteEndDocument();
                return tempDocument[0];
            }
        }
 public void TestTwoEmptyArrays()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartArray("a");
         writer.WriteEndArray();
         writer.WriteStartArray("b");
         writer.WriteEndArray();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : [], 'b' : [] }".Replace("'", "\"");
     Assert.AreEqual(expected, json);
 }
 public void TestArrayWithNestedArrayWithOneElement()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartArray("a");
         writer.WriteStartArray();
         writer.WriteString("a");
         writer.WriteEndArray();
         writer.WriteEndArray();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : [['a']] }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
        public static BsonArray SerializeValues(this ISerializationExpression field, IEnumerable values)
        {
            var tempDocument = new BsonDocument();
            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("values");
                bsonWriter.WriteStartArray();
                foreach (var value in values)
                {
                    field.Serializer.Serialize(context, value);
                }
                bsonWriter.WriteEndArray();
                bsonWriter.WriteEndDocument();

                return (BsonArray)tempDocument[0];
            }
        }
 public void TestOneTimestamp()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteTimestamp("a", 1);
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : Timestamp(0, 1) }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestOneUndefined()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteUndefined("a");
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : undefined }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestTwoNestedMinKeys()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartDocument("nested");
         writer.WriteMinKey("a");
         writer.WriteMinKey("b");
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'nested' : { 'a' : { '$minkey' : 1 }, 'b' : { '$minkey' : 1 } } }".Replace("'", "\"");
     Assert.AreEqual(expected, json);
 }
 public void TestArrayWithTwoElements()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartArray("a");
         writer.WriteInt32(1);
         writer.WriteInt32(2);
         writer.WriteEndArray();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : [1, 2] }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
 public void TestTwoInt64s()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteInt64("a", 1);
         writer.WriteInt64("b", 2);
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : NumberLong(1), 'b' : NumberLong(2) }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestTwoJavaScriptWithScopes()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteJavaScriptWithScope("a", "x");
         writer.WriteStartDocument();
         writer.WriteInt32("x", 1);
         writer.WriteEndDocument();
         writer.WriteJavaScriptWithScope("b", "y");
         writer.WriteStartDocument();
         writer.WriteInt32("y", 2);
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : { '$code' : 'x', '$scope' : { 'x' : 1 } }, 'b' : { '$code' : 'y', '$scope' : { 'y' : 2 } } }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="storeData"></param>
 /// <returns></returns>
 private static BsonDocument ConvertStoreDataToBsonDocument(SessionStateStoreData storeData)
 {
     var items = storeData.Items;
     var document = new BsonDocument();
     var documentWriterSettings = new BsonDocumentWriterSettings();
     var documentWriter = new BsonDocumentWriter(document, documentWriterSettings);
     documentWriter.WriteStartDocument();
     foreach (string key in items.Keys) {
         var value = items[key];
         documentWriter.WriteName(key);
         BsonSerializer.Serialize(documentWriter, value.GetType(), value);
     }
     documentWriter.WriteEndDocument();
     return document;
 }
 public void TestTwoDateTimes()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteDateTime("a", 0);
         writer.WriteDateTime("b", 0);
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : ISODate('1970-01-01T00:00:00Z'), 'b' : ISODate('1970-01-01T00:00:00Z') }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestTwoNestedTimestamps()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartDocument("nested");
         writer.WriteTimestamp("a", 1);
         writer.WriteTimestamp("b", 2);
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'nested' : { 'a' : Timestamp(0, 1), 'b' : Timestamp(0, 2) } }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
 public void TestEmptyDocument()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ }";
     Assert.Equal(expected, json);
 }
 public void TestTwoNestedStrings()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartDocument("nested");
         writer.WriteString("a", "x");
         writer.WriteString("b", "y");
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'nested' : { 'a' : 'x', 'b' : 'y' } }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
 public void TestTwoNestedRegularExpressions()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartDocument("nested");
         writer.WriteRegularExpression("a", new BsonRegularExpression("p", "i"));
         writer.WriteRegularExpression("b", new BsonRegularExpression("q", "m"));
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'nested' : { 'a' : /p/i, 'b' : /q/m } }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
 public void TestArrayWithTwoNestedArrays()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartArray("a");
         writer.WriteStartArray();
         writer.WriteString("a");
         writer.WriteString("b");
         writer.WriteEndArray();
         writer.WriteStartArray();
         writer.WriteString("c");
         writer.WriteStartDocument();
         writer.WriteInt32("d", 9);
         writer.WriteEndDocument();
         writer.WriteEndArray();
         writer.WriteEndArray();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : [['a', 'b'], ['c', { 'd' : 9 }]] }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
 public void TestTwoBinaries()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteBytes("a", new byte[] { 1 });
         writer.WriteBytes("b", new byte[] { 2 });
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : new BinData(0, 'AQ=='), 'b' : new BinData(0, 'Ag==') }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestOneNestedBinary()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartDocument("nested");
         writer.WriteBytes("a", new byte[] { 1, 2 });
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'nested' : { 'a' : new BinData(0, 'AQI=') } }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
 public void TestTwoBooleans()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteBoolean("a", true);
         writer.WriteBoolean("b", false);
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : true, 'b' : false }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestOneNestedInt64()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartDocument("nested");
         writer.WriteInt64("a", 1);
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'nested' : { 'a' : NumberLong(1) } }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }
 public void TestTwoDoubles()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteDouble("a", 1.5);
         writer.WriteDouble("b", 2.5);
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : 1.5, 'b' : 2.5 }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestOneMaxKey()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteMaxKey("a");
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : { '$maxkey' : 1 } }".Replace("'", "\""); ;
     Assert.AreEqual(expected, json);
 }
 public void TestTwoJavaScripts()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteJavaScript("a", "x");
         writer.WriteJavaScript("b", "y");
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : { '$code' : 'x' }, 'b' : { '$code' : 'y' } }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestOneRegularExpression()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteRegularExpression("a", new BsonRegularExpression("p", "i"));
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : /p/i }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestTwoMinKeys()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteMinKey("a");
         writer.WriteMinKey("b");
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : MinKey, 'b' : MinKey }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestOneSymbol()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteSymbol("a", "x");
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : { '$symbol' : 'x' } }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestOneObjectId()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteObjectId("a", ObjectId.Empty);
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'a' : ObjectId('000000000000000000000000') }".Replace("'", "\""); ;
     Assert.Equal(expected, json);
 }
 public void TestTwoNestedDecimal128s()
 {
     var document = new BsonDocument();
     using (var writer = new BsonDocumentWriter(document))
     {
         writer.WriteStartDocument();
         writer.WriteStartDocument("nested");
         writer.WriteDecimal128("a", (Decimal128)1.5M);
         writer.WriteDecimal128("b", (Decimal128)2.5M);
         writer.WriteEndDocument();
         writer.WriteEndDocument();
     }
     var json = document.ToJson();
     var expected = "{ 'nested' : { 'a' : NumberDecimal('1.5'), 'b' : NumberDecimal('2.5') } }".Replace("'", "\"");
     Assert.Equal(expected, json);
 }