-
-
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
EnumMap serialization ignores SerializationFeature.WRITE_ENUMS_USING_TO_STRING #749
Comments
Ok. Thank you for reporting this. |
Hmmh. This is a nasty one to fix actually, due to added dynamicity for root-level values. |
…rther work to solve, not an easy fix
Ok. So, the problem here is due to the fact that at root-level, an There are couple of ways to solve the issue. One would require Anyway, there are ways to solve this, but fix is somewhat more involved than I was hoping for, so it'll take bit longer. In the mean time, the usual work-arounds for type erasure would allow working around the problem. class EnumXMap<V> extends EnumMap<EnumX, V> { } should let enum of type
While not ideal, one of these approaches should work to get things going until fix is available. |
My variation on this is not using an EnumMap. Just using a HashMap that happens to have enum keys, the code in MapSerializer is not looking for custom key serializers based on the key type, at all. I think I see how to do this in MapSerializer, I'm going to make a commit and a PR. |
Test case is welcome, I can try to see if I can find a solution. |
On the new test: I'll have to see how it should go, but this may also be a limitation due to type erasure. Work-around here would be to specify
which appears to fix observed behavior here. But it has been a while since I had a look at the implementation so I may be underestimating how implementation works... |
So, adding a cache for key deserializers, at least to the MapSerializer, On Thu, Sep 24, 2015 at 8:49 PM, Tatu Saloranta notifications@github.com
|
A little more detail on our application:
We have an API that accepts a Map<X extends Enum<X>, Object>
The appropriate type of the value depends on the key.
We have one case, where the appropriate value for the top-level map is, in
fact,
Map<SpecialEnum, -something->
The special enum is this enum that is a perpetual pain for us with Jackson;
years ago we make it with enum item names that are completely wrong for
Json. So we're constantly using customization to make sure that we
serialize and deserialize it to the replacement values that make sense.
We can't go back in time and put the 'good' values in as the enum item
names.
If I'm missing some other approach, please let me know.
|
Right: so original reason for requiring Map key type to be (more) static was because it would have been hassle to replicate much of scaffolding. It's not a good reason, but it did not seem like a major limitation for most uses, esp. at the time, and since Map-valued properties are usually (fully) typed. Now: in specific case of But like I said, I haven't re-read code to see how much of One other possible work-around idea is that if you can use Map sub-classes that preserve generic declarations -- like, say, |
@bimargulies Looking at default key serializer, So I think this is actually quite solvable now I am thinking about it some more. |
... even worse, looks like existing key serializer does not follow default |
Ok. So fixing this particular problem, as reported, was not difficult, as it only affects choice of @bimargulies There are other problems for root-level handling (or, more generally, "untyped"), regarding custom serialization via |
Discovered this when upgrading from Jackson 2.4.1 to 2.5.2. Starting in 2.4.4, EnumMapSerializer is deprecated in favor of MapSerializer. However MapSerializer modifies the expected behavior of Enum serialization by always using toString().
Failing unit tests added here:
scubasau@8e2c4cd
The text was updated successfully, but these errors were encountered: