public static void TestSer() { BsonClassMap.RegisterClassMap <SagaTest.MyState>( x => { x.AutoMap(); x.SetIgnoreExtraElements(true); }); var bd = new MongoDB.Bson.BsonDocument(); MongoDB.Bson.IO.BsonDocumentWriter bdw = (BsonDocumentWriter)BsonDocumentWriter.Create(bd); var obj = new SagaTest.MyState { Collected = new List <int>(), Last = DateTime.Now }; MongoDB.Bson.Serialization.BsonSerializer.Serialize(bdw, obj); bdw.Flush(); /* * bd["_t"] = MongoDB.Bson.Serialization.TypeNameDiscriminator.GetDiscriminator(obj.GetType()); * bd["version"] = "3823424"; * Console.WriteLine(bd.ToJson()); * Type t = TypeNameDiscriminator.GetActualType(bd["_t"].AsString); * var obj2 = BsonSerializer.Deserialize(bd, t); * Console.WriteLine("deser: " + obj2); */ }
public void TestNoCircularReference() { var c2 = new C { X = 2 }; var c1 = new C { X = 1, NestedDocument = c2 }; var json = c1.ToJson(); var expected = "{ 'X' : 1, 'NestedDocument' : { 'X' : 2, 'NestedDocument' : null, 'BsonArray' : { '_csharpnull' : true } }, 'BsonArray' : { '_csharpnull' : true } }".Replace("'", "\""); Assert.AreEqual(expected, json); var memoryStream = new MemoryStream(); using (var writer = new BsonBinaryWriter(memoryStream)) { BsonSerializer.Serialize(writer, c1); Assert.AreEqual(0, writer.SerializationDepth); } var document = new BsonDocument(); using (var writer = new BsonDocumentWriter(document)) { BsonSerializer.Serialize(writer, c1); Assert.AreEqual(0, writer.SerializationDepth); } var stringWriter = new StringWriter(); using (var writer = new JsonWriter(stringWriter)) { BsonSerializer.Serialize(writer, c1); Assert.AreEqual(0, writer.SerializationDepth); } }
/// <summary> /// /// </summary> /// <param name="nominalType"></param> /// <param name="value"></param> /// <param name="options"></param> /// <returns></returns> public static BsonDocument Serialize(Type nominalType, object value, IBsonSerializationOptions options) { var document = new BsonDocument(); var writerSettings = new BsonDocumentWriterSettings(); var writer = new BsonDocumentWriter(document, writerSettings); BsonSerializer.Serialize(writer, nominalType, value, options); return document; }
public BsonDocument Serizalize(IEvent @event) { if (@event == null) throw new ArgumentNullException(nameof(@event)); var document = new BsonDocument(); using (var writer = new BsonDocumentWriter(document)) { BsonSerializer.Serialize(writer, @event); } return document; }
public void TestOneEmptyArray() { var document = new BsonDocument(); using (var writer = new BsonDocumentWriter(document)) { writer.WriteStartDocument(); writer.WriteStartArray("a"); writer.WriteEndArray(); writer.WriteEndDocument(); } var json = document.ToJson(); var expected = "{ '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 TestArrayWithNestedArrayWithTwoElements() { 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.WriteEndArray(); writer.WriteEndDocument(); } var json = document.ToJson(); var expected = "{ 'a' : [['a', 'b']] }".Replace("'", "\""); Assert.Equal(expected, json); }
public void TestTwoNestedEmptyArrays() { var document = new BsonDocument(); using (var writer = new BsonDocumentWriter(document)) { writer.WriteStartDocument(); writer.WriteStartDocument("nested"); writer.WriteStartArray("a"); writer.WriteEndArray(); writer.WriteStartArray("b"); writer.WriteEndArray(); writer.WriteEndDocument(); writer.WriteEndDocument(); } var json = document.ToJson(); var expected = "{ 'nested' : { 'a' : [], 'b' : [] } }".Replace("'", "\""); Assert.AreEqual(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 TestSerializeDeserialize() { var path = "c:/a/b/c/f.txt"; var data = new Data { Path = new ParsedPath(path, PathType.File) }; var doc = new BsonDocument(); using (var writer = new BsonDocumentWriter(doc, new BsonDocumentWriterSettings())) { BsonSerializer.Serialize(writer, data); } Assert.AreEqual(doc["Path"], new BsonString(path)); using (var reader = new BsonDocumentReader(doc, new BsonDocumentReaderSettings())) { data = BsonSerializer.Deserialize<Data>(doc); } Assert.AreEqual(data.Path, new ParsedPath(path, PathType.File)); }
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 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); }
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 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 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 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 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 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); }
/// <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 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 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 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 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 TestOneNestedNull() { var document = new BsonDocument(); using (var writer = new BsonDocumentWriter(document)) { writer.WriteStartDocument(); writer.WriteStartDocument("nested"); writer.WriteNull("a"); writer.WriteEndDocument(); writer.WriteEndDocument(); } var json = document.ToJson(); var expected = "{ 'nested' : { 'a' : null } }".Replace("'", "\""); 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 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 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 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 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 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 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); }