Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-764

Add a prepackaged convention for serializing enums as strings.

    • Fully Compatible

      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.

            Assignee:
            sridhar Sridhar Nanjundeswaran
            Reporter:
            curt Curt Mayers
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: