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 ?
A JsonNode is Jackson's tree model for JSON and it can read JSON into a JsonNode instance and write a JsonNode out to JSON. To read JSON into a JsonNode with Jackson by creating ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of JsonNode class.
@JsonRootName allows to have a root node specified over the JSON. We need to enable wrap root value as well.
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"}
                        Using ObjectMapper you can convert JSON string to Object. Use JsonUnwrapped in your AnObject class over someObject field.
@JsonUnwrapped
private SomeObject someObject;
then read JSON string and convert it to AnObject.
ObjectMapper mapper = new ObjectMapper();
try {
   AnObject anObject1 = mapper.readValue(jsonString, AnObject.class);   
} catch (IOException e) {
    e.printStackTrace();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With