Search code examples
c#mongodbserializationbsonmongodb-.net-driver

Serializing Immutable Value types with Mongo C# Driver


I have many immutable value type classes, for example EmailAddress, which ensure any non null instance is valid. I would like to control the serialization of these types of objects to be just the standard string representation ("[email protected]") when persisted using MongoDB C# Driver.

I have tried implementing the IBsonSerilizer however it will only allow for objects or arrays at the root level. I was able to implement proper Json Serilization with Json.NET, is there a different approach I should be taking?


Solution

  • I assume you mean an EmailAddress class something like this:

    [BsonSerializer(typeof(EmailAddressSerializer))]
    public class EmailAddress
    {
        private string _value;
    
        public EmailAddress(string value)
        {
            _value = value;
        }
    
        public string Value
        {
            get { return _value; }
        }
    }
    

    I've used an attribute to link the EmailAddress class to a custom serializer, which could be implemented like this:

    public class EmailAddressSerializer : BsonBaseSerializer
    {
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            if (bsonReader.GetCurrentBsonType() == BsonType.Null)
            {
                bsonReader.ReadNull();
                return null;
            }
            else
            {
                var value = bsonReader.ReadString();
                return new EmailAddress(value);
            }
        }
    
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var emailAddress = (EmailAddress)value;
                bsonWriter.WriteString(emailAddress.Value);
            }
        }
    }
    

    You can't serialize an EmailAddress as the root document (because it's not a document...). But you could use an EmailAddress embedded in some other document. For example:

    public class Person
    {
        public int Id { get; set; }
        public EmailAddress EmailAddress { get; set; }
    }
    

    Which you could test using code like the following:

    var person = new Person { Id = 1, EmailAddress = new EmailAddress("[email protected]") };
    var json = person.ToJson();
    var rehyrdated = BsonSerializer.Deserialize<Person>(json);
    

    The resulting JSON/BSON document is:

    { "_id" : 1, "EmailAddress" : "[email protected]" }