-
-
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
Serializing a Map<K, V>
field uses declared type for key serialization rather than key instance type
#4736
Comments
Even if this is changed, it will require a configuration that only enables the change if it explicitly set by the user. We might be able to change the behaviour in Jackson 3 but in Jackson 2, we need to assume that there are users who depend on the existing behaviour. The suggested new behaviour is probably also going to run slower as you need to go around checking more classes. Jackson supports the registration of custom key serializers so you can workaround this by using this approach. https://www.baeldung.com/jackson-map includes some documenation about this. |
I agree suggested behavior would add runtime, overhead type-checking for every key instance. I expect a lot of people will come and complain about performance.
Only if we agree that suggested behavior should be supported? +1 on configuration. |
I figured it'd be a bug given that values are serialized based on the instance's type, it was quite unexpected to see keys weren't, but if this is intentional I'm more than happy to close this report in favour of a feature request instead |
This works as intended: annotation introspection for I have no interest in changing this myself, but I am not against feature requests being filed (or this being changed). |
Search before asking
Describe the bug
When serializing a field of type
Map<K, V>
, Jackson will inspect the declaredK
type for serialization annotations, rather than the individual entries key's instance type. This creates issues whereK
is a supertype and the map holds subtypes ofK
(K'
) that do specify serialization annotations.Values are serialized as expected and the instance type is used for annotation introspection.
Version Information
2.18.0
Reproduction
Expected behavior
In the reproducer above, the first
println
prints{"values":["foo",123]}
, the secondprintln
prints{"values":{"StringValue[value=bar]":456}}
, using the defaulttoString()
for the key. The thirdprintln
is included to display that the issue is when serializing a map field, not a map value directly as that third prints{"baz":789}
which is the kind of serialization behavior I'd expect for the second case as well.Enabling the
@JsonKey
in the interface's method makes it behave how I'd expect, and the secondprintln
now prints{"values":{"bar":456}}
. The annotation's presence when disabled doesn't play a role, as it doesn't behave any differently when it isn't there (as expected).Additional context
The most relevant issue I could find is #3119 given the inheritance setting, but explicitly specifying
@JsonSerialize(keyUsing = ...)
behaves correctly so I ruled that out, but they might be related in a way I can't see.The text was updated successfully, but these errors were encountered: