简体   繁体   中英

How do you serialize value types with MongoDB C# serializer?

The Mongodb C# Driver will not serialize structs/value types. How can this be done?

You can create a custom serializer to handle structs using this code:

public class StructBsonSerializer : IBsonSerializer 
{ 
    public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) 
    { 
        var fields = nominalType.GetFields(BindingFlags.Instance | BindingFlags.Public); 
        var propsAll = nominalType.GetProperties(BindingFlags.Instance | BindingFlags.Public); 

        var props = new List<PropertyInfo>(); 
        foreach (var prop in propsAll) 
        { 
            if (prop.CanWrite) 
            { 
                props.Add(prop); 
            } 
        } 

        bsonWriter.WriteStartDocument(); 

        foreach (var field in fields) 
        { 
            bsonWriter.WriteName(field.Name); 
            BsonSerializer.Serialize(bsonWriter, field.FieldType, field.GetValue(value)); 
        } 
        foreach (var prop in props) 
        { 
            bsonWriter.WriteName(prop.Name); 
            BsonSerializer.Serialize(bsonWriter, prop.PropertyType, prop.GetValue(value, null)); 
        } 

        bsonWriter.WriteEndDocument(); 
    } 

    public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
    { 
        var obj = Activator.CreateInstance(actualType); 

        bsonReader.ReadStartDocument(); 

        while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) 
        { 
            var name = bsonReader.ReadName(); 

            var field = actualType.GetField(name); 
            if (field != null) 
            { 
                var value = BsonSerializer.Deserialize(bsonReader, field.FieldType); 
                field.SetValue(obj, value); 
            } 

            var prop = actualType.GetProperty(name); 
            if (prop != null) 
            { 
                var value = BsonSerializer.Deserialize(bsonReader, prop.PropertyType); 
                prop.SetValue(obj, value, null); 
            } 
        } 

        bsonReader.ReadEndDocument(); 

        return obj; 
    } 

    public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
    { 
        return Deserialize(bsonReader, nominalType, nominalType, options); 
    } 

    public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) 
    { 
        throw new NotImplementedException(); 
    } 

    public void SetDocumentId(object document, object id) 
    { 
        throw new NotImplementedException(); 
    } 
} 

Then, register that serializer for your struct:

BsonSerializer.RegisterSerializer(typeof(MyStruct), new StructBsonSerializer());

Based on above code I manage to adapt it to Mongo Driver version 2.2.3.

public class BasicStructSerializer<T> : StructSerializerBase<T> where T: struct
{
    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value)
    {
        var nominalType = args.NominalType;
        var fields = nominalType.GetFields(BindingFlags.Instance | BindingFlags.Public);
        var propsAll = nominalType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

        var props = new List<PropertyInfo>();
        foreach (var prop in propsAll)
        {
            if (prop.CanWrite)
            {
                props.Add(prop);
            }
        }

        var bsonWriter = context.Writer;

        bsonWriter.WriteStartDocument();

        foreach (var field in fields)
        {
            bsonWriter.WriteName(field.Name);
            BsonSerializer.Serialize(bsonWriter, field.FieldType, field.GetValue(value));
        }
        foreach (var prop in props)
        {
            bsonWriter.WriteName(prop.Name);
            BsonSerializer.Serialize(bsonWriter, prop.PropertyType, prop.GetValue(value, null));
        }

        bsonWriter.WriteEndDocument();
    }

    public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
    {
        //boxing is required for SetValue to work
        var obj = (object)(new T());
        var actualType = args.NominalType;
        var bsonReader = context.Reader;

        bsonReader.ReadStartDocument();

        while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
        {
            var name = bsonReader.ReadName();

            var field = actualType.GetField(name);
            if (field != null)
            {
                var value = BsonSerializer.Deserialize(bsonReader, field.FieldType);
                field.SetValue(obj, value);
            }

            var prop = actualType.GetProperty(name);
            if (prop != null)
            {
                var value = BsonSerializer.Deserialize(bsonReader, prop.PropertyType);
                prop.SetValue(obj, value, null);
            }
        }

        bsonReader.ReadEndDocument();

        return (T)obj;
    }
}

Usage:

cm.GetMemberMap(c => c.SomeMemberName).SetSerializer(new BasicStructSerializer<SomeMemberType>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM