20

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>
Patrick
  • 12,336
  • 15
  • 73
  • 115
  • Maybe this: http://stackoverflow.com/a/31158706/829571 See also: https://github.com/FasterXML/jackson-annotations/issues/33 – assylias Jun 17 '16 at 13:13
  • @assylias yes saw that answer, too. But dont know how to get it as lower camel case. Need `user` and not `User`. Not sure if this is possible – Patrick Jun 17 '16 at 13:19

3 Answers3

40
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"
  }
}
isank-a
  • 1,545
  • 2
  • 17
  • 22
0

I think this has been requested as:

 https://github.com/FasterXML/jackson-databind/issues/1022

so if anyone wants a challenge & a chance to make many users happy (it is something that'd be nice to have for sure), it's up for grabs :)

Aside from that one small thing worth noting is that you can use ObjectWriter to enable/disable SerializationFeatures.

String json = objectMapper.writer()
   .with(SerializationFeature.WRAP_ROOT_VALUE)
   .writeValueAsString(value);

in case you need to sometimes use this, other times not (ObjectMapper settings should not be changed after initial construction and configuration).

StaxMan
  • 113,358
  • 34
  • 211
  • 239
-1

You can use the it in the below way, so this property will be applied to throughout the objectMapper usage.

static {
    objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
}
Jithesh Nair
  • 131
  • 1
  • 4
  • 11