Jackson JSON lib how to indicate the start of an element for deserialization

I am using jackson to deserialize json data. I use objectmapper to de-serialize data and want to ignore unknown properties using mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Is there a way in Jackson to indicate the start of the element that I want to deserialize? My JSON data has a few more elements, but I'm only interested in some element for deserialization.

+4
source share
2 answers

The structure of the object must match the structure of JSON, so it is often convenient to use wrapper classes if necessary. Sort of:

 class Response { public Thing value; } class Thing { public String name; } 

so even if you just want a "name", you would do something like:

 Response resp = mapper.readValue(jsonInput, Response.class); String name = response.value.name; 

If so, you can omit defining properties that you do not need; or identify them and not use them.

+1
source

JAXB supports XPath expressions for mapping elements to classes using @XmlPath(...) .

Not sure if you can skip the root element, but for nesting, you can make inline properties using @XmlPath(".") .

Not sure if Jackson supports this, but EclipeLink JAXB does.

0
source

All Articles