Skip to content
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

Allow @JsonAnySetter to flow through Creators #562

Closed
bimargulies opened this issue Sep 29, 2014 · 14 comments · Fixed by #4558
Closed

Allow @JsonAnySetter to flow through Creators #562

bimargulies opened this issue Sep 29, 2014 · 14 comments · Fixed by #4558
Labels
2.18 most-wanted Tag to indicate that there is heavy user +1'ing action
Milestone

Comments

@bimargulies
Copy link

I could speed up some code if I could write:

@JsonCreator
public MyClass(@JsonProperty("a") String a, @JsonAnySetter Map<String, Object> leftovers) {}
@whiskeysierra
Copy link

👍 Is this something that you already considered putting in?

@cowtowncoder
Copy link
Member

Considered, yes, had time to try to tackle, no. Would definitely be nice thing to support.

@huhlig
Copy link

huhlig commented Dec 3, 2018

Any likelihood this will land in 3.x?

@davide-imbriaco
Copy link

Hi,
I would also really like this (allows for immutable beans, and validation of data in constructor method).

@cowtowncoder
Copy link
Member

@huhlig It would be nice to get it there, but I can not predict whether it will or not. I do think it would be great to get it to work but there are so many things to work on...

I will keep this in mind as one of "more/most wanted" issues.

@cowtowncoder cowtowncoder added the most-wanted Tag to indicate that there is heavy user +1'ing action label Jan 16, 2023
@davidmoten
Copy link
Contributor

I'd also like to see this feature. As a workaround for the immutability enthusiasts, here's an example of using JsonAnySetter in an immutable class (swap out the Map/HashMap for an immutable equivalent as you like). The trick is that the JsonAnySetter method can be made private.

@JsonAutoDetect(fieldVisibility = Visibility.ANY, creatorVisibility = Visibility.ANY, setterVisibility = Visibility.ANY)
public static final class WithMap {

    @JsonProperty("name")
    private String name;

    @JsonAnyGetter
    private Map<String, String> map;

    @JsonCreator
    public WithMap(@JsonProperty("name") String name) {
        this.name = name;
        this.map = new HashMap<>();
    }
    
    public WithMap(String name, Map<String, String> map) {
        this.name = name;
        this.map = map;
    }
    
    @JsonAnySetter
    private void put(String key, String value) {
        map.put(key, value);
    }
    
    public String name() {
        return name;
    }

    public Map<String, String> map() {
        return Collections.unmodifiableMap(map);
    }
}

@cowtowncoder
Copy link
Member

cowtowncoder commented Jan 27, 2024

@JooHyukKim I think we could use a "failing" test case for this, as pre-cursor to #3439.

Note, too, that this is definitely one of Most-Wanted issues open.

@cowtowncoder cowtowncoder added need-test-case To work on issue, a reproduction (ideally unit test) needed 2.17 Issues planned at earliest for 2.17 and removed need-test-case To work on issue, a reproduction (ideally unit test) needed 3.x Issues to be only tackled for Jackson 3.x, not 2.x labels Jan 27, 2024
@cowtowncoder
Copy link
Member

Added a failing test, as a minor help for anyone considering to try to implement.

Also, made @JsonAnySetter applicable to (constructor) parameters in jackson-annotations 2.17 (was not previously).

@JooHyukKim
Copy link
Member

Also, made @JsonAnySetter applicable to (constructor) parameters in jackson-annotations 2.17 (was not previously).

This is great. Thank you for taking care of things in advance.

@cowtowncoder
Copy link
Member

Np. Needed that already for reproduction :)

@cowtowncoder
Copy link
Member

Quick note: really hoping to get this resolved for 2.18 -- and I think there's a good chance this can happen.

@cowtowncoder cowtowncoder changed the title Allow 'JsonAnySetter' to flow through JsonCreator. Allow @JsonAnySetter to flow through Creators Jun 2, 2024
@cowtowncoder cowtowncoder added this to the 2.18.0 milestone Jun 9, 2024
@cowtowncoder
Copy link
Member

Thanks to PR by @JooHyukKim this is now FINALLY implemented, merged for inclusion in 2.18.0!!!

@Nick-Minutello
Copy link

Excellent. When is 2.18.0 expected to be released ?

@JooHyukKim
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.18 most-wanted Tag to indicate that there is heavy user +1'ing action
Projects
None yet
8 participants