Search code examples
javajackson2

Jackson : map nested object


Using jackson, i wonder if it's possible du map json to Java with nested Object that are not like the json structure.

Here an exemple of what i want to do.

Json :

{
  a = "someValue",
  b = "someValue",
  c = "someValue"
}

Java :

public class AnObject {
  @JsonProperty("a")
  private String value;

  //Nested object
  private SomeObject;
}

public class SomeObject {
  @JsonProperty("b")
  private String value1;

  @JsonProperry("c")
  private String value2;
}

Is it possible ?


Solution

  • Use the JsonUnwrapped annotation:

    @JsonUnwrapped
    private final SomeObject someObject;
    

    which unwrappes all of SomeObject's fields into the parent, resulting in the following when serializing:

    {"a":"foo","b":"bar","c":"baz"}