[CSHARP-764] Add a prepackaged convention for serializing enums as strings. Created: 27/Jun/13  Updated: 02/Apr/15  Resolved: 15/Aug/13

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

Type: Improvement Priority: Minor - P4
Reporter: Curt Mayers Assignee: Sridhar Nanjundeswaran
Resolution: Done Votes: 0
Labels: bson, convention, enum, enumerations, serialization
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows


Backwards Compatibility: Fully Compatible

 Description   

We should add a pre-packages convention for serializing all enums as strings.


I need to store all enums in my project as the string representations of their mnemonics (as opposed to their ordinal values). Most of the enum types I am referencing are defined in an external API which I don't have the luxury of modifying or elaborating.

The problem is, different versions of the external libraries which define the enums have different ordinal values for the same mnemonic. This is not unusual, I've seen many API libraries that do something similar.

Rather than have to decorate every enum class, I wanted to add a convention for the serialization of enum values. However, this seems not to be possible, except by creating a separate convention for EVERY enumeration type that may be serialized.

The following code:

pack.Add( new MongoDB.Bson.Serialization.Conventions.MemberSerializationOptionsConvention(typeof(Enum), new RepresentationSerializationOptions(BsonType.String)));

accomplishes precisely zero. Instead, I have to create a new convention for every single specific enumerated type (which in the case of my current project, numbers close to 200).

I would imagine that most developers would want to save their enum types as strings, rather than ordinals. It makes the records more readable, makes queries easier to write and read, and makes documents which are based on external interfaces (often based on XSDs) more robust and likely to survive version changes.

It should be easier to make this the default option in the driver.



 Comments   
Comment by auto [ 15/Aug/13 ]

Author:

{u'username': u'sridharn', u'name': u'Sridhar Nanjundeswaran', u'email': u'sridhar@10gen.com'}

Message: CSHARP-764 - New convention to choose enum serialization representation
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/81a665f10888c214ca2348af0a31417868a18f8c

Comment by Curt Mayers [ 27/Jun/13 ]

Craig –

That works beautifully. Thank you.

I'd like to leave the issue open, though, because I believe that it might be useful to the community to have a pre-defined convention that does this in the MongoDB.Bson.Serialization.Conventions namespace...

Thanks, though: that is a pretty elegant solution.

Comment by Craig Wilson [ 27/Jun/13 ]

I believe the below will do what you want... It's not as concise as you'd like, but it's pretty small. This could also be encapsulated into a reusable class that by implementing IMemberMapConvention. The fundamental problem with your approach above is that typeof(MyEnum) != typeof(Enum).

var pack = new ConventionPack();
pack.AddMemberMapConvention("EnumsAsStrings", m =>
{
    if (m.MemberType.IsEnum)
    {
        m.SetSerializationOptions(new RepresentationSerializationOptions(BsonType.String));
    }
});

Let me know if that is sufficient...

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