jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Allow for multiple @JsonAnySetter when @JsonUnwrapped is supplied

Open raphw opened this issue 3 years ago • 4 comments

I am dealing with JSON where properties for certain areas are prefixed with given values. For example,

{
  "key" : "val1",
  "@key" : "val2",
  "$key" : "val3"
}

should be represented as a bean of three categories of property:

class Bean {
  @JsonAnySetter
  Map<String, String> normal;
  @JsonAnySetter
  @JsonUnwrapped(prefix = "@")
  Map<String, String> ats;
  @JsonAnySetter
  @JsonUnwrapped(prefix = "$")
  Map<String, String> dollars;
}

I understand that this is rather comples but the serialization works just fine. It would be brilliant being able to deserialize a JSON property if any property would only match one of the unwrapped specifications whereas any other property would end up in the non-unwrapped target.

raphw avatar Oct 10 '20 21:10 raphw

Ah. Yes, I can see how combination would make sense wrt Map-bound @JsonUnwapped entries. Although TBH I think that is one case where use of @JsonAnySetter itself seems superfluous.

Use does not work currently, both in that only POJOs support unwrapping (second oldest open issue, #171) but I can see how that'd make sense.

cowtowncoder avatar Oct 10 '20 23:10 cowtowncoder

Ah, I did not know that there was another open issue on this. I worked myself around this now using a serialization and deserialization converter that is generic enough to redirect any map's value to the correct collection but it is of course a bit tedious to maintain compared to the annotations. If the feature can ever make the cut, I think I'd be appreciated by a few powerusers but I see why this is not a top priority!

raphw avatar Oct 11 '20 21:10 raphw

It would be good to implement; problem lies more with implementation -- @JsonUnwrapped is bit of kludgey. And it has been easier to think of it as 3.0-only as rewrite of handling would be less risky.

cowtowncoder avatar Oct 11 '20 23:10 cowtowncoder

https://github.com/FasterXML/jackson-annotations/issues/202

alxxyz avatar Aug 02 '22 08:08 alxxyz