Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable/enable jackson SerializationFeature.WRAP_ROOT_VALUE?

I'm using JSONAPI, so I need to wrap some classes, but not all classes, like:

{"users": {"aKey": "aValue"}} // wrapped.
{"aKey": "aValue"} // not wrapped.

There's a way to disable tis feature dynamically or from the class itself?,

I try this:

  • https://stackoverflow.com/a/27688284/255463, this class is never called.
  • @JsonRootName(value = ""), doesn't work either.

To wrap/unwrap I'm doing this:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
objectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

JacksonConverterFactory jacksonConverterFactory = JacksonConverterFactory.create(objectMapper);

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new LoggingInterceptor());

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(okHttpClient)
            .addConverterFactory(jacksonConverterFactory)
            .build();

I need some of the POJOs disable that feature, is that possible?.

Thank you.

like image 696
nebiros Avatar asked Oct 06 '15 14:10

nebiros


People also ask

What is SerializationFeature 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 Fail_on_empty_beans?

FAIL_ON_EMPTY_BEANS. public static final SerializationFeature FAIL_ON_EMPTY_BEANS. Feature that determines what happens when no accessors are found for a type (and there are no annotations to indicate it is meant to be serialized).

What is JsonProperty annotation Java?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

What is JsonTypeInfo?

@JsonTypeInfo is used to indicate details of type information which is to be included in serialization and de-serialization.

How to enable wrap_root_value in Spring Boot and Jackson?

By default, SpringBoot and Jackson object mapper disable the WRAP_ROOT_VALUE feature of serialization. So , you need to enable it explicitly by using Configuration Beans or properties. The example source code has been uploaded to github, you can visit here to view the example source codes.

How to enable/disable serializationfeature in mapper?

Aside from that one small thing worth noting is that you can use ObjectWriter to enable/disable SerializationFeature s. String json = objectMapper.writer () .with (SerializationFeature.WRAP_ROOT_VALUE) .writeValueAsString (value);

Does @jsonrootname work for deserialize?

@JsonRootName seemed promising but didn't work. Thank you for this! Also it's work for deserialize. Show activity on this post. 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 :)

How do I serialize an employee class to a JSON object?

Begin by using a Jackson annotation intuitively named @JsonRootName. Let's take a look at an Employee class without that annotation. That's a simple POJO that you've likely seen quite a few times. It defines just three (3) fields as well as related getters and setters. Now serialize an instance of that class to a JSON object with this code:


2 Answers

Currently, no. This is tracked under FasterXML/jackson-databind#1022 As a workaround, you can create two different retrofit instances one with root enabled converter factory and one without.

like image 197
Ameya Joshi Avatar answered Oct 17 '22 02:10

Ameya Joshi


You can workaround this by not using WRAP_ROOT_VALUE/WRAP_UNWROOT_VALUE, but only using the following annotations for the classes where you actually want to wrap/unwrap automatically:

@JsonTypeName(value = "card")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
data class Card(
    val id: UUID,
)
like image 1
edwardmp Avatar answered Oct 17 '22 03:10

edwardmp