What's the Jackson deserialization equivalent of @JsonUnwrapped?
Say I have the following class:
public class Parent {
public int age;
@JsonUnwrapped
public Name name;
}
Producing JSON:
{
"age" : 18,
"first" : "Joey",
"last" : "Sixpack"
}
How do I deserialize this back into the Parent class? I could use @JsonCreator
@JsonCreator
public Parent(Map<String,String> jsonMap) {
age = jsonMap.get("age");
name = new Name(jsonMap.get("first"), jsonMap.get("last"));
}
But this also effectively adds @JsonIgnoreProperties(ignoreUnknown=true)
to the Parent class, as all properties map to here. So if you wanted unknown JSON fields to throw an exception, you'd have to do that yourself. In addition, if the map values could be something other than Strings, you'd have to do some manual type checking and conversion. Is there a way for Jackson to handle this case automatically?
Edit:
I might be crazy, but this actually appears to work despite never being explicitly mentioned in the documentation: http://fasterxml.github.io/jackson-annotations/javadoc/2.2.0/com/fasterxml/jackson/annotation/JsonUnwrapped.html
I was pretty sure it didn't work for me previously. Still, the proposed @JsonCreator approach might be preferred when custom logic is required to deserialize unwrapped polymorphic types.
You can use @JsonCreator
with @JsonProperty
for each field:
@JsonCreator
public Parent(@JsonProperty("age") Integer age, @JsonProperty("firstName") String firstName,
@JsonProperty("lastName") String lastName) {
this.age = age;
this.name = new Name(firstName, lastName);
}
Jackson does type checking and unknown field checking for you in this case.
It does work for deserialization as well, although it's not mentioned in the docs explicitly, like you said. See the unit test for deserialization of @JsonUnwrapped
here for confirmation - https://github.com/FasterXML/jackson-databind/blob/d2c083a6220f2875c97c29f4823d9818972511dc/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java#L138
@JsonUnwrapped
works for both serialization and deserialization, you shouldn't need to take any additional steps.
Despite not being mentioned in the Javadocs prior to Jackson 2.13 (per
jackson-annotations#184), the @JsonUnwrapped
annotation does apply to deserialization as well as serialization, so no additional work is needed to support deserialization of a field using the annotation.
The Jackson 2.13 Javadocs for @JsonUnwrapped
clarify that the annotation applies to deserialization as well as serialization:
Annotation used to indicate that a property should be serialized "unwrapped" -- that is, if it would be serialized as JSON Object, its properties are instead included as properties of its containing Object -- and deserialized reproducing "missing" structure.
[...]
When values are deserialized "wrapping" is applied so that serialized output can be read back in.