Reputation: 2134
How to ignore parent tag from json??
Here is my json
String str = "{\"parent\": {\"a\":{\"id\": 10, \"name\":\"Foo\"}}}";
And here is the class to be mapped from json.
public class RootWrapper {
private List<Foo> foos;
public List<Foo> getFoos() {
return foos;
}
@JsonProperty("a")
public void setFoos(List<Foo> foos) {
this.foos = foos;
}
}
Here is the test public class JacksonTest {
@Test
public void wrapRootValue() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String str = "{\"parent\": {\"a\":{\"id\": 10, \"name\":\"Foo\"}}}";
RootWrapper root = mapper.readValue(str, RootWrapper.class);
Assert.assertNotNull(root);
}
I get the error ::
org.codehaus.jackson.map.JsonMappingException: Root name 'parent' does not match expected ('RootWrapper') for type [simple type, class MavenProjectGroup.mavenProjectArtifact.RootWrapper]
I found the solution given by Jackson annotation::
(a) Annotate you class as below
@JsonRootName(value = "parent")
public class RootWrapper {
(b) It will only work if and only if ObjectMapper is asked to wrap.
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
Job Done!!
Another hiccup with Jackson way of Deserialization :(
if 'DeserializationConfig.Feature.UNWRAP_ROOT_VALUE configured', it unwrap all jsons, eventhough my class in not annotated with @JsonRootName(value = "rootTagInJson"), isn't weired.
I want to unwrap root tag only if the class is annotated with @JsonRootName otherwise, don't unwrap.
So below is the usecase for unwrap root tag.
###########################################################
Unwrap only if the class is annotated with @JsonRootName.
############################################################
I did a small change in ObjectMapper of Jackson source code and created a new version of jar. 1. Place this method in ObjectMapper
// Ash:: Wrap json if the class being deserialized, are annotated
// with @JsonRootName else do not wrap.
private boolean hasJsonRootName(JavaType valueType) {
if (valueType.getRawClass() == null)
return false;
Annotation rootAnnotation = valueType.getRawClass().getAnnotation(JsonRootName.class);
return rootAnnotation != null;
}
2. Edit ObjectMapper method ::
Replace
cfg.isEnabled(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE)
with
hasJsonRootName(valueType)
3. Build your jar file and use it.
Upvotes: 47
Views: 88913
Reputation: 7653
An example taken from TestRootName.java
in https://github.com/FasterXML/jackson-databind may give a better way of doing this. Specifically using withRootName("")
:
private ObjectMapper rootMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
return mapper;
}
public void testRootUsingExplicitConfig() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer().withRootName("wrapper");
String json = writer.writeValueAsString(new Bean());
assertEquals("{\"wrapper\":{\"a\":3}}", json);
ObjectReader reader = mapper.reader(Bean.class).withRootName("wrapper");
Bean bean = reader.readValue(json);
assertNotNull(bean);
// also: verify that we can override SerializationFeature as well:
ObjectMapper wrapping = rootMapper();
json = wrapping.writer().withRootName("something").writeValueAsString(new Bean());
assertEquals("{\"something\":{\"a\":3}}", json);
json = wrapping.writer().withRootName("").writeValueAsString(new Bean());
assertEquals("{\"a\":3}", json);
bean = wrapping.reader(Bean.class).withRootName("").readValue(json);
assertNotNull(bean);
}
Upvotes: 40
Reputation: 29
I was also facing this type issue. and I added below line of codes in configuration class where I defined the configuration of RestTemplate and its MessageConverters.
Annotation rootAnnotation = mapper.getTypeFactory().getClass().getAnnotation(JsonRootName.class); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, Objects.nonNull(rootAnnotation));
Basically this configuration will help Mapper to identify if provided class is annotated with '@JsonRootName', if it is not then no need to search for root. Hope It will help you and others.
Upvotes: 0
Reputation: 2571
Its very simple:
Create an instance of object mapper and enable wrapping and unwrapping root value
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
Add
@JsonRootName("yourname")
annotation to you DTO
@JsonRootName("root")
public class YourDto {
// ...
}
Upvotes: 4
Reputation: 1
As an update Seagabond's post, if you want to have the same effect when you write parameter values you can override the additional write methods.
@Component
public class ObjectMapper extends com.fasterxml.jackson.databind.ObjectMapper {
private void autoconfigureFeatures(Object value) {
JavaType javaType = _typeFactory.constructType(value.getClass());
autoconfigureFeatures(javaType);
}
private void autoconfigureFeatures(JavaType javaType) {
Annotation rootAnnotation = javaType.getRawClass().getAnnotation(JsonRootName.class);
this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, rootAnnotation != null);
}
@Override
public void writeValue(DataOutput out, Object value) throws IOException {
autoconfigureFeatures(value);
super.writeValue(out, value);
}
@Override
public void writeValue(Writer w, Object value) throws IOException, JsonGenerationException, JsonMappingException {
autoconfigureFeatures(value);
super.writeValue(w, value);
}
@Override
public byte[] writeValueAsBytes(Object value) throws JsonProcessingException {
autoconfigureFeatures(value);
return super.writeValueAsBytes(value);
}
@Override
public String writeValueAsString(Object value) throws JsonProcessingException {
autoconfigureFeatures(value);
return super.writeValueAsString(value);
}
@Override
protected Object _readMapAndClose(JsonParser jsonParser, JavaType javaType) throws IOException, JsonParseException, JsonMappingException {
autoconfigureFeatures(javaType);
return super._readMapAndClose(jsonParser, javaType);
}
}
Upvotes: 0
Reputation: 1143
I experienced a similar problem developing a restful application in Spring. I had to support a very heterogeneous API, some of it had root elements, another did not. I could not find a better solution than to configure this property realtime. It's a great pity there is no support for per-class root element unwrapping in Jackson. Anyway, somebody may find this helpful.
@Component
public class ObjectMapper extends com.fasterxml.jackson.databind.ObjectMapper {
private void autoconfigureFeatures(JavaType javaType) {
Annotation rootAnnotation = javaType.getRawClass().getAnnotation(JsonRootName.class);
this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, rootAnnotation != null);
}
@Override
protected Object _readMapAndClose(JsonParser jsonParser, JavaType javaType) throws IOException, JsonParseException, JsonMappingException {
autoconfigureFeatures(javaType);
return super._readMapAndClose(jsonParser, javaType);
}
}
Upvotes: 15