question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@JsonCreator ignored when deserializing map keys

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
cowtowncodercommented, Jun 25, 2021

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).

1reaction
cowtowncodercommented, Apr 10, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found