using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Bson.Serialization.Attributes;
public string name { get; set; }
[BsonSerializer(typeof(JsonStringAsObjectSerializer<Dob>))]
public string dob { get; set; }
public string month { get; set; }
public int day { get; set; }
public int year { get; set; }
public class JsonStringAsObjectSerializer<TObject> : SerializerBase<string> where TObject : class
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, string value)
var bsonWriter = context.Writer;
var obj = MyBsonExtensionMethods.FromJson<TObject>(value);
var serializer = BsonSerializer.LookupSerializer(typeof(TObject));
serializer.Serialize(context, obj);
public override string Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
var bsonReader = context.Reader;
var serializer = BsonSerializer.LookupSerializer(typeof(TObject));
var obj = (TObject)serializer.Deserialize(context);
return (obj == null ? null : BsonExtensionMethods.ToJson(obj));
public static partial class MyBsonExtensionMethods
public static TNominalType FromJson<TNominalType>(
JsonReaderSettings readerSettings = null,
IBsonSerializer<TNominalType> serializer = null,
Action<BsonDeserializationContext.Builder> configurator = null)
return (TNominalType)FromJson(json, typeof(TNominalType), readerSettings, serializer, configurator);
public static object FromJson(
JsonReaderSettings readerSettings = null,
IBsonSerializer serializer = null,
Action<BsonDeserializationContext.Builder> configurator = null)
if (nominalType == null || json == null)
throw new ArgumentNullException();
serializer = serializer ?? BsonSerializer.LookupSerializer(nominalType);
if (serializer.ValueType != nominalType)
throw new ArgumentException(string.Format("serializer.ValueType {0} != nominalType {1}.", serializer.GetType().FullName, nominalType.FullName), "serializer");
using (var textReader = new StringReader(json))
using (var reader = new JsonReader(textReader, readerSettings ?? JsonReaderSettings.Defaults))
var context = BsonDeserializationContext.CreateRoot(reader, configurator);
return serializer.Deserialize(context);
public static void Test()
dob = @"{ ""month"" : ""December"", ""day"" : 25, ""year"" : 2019 }",
static void Test(Person person)
var json = BsonExtensionMethods.ToJson(person);
Console.WriteLine("Serialized {0}:", person);
var person2 = MyBsonExtensionMethods.FromJson<Person>(json);
var json2 = BsonExtensionMethods.ToJson(person2);
Assert.IsTrue(person.dob == person2.dob, "person.dob == person2.dob");
Assert.IsTrue(json2 == json, "json2 == json");
var bsonDoc = BsonExtensionMethods.ToBsonDocument(person2);
var person3 = BsonSerializer.Deserialize<Person>(bsonDoc);
Assert.IsTrue(person.dob == person3.dob, "person.dob == person3.dob");
var json3 = BsonExtensionMethods.ToJson(person3);
Assert.IsTrue(json3 == json, "json3 == json");
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("BsonDocument version: " + typeof(BsonDocument).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];