@JsonCreator ignored when deserializing map keys
See original GitHub issueI’m unsure whether this is a bug or expected behavior, but I found it a bit surprising. Given the following class:
class Key {
private final String value;
private Key(final String value) {
this.value = value;
}
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static Key of(final String value) {
return new Key(value);
}
}
Then, when a Key
value is deserialized, the factory method annotated with @JsonCreator
is called as expected. If Key
is used as a key in a Map
though, the factory method is not called and Key’s private constructor is used directly instead.
Key
is of course reduced to demonstrate the issue and hence lacking all properties that would make it actually useful as a key in a map.
I tried the above with version 2.9.7. A repository demonstrating the issue can be found at https://github.com/Trundle/jackson-databind-oddity
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (10 by maintainers)
Top Results From Across the Web
Deserializing non-string map keys with Jackson - Stack Overflow
In my case a non-String map key was configured on my class but it wasn't in ... JsonGenerator gen, SerializerProvider provider) throws IOException...
Read more >Map Serialization and Deserialization with Jackson - Baeldung
A quick and practical guide to serializing and deserializing Java Maps using Jackson.
Read more >JSON serialization - Immutables
Collections mapped to JSON arrays; Map and Multimaps mapped to JSON object (keys always converted to strings); Optional attributes - as nullable fields....
Read more >SUSE alert SUSE-SU-2022:1678-1 (jackson-databind ...
Thread fails on JDK 11 and above + String-based 'Map' key ... invoked when trying to deserialize String + Fix failing 'double' JsonCreators...
Read more >Using @JsonAnyGetter Annotation to serialize any ... - LogicBig
otherInfo.put(key, value); } @JsonAnyGetter public Map<String, Object> getOtherInfo() ... 200); System.out.println("-- before serialization ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this was solved along with #3143 – test now passes. Fix will be included in 2.13.0 (since it required bigger refactoring of things unfortunately, cannot safely backport).
@Stephan202 that’ll be awesome, whenever – I probably won’t have time to look into this particular issue immediately anyway. But will be good to have reproduction.