jackson-databind
jackson-databind copied to clipboard
Allow 'JsonAnySetter' to flow through JsonCreator.
I could speed up some code if I could write:
@JsonCreator
public MyClass(@JsonProperty("a") String a, @JsonAnySetter Map<String, Object> leftovers) {}
:+1: Is this something that you already considered putting in?
Considered, yes, had time to try to tackle, no. Would definitely be nice thing to support.
Any likelihood this will land in 3.x?
Hi, I would also really like this (allows for immutable beans, and validation of data in constructor method).
@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.
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);
}
}
@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.
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).
Also, made
@JsonAnySetter
applicable to (constructor) parameters injackson-annotations
2.17 (was not previously).
This is great. Thank you for taking care of things in advance.
Np. Needed that already for reproduction :)
Quick note: really hoping to get this resolved for 2.18 -- and I think there's a good chance this can happen.
Thanks to PR by @JooHyukKim this is now FINALLY implemented, merged for inclusion in 2.18.0!!!