using jackson 2.x
the json response looks like this:
{
"flag": true,
"important": {
"id": 123,
"email": "[email protected]"
}
}
The "flag" key does not provide any useful information. I would like to ignore the "flag" key and unwrap the "important" value to an instance of Important.
public class Important {
private Integer id;
private String email;
public Important(@JsonProperty("id") Integer id,
@JsonProperty("email") String email) {
this.id = id;
this.email = email;
}
public String getEmail() { this.email }
public Integer getId() { this.id }
}
When I try to add a @JsonRootName("important") to Important and configure the ObjectMapper with DeserializationFeature.UNWRAP_ROOT_VALUE I receive a JsonMappingException:
Root name 'flag' does not match expected ('important') for type ...
When i remove the "flag" key/value from the JSON the data binding works just fine. I get the same result if i add @JsonIgnoreProperties("flag") to Important as well.
UPDATES
updated class ... that will actually pass the compile step
@JsonRootName("important")
public static class Important {
private Integer id;
private String email;
@JsonCreator
public Important(@JsonProperty("id") Integer id,
@JsonProperty("email") String email) {
this.id = id;
this.email = email;
}
public String getEmail() { return this.email; }
public Integer getId() { return this.id; }
}
actual test:
@Test
public void deserializeImportant() throws IOException {
ObjectMapper om = new ObjectMapper();
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Important important = om.readValue(getClass().getResourceAsStream("/important.json"), Important.class);
assertEquals((Integer)123, important.getId());
assertEquals("[email protected]", important.getEmail());
}
results:
com.fasterxml.jackson.databind.JsonMappingException: Root name 'flag' does not match expected ('important') for type [simple type, class TestImportant$Important]