-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserialize Enums using ordinal does not work for map keys #1877
Comments
After upgrade from 2.7 to 2.9, our app is broken due to this issue too. The change seems introduced in #1570 BTW, since json's key is always string type, IMO it is uncommon to write enum to a string of number, it would be better using a different flag, e.g. SerializationFeature.WRITE_MAP_KEY_ENUMS_USING_INDEX, so user could opt-in(or out) |
@renzihui Just to make sure: if the behavior that broke is different from this issue, please file a new one specifically for that. Similarly, idea of separate serialization feature (which I think makes sense) is better filed as a new issue. That way I can address specific original problem here, and then consider addition separately -- this is necessary because new feature enums can only be introduced in new minor version, whereas I can fix bugs in a patch release. Also, note that there is |
@cowtowncoder |
Closing this in favor of #2129. |
We are using java enums to communicate between java and c. So we decided to serialize enums using the java ordinal. The SerializationFeature.WRITE_ENUMS_USING_INDEX works fine for that. Thank you for that feature!
By the way, we are using java 1.8.0_151 and jackson-databind 2.9.2
Concerning deserialization we hit an unbalance concerning enums as map key:
Within the serialization step the enum is written as ordinal.
Within the deserialization the interpretation of the ordinal as enum failes only for map keys.
For common properties the deserialization from ordinal number works fine.
The following sample code describes this more precise:
This code produces the following output. Serialization works fine, deserialization fails on the map key:
Running the code above without
SerializationFeature.WRITE_ENUMS_USING_INDEX
set to true, everything works fine with following output:{"simpleType":"ANY","map":{"OTHER":"hello world"}}
So deserialization fails only for enums repesented as ordinal used as map keys!
Regarding the code in jackson-databind we found an unbalance in the Deserialization-Classes:
The usual
com.fasterxml.jackson.databind.deser.std.EnumDeserializer
used for simple properties checks forJsonToken#VALUE_NUMBER_INT
and has code path for enums as ordinal. Source code is commented byBut let's consider int acceptable as well (if within ordinal range)
Unlike the
com.fasterxml.jackson.databind.deser.std.StdKeyDeserializer.EnumKD
used for map keys uses only a_byNameResolver
and has no code path for resolving by ordinal. This explains why resolving by ordinal does not work for map keys.This unbalanced situation seems like a missing feature or bug.
Our workaround: We use a custom deserializer for our enum.
So in
com.fasterxml.jackson.databind.deser.BasicDeserializerFactory#_createEnumKeyDeserializer
for the map key a different code path is used, triggering acom.fasterxml.jackson.databind.deser.std.StdKeyDeserializer.DelegatingKD
which delegates to our custom deserializer which is able to resolves enum by ordinal.Not a very elegant workaround, because we have to register a concrete deserializer only for those enums which are used as map keys. Enums only used as normal properties are fine with the standard EnumDeserializer.
The most relevant issue we found here is #1674 - this issue mentions the unbalance between deserializer for normal properties versus deserializier for map keys. But this issue does not mention the enum ordinal in any way. May be relating to #1859 but this issue regards unknown enum values, not ordinals.
Regarding issue #1882 I tried a EnumMap instead of a HashMap. But this didn't change anything. Same Exception.
The text was updated successfully, but these errors were encountered: