Skip to content

Allow @JsonAnySetter to flow through Creators #562

Closed

Description

@bimargulies

I could speed up some code if I could write:

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

Activity

  1. whiskeysierra commented on Jul 8, 2015

    @whiskeysierra

    👍 Is this something that you already considered putting in?

  2. cowtowncoder commented on Jul 8, 2015

    @cowtowncoder
    Member

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

  3. added
    3.xIssues to be only tackled for Jackson 3.x, not 2.x
    and removed on May 4, 2017
  4. huhlig commented on Dec 3, 2018

    @huhlig

    Any likelihood this will land in 3.x?

  5. davide-imbriaco commented on Jan 28, 2019

    @davide-imbriaco

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

  6. cowtowncoder commented on Feb 5, 2019

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

  7. added
    most-wantedTag to indicate that there is heavy user +1'ing action (5 or more)
    on Jan 16, 2023
  8. davidmoten commented on May 12, 2023

    @davidmoten
    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);
        }
    }
  9. cowtowncoder commented on Jan 27, 2024

    @cowtowncoder
    Member

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

  10. added
    need-test-caseTo work on issue, a reproduction (ideally unit test) needed
    and removed
    need-test-caseTo work on issue, a reproduction (ideally unit test) needed
    3.xIssues to be only tackled for Jackson 3.x, not 2.x
    on Jan 27, 2024
  11. cowtowncoder commented on Jan 28, 2024

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

  12. JooHyukKim commented on Jan 28, 2024

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

  13. cowtowncoder commented on Jan 28, 2024

    @cowtowncoder
    Member

    Np. Needed that already for reproduction :)

  14. added
    2.18Issues planned at 2.18 or later
    and removed on Jun 1, 2024
  15. cowtowncoder commented on Jun 1, 2024

    @cowtowncoder
    Member

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

  16. changed the title [-]Allow 'JsonAnySetter' to flow through JsonCreator.[/-] [+]Allow `@JsonAnySetter` to flow through Creators[/+] on Jun 2, 2024
  17. added this to the 2.18.0 milestone on Jun 9, 2024
  18. cowtowncoder commented on Jun 9, 2024

    @cowtowncoder
    Member

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

  19. Nick-Minutello commented on Aug 30, 2024

    @Nick-Minutello

    Excellent. When is 2.18.0 expected to be released ?

  20. JooHyukKim commented on Aug 30, 2024

    @JooHyukKim
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18Issues planned at 2.18 or latermost-wantedTag to indicate that there is heavy user +1'ing action (5 or more)

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions