How to deserialize an EnumSet?

435 views
Skip to first unread message

da...@bakins-bits.com

unread,
Oct 2, 2015, 8:31:50 PM10/2/15
to jackson-user
Can someone show me how to deserialize an EnumSet (presumably using EnumSetDeserializer)?  Serialize works as expected, but deserialize fails in the face of type erasure.  I can't find an example anywhere ... yet the class EnumSetDeserializer is there.

Perhaps you can't do it for a "standalone" "root" EnumSet but it does work (somehow) for an EnumSet which is a property of a class which you are serializing instead?

For this purpose the EnumSet is instantiated at a fixed type, e.g., it will always be EnumSet<Foo> for some enum type Foo, either standalone or as a property in a containing class.  So a solution which doesn't need type metadata written to the JSON would be extra nice (though a solution which does have type metadata written to the JSON is ok I suppose).

(I know how to finesse the issue by using an array instead, as arrays don't suffer type erasure, but the property type is more naturally a set ... and the class EnumSetDeserializer is there ...)

Thanks! -- David

Tatu Saloranta

unread,
Oct 2, 2015, 8:40:35 PM10/2/15
to jackso...@googlegroups.com
It should "Just Work". How are you trying to use it?
There are bunch of tests at

src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java

for example.

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

da...@bakins-bits.com

unread,
Oct 2, 2015, 10:20:43 PM10/2/15
to jackson-user
You're right.  I didn't understand how to use the TypeReference parameter to one of the readValue() overloads to tell it what to do.  Thanks for the pointer to the test case! -- David

Tatu Saloranta

unread,
Oct 2, 2015, 10:21:55 PM10/2/15
to jackson-user
Ah. Glad it works out then; Java's Type Erasure can be a pain.

-+ Tatu +-

--
Reply all
Reply to author
Forward
0 new messages