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

Add @JsonFallback annotation for Property Constructor to handle Unknown Fields. #147

Closed
huhlig opened this issue Dec 3, 2018 · 4 comments

Comments

@huhlig
Copy link

huhlig commented Dec 3, 2018

Request to create a new Annotation called @JsonFallback to handle any unknown fields in a property based Constructor. This allows for the normal usage of a property constructor and strong typing but also allows for final classes to be constructed with "additional" unbound fields. This could be useful with a String/Object, String/String Map or similar.

public class MyClass {
    public final int id;
    public final String name;
    public final Map<String,Object>extraFields;
    public MyClass(
        @JsonProperty("id") final int id,
        @JsonProperty("name") final String name;
        @JsonFallback Map<String, Object>extraFields
    ) {
        this.id = id;
        this.name = name;
        this.extraFields = ImmutableMap.of(extraFields);
    }
}
@cowtowncoder
Copy link
Member

Usage makes sense, but I think there is already an issue in jackson-databind where @JsonAnySetter annotation would serve the same role:

FasterXML/jackson-databind#562

which I think might do what you want? (if it gets implemented)

@huhlig
Copy link
Author

huhlig commented Dec 3, 2018

Hadn't seen that one but it makes sense and would do what I'm looking for. Although given that it's 3 years old what's the likelihood of it being implemented?

@huhlig huhlig closed this as completed Dec 3, 2018
@cowtowncoder
Copy link
Member

cowtowncoder commented Dec 3, 2018

@huhlig Fair point, but I think chances are better than age would suggest mostly because:

  1. We are working on 3.0 (... although, with time constraints etc, slowly...).
  2. I consider this both useful and more important (relatively speaking) than many other things.

But someone still needs to work on it: it's not trivial to implement: not necessarily difficult, but lots of work to pipe things through.

Oh and the reason (1) matters (3.0) is that the whole Creator method handling code really could use a rewrite, and this change would benefit from rewrite.

@huhlig
Copy link
Author

huhlig commented Dec 3, 2018

Alright, I've closed this out and commented on the other ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants