EnumSerializer fails when when multiple enums are used
lift-json-ext's EnumSerializer and EnumNameSerializer do not work properly when multiple enums are used to serialize/deserialize data.
The root cause appears to be the use of,
which returns the same class for all Enumeration-deriving classes, and therefore the subsequent match:
matches against any possible enums, leading to -- at best -- a runtime failure because the enum value cannot be found, or -- at worst -- a sneaky failure since a different enum value may be returned.
I'm attaching a test that demonstrates the issue with EnumNameSerializer.
The root cause appears to be the use of,
val EnumerationClass = classOf[E#Value]
which returns the same class for all Enumeration-deriving classes, and therefore the subsequent match:
PartialFunction[(TypeInfo, JValue), E#Value] = {
case (TypeInfo(EnumerationClass, _), json) => json match {
matches against any possible enums, leading to -- at best -- a runtime failure because the enum value cannot be found, or -- at worst -- a sneaky failure since a different enum value may be returned.
I'm attaching a test that demonstrates the issue with EnumNameSerializer.
Leave a comment
on 2011-07-29 22:58 *
By Alex Boisvert
Description changed from lift-json-ext's EnumSeriali... to lift-json-ext's EnumSeriali...
on 2011-09-22 20:58 *
By justinpatterson
I ran into the same problem. I've created a Serializer named EnumerationSerializer (attached) that stores the enum type along with the value so that it can make sure it deserializes to the correct type. You can either use the enum class name as the key or specify one (the optional second argument). If you specify one, you need to make sure that it's unique among all the configured EnumerationSerializers.
Specifying a different serialization key allows you to shorten the serialized form as well as decouple it from the implementation.
Specifying a different serialization key allows you to shorten the serialized form as well as decouple it from the implementation.
on 2011-11-01 14:15 *
By iron9light
Me too