[JAVA-268] Add support for Java enums Created: 08/Feb/11 Updated: 05/Dec/17 Resolved: 06/Jun/17 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | BSON |
| Affects Version/s: | 2.4 |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Javier Ferrero | Assignee: | Ross Lawley |
| Resolution: | Done | Votes: | 10 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Description |
|
Add support for encoding/decoding java enums |
| Comments |
| Comment by Ross Lawley [ 07/Jun/17 ] | |||||||||||||||||||||||
|
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. | |||||||||||||||||||||||
| Comment by Githook User [ 06/Jun/17 ] | |||||||||||||||||||||||
|
Author: {u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}Message: Added Enum support for POJOs
| |||||||||||||||||||||||
| Comment by Ross Lawley [ 26/May/17 ] | |||||||||||||||||||||||
| Comment by Šimon Demo?ko [ 28/Jul/15 ] | |||||||||||||||||||||||
|
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. | |||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 15/Apr/14 ] | |||||||||||||||||||||||
|
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.
and register an instance as an encoding hook for all classes that extend Enum:
If you need to, you can specialize the Transformer for particular Enum types, e.g.
| |||||||||||||||||||||||
| Comment by Trisha Gee [ 23/May/13 ] | |||||||||||||||||||||||
|
That's a valid concern, but the 3.0 driver will have a new (and hopefully better) way to deal with serialisation. | |||||||||||||||||||||||
| Comment by Mathieu Carbou [ 23/May/13 ] | |||||||||||||||||||||||
|
I completely agree for deserialization. This is up to the application in my opinion. Otherwise it lead to a custom schema structure for enum wich is not easy to query. But for serialzation, I strongly think that the Java driver is not well developped since it makes a lot of static calls without letting the user have any control over how objects are serialized. In my example, I would have liked something like this: Mongo driver = new ... and this method call would modify the encoder behavior and also the serializer behavior. | |||||||||||||||||||||||
| Comment by Trisha Gee [ 23/May/13 ] | |||||||||||||||||||||||
|
We could add support for Enums, but there are a number of problems with doing it by default:
In summary, adding support for enums is possible, but it's tricky, especially from the decoding side. As a general guideline, support for anything other than the BSON primitives is best left to ODMs, which are designed to cater for more complex objects. | |||||||||||||||||||||||
| Comment by Mathieu Carbou [ 22/May/13 ] | |||||||||||||||||||||||
|
You can easily override the encoder. This is at least half of the job done. I've done it liek this:
The code is in groovy but it can be easily translated to Java. And to be able to use enums in queries, I had to override in the classpath the class ClassMapBasedObjectSerializer: i took the source, put it in the package com.mongo.util and added this default constructor:
| |||||||||||||||||||||||
| Comment by A Mare [ 26/Apr/13 ] | |||||||||||||||||||||||
|
Please, can you do something about it?
fail simply because serialization does not work for BasicDBObject with enumerations, since there is no appropriate serializer in JSONSerializers. Thank you. | |||||||||||||||||||||||
| Comment by Gerhard Balthasar [ 13/Mar/12 ] | |||||||||||||||||||||||
|
See also: http://ghads.wordpress.com/2011/04/12/mongodb-and-java-enums/ | |||||||||||||||||||||||
| Comment by Antoine Girbal [ 15/Feb/11 ] | |||||||||||||||||||||||
|
there is no representation for enum in json / bson, so would have to use integers or strings. We could add convenience so that:
For the reflectionDBObject it may be more interesting, if we can automatically handle enums from objects. |