FasterXML / jackson-annotations

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
https://github.com/FasterXML/jackson
Apache License 2.0
1.03k stars 330 forks source link

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

Closed huhlig closed 5 years ago

huhlig commented 5 years ago

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 commented 5 years ago

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

https://github.com/FasterXML/jackson-databind/issues/562

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

huhlig commented 5 years ago

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?

cowtowncoder commented 5 years ago

@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 commented 5 years ago

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