Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SerializationFeature.WRAP_ROOT_VALUE as annotation in jackson json

Is there a way to have the configuration of SerializationFeature.WRAP_ROOT_VALUE as an annotation on the root element instead using ObjectMapper?

For example I have:

@JsonRootName(value = "user")
public class UserWithRoot {
    public int id;
    public String name;
}

Using ObjectMapper:

@Test
public void whenSerializingUsingJsonRootName_thenCorrect()
  throws JsonProcessingException {
    UserWithRoot user = new User(1, "John");

    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
    String result = mapper.writeValueAsString(user);

    assertThat(result, containsString("John"));
    assertThat(result, containsString("user"));
}

Result:

{
    "user":{
        "id":1,
        "name":"John"
    }
}

Is there a way to have this SerializationFeature as an annotation and not as an configuration on the objectMapper?

Using dependency:

<dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
     <version>2.7.2</version>
</dependency>
like image 762
Patrick Avatar asked Jun 17 '16 13:06

Patrick


People also ask

What is Wrap_root_value?

We can use the "WRAP_ROOT_VALUE" feature of SerializationFeature enum that can be enabled to make root value wrapped within a single property JSON object where the key is a root name.

What is the use of @JsonIgnore annotation?

@JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setters, getters or fields. If you add @JsonIgnore to a field or its getter method, the field is not going to be serialized.

What is the use of @JsonProperty annotation?

@JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.

How do you ignore NULL values in JSON response?

In order to ignore null fields at the class level, we use the @JsonInclude annotation with include. NON_NULL.


1 Answers

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test2 {
    public static void main(String[] args) throws JsonProcessingException {
        UserWithRoot user = new UserWithRoot(1, "John");

        ObjectMapper objectMapper = new ObjectMapper();

        String userJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);

        System.out.println(userJson);
    }

    @JsonTypeName(value = "user")
    @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
    private static class UserWithRoot {
        public int id;
        public String name;
    }
}

@JsonTypeName and @JsonTypeInfo together make it possible.

Result:

{
  "user" : {
    "id" : 1,
    "name" : "John"
  }
}
like image 161
Isank Avatar answered Sep 20 '22 20:09

Isank