89

Is there a way to store Enums as string names rather than ordinal values?

Example:

Imagine I've got this enum:

public enum Gender
{
    Female,
    Male
}

Now if some imaginary User exists with

...
Gender gender = Gender.Male;
...

it'll be stored in MongoDb database as { ... "Gender" : 1 ... }

but i'd prefer something like this { ... "Gender" : "Male" ... }

Is this possible? Custom mapping, reflection tricks, whatever.

My context: I use strongly typed collections over POCO (well, I mark ARs and use polymorphism occasionally). I've got a thin data access abstraction layer in a form of Unit Of Work. So I'm not serializing/deserializing each object but I can (and do) define some ClassMaps. I use official MongoDb driver + fluent-mongodb.

Kostassoid
  • 1,870
  • 2
  • 20
  • 29
  • 9
    I'd avoid it. The string value takes up way more space than an integer. I would however, if persistence is involved give deterministic values to each item in your enum so Female = 1, Male = 2 so if the enum is added to later or the order of items changed that you don't end up with problems. – Deleted Aug 09 '11 at 13:28
  • 1
    Yes, I was thinking about space but considered it's not a problem since I've got only a few cases where I prefer honest Enums. And you've got it right, future changes is what bothers me. I guess marking every enum values is a viable solution indeed. Thanks Chris! – Kostassoid Aug 09 '11 at 13:37

9 Answers9

157
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public class Person
{
    [JsonConverter(typeof(StringEnumConverter))]  // JSON.Net
    [BsonRepresentation(BsonType.String)]         // Mongo
    public Gender Gender { get; set; }
}
Luca Ziegler
  • 3,236
  • 1
  • 22
  • 39
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
  • 3
    this is the simplest implementation without any overhead. – Deni Spasovski Feb 05 '14 at 15:51
  • after update mongo driver version 2.0 not working :( !! – Bimawa Apr 21 '15 at 06:58
  • 4
    I'm using the latest driver and it works. Should be the accepted answer. – Henrique Miranda Apr 10 '17 at 17:14
  • 5
    The accepted answer is the better way to go. In general you want your model to be persistence-ignorant. It's arguably no longer a POCO class if it has a MongoDB dependence. Anything that references the model has to reference MongoDB.Bson even if it's not involved in reading/writing. The rules of how to read/write are better contained in the repository layer. – Chad Hedgcock Feb 21 '19 at 16:23
  • Agree with @ChadHedgcock on this. I put the code from the accepted answer into the static constructor of my Mongo context class. – Neo Aug 10 '19 at 18:29
  • Its an old answer but this helped me so i am great-full – Milos Jan 09 '20 at 17:42
  • 1
    I tend to agree with @ChadHedgcock that it is better to register these types of dependencies at some app- or container-level. However, this answer shows how to override the default / conventional behavior, so I think it's still quite useful. – John Gietzen Jul 05 '20 at 17:30
63

The MongoDB .NET Driver lets you apply conventions to determine how certain mappings between CLR types and database elements are handled.

If you want this to apply to all your enums, you only have to set up conventions once per AppDomain (usually when starting your application), as opposed to adding attributes to all your types or manually map every type:

// Set up MongoDB conventions
var pack = new ConventionPack
{
    new EnumRepresentationConvention(BsonType.String)
};

ConventionRegistry.Register("EnumStringConvention", pack, t => true);
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Ricardo Rodriguez
  • 1,010
  • 11
  • 22
  • 8
    Documentation on this is less than wonderful. I'd go with the [BsonRepresentation(BsonType.String)] approach on the properties you want to serialize because it's *very* obvious (as suggested by John Gietzen below, which I'm guessing he got from http://www.drdobbs.com/database/mongodb-with-c-deep-dive/240152181?pgno=2 given the example he's used). – Bart Read Nov 10 '14 at 15:41
  • 5
    I have found that this is not working for all serialization cases where an enum is used. If in your data structure your enums are boxed in an object type, as it would if you use ExpandoObjects or put values into Dictionary to be serialized, the enum values will be serialized as int values even if the convention is there. It seems like the mongodb serialization will work correctly for enum values if the nominal type to serialize is the enum type. But if the nomil type is typeof(Object), serialization to a string is not working. – sboisse Apr 01 '18 at 12:13
16

You can customize the class map for the class that contains the enum and specify that the member be represented by a string. This will handle both the serialization and deserialization of the enum.

if (!MongoDB.Bson.Serialization.BsonClassMap.IsClassMapRegistered(typeof(Person)))
      {
        MongoDB.Bson.Serialization.BsonClassMap.RegisterClassMap<Person>(cm =>
         {
           cm.AutoMap();
           cm.GetMemberMap(c => c.Gender).SetRepresentation(BsonType.String);

         });
      }

I am still looking for a way to specify that enums be globally represented as strings, but this is the method that I am currently using.

Wade Kaple
  • 321
  • 3
  • 4
  • thanks! that's what i kind of looked for. i can see a certain problems, like more elaborate query model for implementing cqrs, but nothing unmanageable. – Kostassoid Feb 03 '12 at 14:10
  • 5
    for the Mongo 2.0 driver, this syntax works: BsonSerializer.RegisterSerializer(new EnumSerializer(BsonType.String)); – BrokeMyLegBiking Aug 26 '16 at 05:49
9

I have found that just applying Ricardo Rodriguez' answer is not sufficient in some cases to properly serialize enum values to string into MongoDb:

// Set up MongoDB conventions
var pack = new ConventionPack
{
    new EnumRepresentationConvention(BsonType.String)
};

ConventionRegistry.Register("EnumStringConvention", pack, t => true);

If your data structure involves enum values being boxed into objects, the MongoDb serialization will not use the set EnumRepresentationConvention to serialize it.

Indeed, if you look at the implementation of MongoDb driver's ObjectSerializer, it will resolve the TypeCode of the boxed value (Int32 for enum values), and use that type to store your enum value in the database. So boxed enum values end up being serialized as int values. They will remain as int values when being deserialized as well.

To change this, it's possible to write a custom ObjectSerializer that will enforce the set EnumRepresentationConvention if the boxed value is an enum. Something like this:

public class ObjectSerializer : MongoDB.Bson.Serialization.Serializers.ObjectSerializer
{
     public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
    {
        var bsonWriter = context.Writer;
        if (value != null && value.GetType().IsEnum)
        {
            var conventions = ConventionRegistry.Lookup(value.GetType());
            var enumRepresentationConvention = (EnumRepresentationConvention) conventions.Conventions.FirstOrDefault(convention => convention is EnumRepresentationConvention);
            if (enumRepresentationConvention != null)
            {
                switch (enumRepresentationConvention.Representation)
                {
                    case BsonType.String:
                        value = value.ToString();
                        bsonWriter.WriteString(value.ToString());
                        return;
                }
            }
        }

        base.Serialize(context, args, value);
    }
}

and then set the custom serializer as the one to use for serializing objects:

BsonSerializer.RegisterSerializer(typeof(object), new ObjectSerializer());

Doing this will ensure boxed enum values will be stored as strings just like the unboxed ones.

Keep in mind however that when deserializing your document, the boxed value will remain a string. It will not be converted back to the original enum value. If you need to convert the string back to the original enum value, a discrimination field will likely have to be added in your document so the serializer can know what is the enum type to desrialize into.

One way to do it would be to store a bson document instead of just a string, into which the discrimination field (_t) and a value field (_v) would be used to store the enum type and its string value.

sboisse
  • 4,860
  • 3
  • 37
  • 48
7

With driver 2.x I solved using a specific serializer:

BsonClassMap.RegisterClassMap<Person>(cm =>
            {
                cm.AutoMap();
                cm.MapMember(c => c.Gender).SetSerializer(new EnumSerializer<Gender>(BsonType.String));
            });
wilver
  • 2,106
  • 1
  • 19
  • 26
5

Use MemberSerializationOptionsConvention to define a convention on how an enum will be saved.

new MemberSerializationOptionsConvention(typeof(Gender), new RepresentationSerializationOptions(BsonType.String))
boypula
  • 81
  • 1
  • 4
  • I used this solution in the past, but now seems that in 2.x version of the drivers the class MemberSerializationOptionsConvention disappeared. Anyone knows how to obtain the same result with 2.x driver version? – Alkampfer Apr 12 '16 at 07:31
4

If you are using .NET Core 3.1 and above, use the latest ultra-fast Json Serializer/Deserializer from Microsoft, System.Text.Json (https://www.nuget.org/packages/System.Text.Json).

See the metrics comparison at https://medium.com/@samichkhachkhi/system-text-json-vs-newtonsoft-json-d01935068143

using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System.Text.Json.Serialization;;

public class Person
{
    [JsonConverter(typeof(JsonStringEnumConverter))]  // System.Text.Json.Serialization
    [BsonRepresentation(BsonType.String)]         // MongoDB.Bson.Serialization.Attributes
    public Gender Gender { get; set; }
}
Chipo Hamayobe
  • 877
  • 11
  • 13
2

The answers posted here work well for TEnum and TEnum[], however won't work with Dictionary<TEnum, object>. You could achieve this when initializing serializer using code, however I wanted to do this through attributes. I've created a flexible DictionarySerializer that can be configured with a serializer for the key and value.

public class DictionarySerializer<TDictionary, KeySerializer, ValueSerializer> : DictionarySerializerBase<TDictionary>
    where TDictionary : class, IDictionary, new()
    where KeySerializer : IBsonSerializer, new()
    where ValueSerializer : IBsonSerializer, new()
{
    public DictionarySerializer() : base(DictionaryRepresentation.Document, new KeySerializer(), new ValueSerializer())
    {
    }

    protected override TDictionary CreateInstance()
    {
        return new TDictionary();
    }
}

public class EnumStringSerializer<TEnum> : EnumSerializer<TEnum>
    where TEnum : struct
{
    public EnumStringSerializer() : base(BsonType.String) { }
}

Usage like this, where both key and value are enum types, but could be any combination of serializers:

    [BsonSerializer(typeof(DictionarySerializer<
        Dictionary<FeatureToggleTypeEnum, LicenseFeatureStateEnum>, 
        EnumStringSerializer<FeatureToggleTypeEnum>,
        EnumStringSerializer<LicenseFeatureStateEnum>>))]
    public Dictionary<FeatureToggleTypeEnum, LicenseFeatureStateEnum> FeatureSettings { get; set; }
Bouke
  • 11,768
  • 7
  • 68
  • 102
0

.NET 7.0

Improving on @sboisse very good answer, I've found a way that satisfies all of my usecases.

Generic Enum Serializer

// for boxed enums
BsonSerializer.RegisterSerializer(typeof(object), new BoxedEnumStringSerializer());
// for specifix unboxed enum
BsonSerializer.RegisterSerializer(typeof(MyEnum), new EnumStringSerializer<MyEnum>());
// serializer class
public class BoxedEnumStringSerializer : ObjectSerializer
{
    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
    {
        var bsonWriter = context.Writer;
        string? serialized = null;

        if (value.GetType().IsEnum && value.ToString() is string valStr)
        {
            var conventions = ConventionRegistry.Lookup(value.GetType());
            var enumRpz = conventions.Conventions.FirstOrDefault(convention => convention is EnumRepresentationConvention) as EnumRepresentationConvention;

            switch (enumRpz?.Representation)
            {
                case BsonType.String:
                    serialized = valStr;
                    break;
            }
        }

        if (serialized != null)
            base.Serialize(context, args, serialized);
        else
            base.Serialize(context, args, value);
    }

    public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
    {
        var val = context.Reader.ReadString();

        return Enum.Parse(args.NominalType, val);
    }
}

public class EnumStringSerializer<T> : BoxedEnumStringSerializer, IBsonSerializer<T> where T : struct, Enum
{
    public new Type ValueType => typeof(T);

    public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value)
    {
        base.Serialize(context, args, value);
    }

    T IBsonSerializer<T>.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
    {
        return (T)base.Deserialize(context, args);
    }
}

Registering every enum in assembly

var enums = Assembly.GetExecutingAssembly()
                            .GetTypes()
                            .Where(t => t.IsEnum && t.IsPublic);
foreach (var e in enums)
{
    var serializer = typeof(EnumStringSerializer<>).MakeGenericType(e);
    BsonSerializer.RegisterSerializer(e, Activator.CreateInstance(serializer) as IBsonSerializer);
}

Notes:

  • If you want to auto register all enums in assembly you can consider creating your own enum attribute in order to mark them rather than blindly taking them all
  • This is a good template to use your custom enum serialization case, I use snake case so convention pack cannot work
Alexandre Daubricourt
  • 3,323
  • 1
  • 34
  • 33