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 JsonCreator. #562

Open
bimargulies opened this issue Sep 29, 2014 · 10 comments · May be fixed by #4366
Open

Allow 'JsonAnySetter' to flow through JsonCreator. #562

bimargulies opened this issue Sep 29, 2014 · 10 comments · May be fixed by #4366
Labels
2.17 Issues planned at earliest for 2.17 most-wanted Tag to indicate that there is heavy user +1'ing action

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

@JooHyukKim JooHyukKim linked a pull request Feb 4, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.17 Issues planned at earliest for 2.17 most-wanted Tag to indicate that there is heavy user +1'ing action
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants