Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 2.4
    • Fix Version/s: undecided
    • Component/s: BSON
    • Labels:
      None
    • # Replies:
      12
    • Last comment by Customer:
      false
    • Story Points:
      1
    • Sprint:
      JVM Sprint 40

      Description

      Add support for encoding/decoding java enums

        Issue Links

          Activity

          Hide
          jeff.yemin Jeff Yemin added a comment - - edited

          Hi Mathieu,

          I was just reviewing this ticket, and after re-reading your comment I realized that the driver can already do what you asked for via org.bson.BSON.addEncodingHook.

          Just create a class that implements org.bson.Transformer, e.g.

          class EnumTransformer implements Transformer {
                  public Object transform(Object o) {
                      return o.toString();  // convert any object (including an Enum) to its String representation
                  }
              }

          and register an instance as an encoding hook for all classes that extend Enum:

                  BSON.addEncodingHook(Enum.class, new EnumTransformer());

          If you need to, you can specialize the Transformer for particular Enum types, e.g.

                  BSON.addEncodingHook(MyEnum.class, new MyEnumTransformer());

          Show
          jeff.yemin Jeff Yemin added a comment - - edited Hi Mathieu, I was just reviewing this ticket, and after re-reading your comment I realized that the driver can already do what you asked for via org.bson.BSON.addEncodingHook. Just create a class that implements org.bson.Transformer, e.g. class EnumTransformer implements Transformer { public Object transform(Object o) { return o.toString(); // convert any object (including an Enum) to its String representation } } and register an instance as an encoding hook for all classes that extend Enum: BSON.addEncodingHook(Enum. class , new EnumTransformer()); If you need to, you can specialize the Transformer for particular Enum types, e.g. BSON.addEncodingHook(MyEnum. class , new MyEnumTransformer());
          Hide
          mimkorn Šimon Demočko added a comment -

          Hi Jeff,

          this technique is probably not appliable for when you want to use the enum as a key, not as a value. The encodingHook only cares about the values, keys are still only expected as Strings. Found in BasicBSONEncoder#_putObjectField(String name, Object val) - I would want the name to be optionally Enum so that it would just get the String representation of it, if necessary.

          Show
          mimkorn Šimon Demočko added a comment - Hi Jeff, this technique is probably not appliable for when you want to use the enum as a key, not as a value. The encodingHook only cares about the values, keys are still only expected as Strings. Found in BasicBSONEncoder#_putObjectField(String name, Object val) - I would want the name to be optionally Enum so that it would just get the String representation of it, if necessary.
          Hide
          ross.lawley Ross Lawley added a comment -
          Show
          ross.lawley Ross Lawley added a comment - PR: https://github.com/rozza/mongo-java-driver/pull/200
          Hide
          xgen-internal-githook Githook User added a comment -

          Author:

          {u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

          Message: Added Enum support for POJOs

          JAVA-268
          Branch: master
          https://github.com/mongodb/mongo-java-driver/commit/befbea5c0a695e35e8789bd9ddbc4dabf2832176

          Show
          xgen-internal-githook Githook User added a comment - Author: {u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'} Message: Added Enum support for POJOs JAVA-268 Branch: master https://github.com/mongodb/mongo-java-driver/commit/befbea5c0a695e35e8789bd9ddbc4dabf2832176
          Hide
          ross.lawley Ross Lawley added a comment -

          In 3.5 ENUMs in POJOs will be supported automatically.

          There are no current plans to support ENUMs in documents as the value wouldn't round trip. To make them round trip in documents, extra meta data would be required as well as a custom Codec / transformer.

          Show
          ross.lawley Ross Lawley added a comment - In 3.5 ENUMs in POJOs will be supported automatically. There are no current plans to support ENUMs in documents as the value wouldn't round trip. To make them round trip in documents, extra meta data would be required as well as a custom Codec / transformer.

            People

            • Votes:
              10 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since reply:
                2 weeks, 4 days ago
                Date of 1st Reply:

                  Agile