[CSHARP-2530] MongoDB C# driver enum mapping Created: 25/Feb/19  Updated: 19/Jun/19  Resolved: 07/May/19

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Jakub Dropia Assignee: Wan Bachtiar
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I have Enum:

{{}}

public enum SomeType
{
   TypeA,
   TypeB, 
   TypeC
}

{{}}

but in MongoDB i would like this map to: type_a type_b type_c

I'm using EnumRepresentationConvention(BsonType.String)

I tried:

{{}}

public enum SomeType
{
   [BsonElement("type_a")]
   TypeA,
   [BsonElement("type_b")]
   TypeB, 
   [BsonElement("type_c")]
   TypeC
}

{{}}

but this doesn't work. Im getting exception:

Requested value 'type_a' was not found.

Is anyone know how can achieve such mapping in MongoDb C# driver?



 Comments   
Comment by Wan Bachtiar [ 14/Jun/19 ]

Hi Jakub,

This is out of the design scope for the driver. You could try using reflection with a generic custom serialiser.

Regards,
Wan.

Comment by Jakub Dropia [ 24/May/19 ]

Hi, thanks for reply!
Is there any way to create some kind "convention" for all enums? 

Comment by Wan Bachtiar [ 19/Mar/19 ]

Is anyone know how can achieve such mapping in MongoDb C# driver?

Hi Jakub,
You need to write and register a custom deserializer/serializer for SomeType. For example, given the following enum :

        
        public enum SomeType 
        {
            TypeA, 
            TypeB, 
            TypeC        
        }

You can write a custom class deserialiser/serializer as below:

   
 public class EnumAsStringSerializationProvider : BsonSerializationProviderBase
    {
        public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
        {
            if (!type.IsEnum) return null;
            if (type == typeof(Program.SomeType))
            {
                return new MyEnumSerializer();
            }
            return null;
        }
    }
 
    class MyEnumSerializer : SerializerBase<Program.SomeType> 
    {
            public override Program.SomeType Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
            {
                switch(context.Reader.ReadString())
                {
                    case "type_a":
                        return Program.SomeType.TypeA;
                    case "type_b":
                        return Program.SomeType.TypeB;                    
                    case "type_c":
                        return Program.SomeType.TypeC;     
                    default:
                        throw new FormatException("Unrecognised SomeType to be deserialized");          
                }
            }
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Program.SomeType value)
        {
            switch(value)
            {
                case Program.SomeType.TypeA:
                    context.Writer.WriteString("type_a");
                    return;                    
                case Program.SomeType.TypeB:
                    context.Writer.WriteString("type_b");
                    return;
                case Program.SomeType.TypeC:
                    context.Writer.WriteString("type_c");
                    return;
                default: 
                    throw new ArgumentException("Unrecognized SomeType to be serialized");
            }
        }
    }

Then you need to register it:

    
var enumAsStringSerializationProvider = new EnumAsStringSerializationProvider();
BsonSerializer.RegisterSerializationProvider(enumAsStringSerializationProvider);

Please note that the CSHARP project is for reporting bugs or feature suggestions for the MongoDB .NET/C# driver. If you have any follow-up questions on the use of the driver, please post a question on mongodb-user group with relevant the information.

Regards,
Wan.

Comment by Jakub Dropia [ 18/Mar/19 ]

Any thoughts? 

Generated at Wed Feb 07 21:42:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.