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:
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.
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.
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).
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.
@JsonTypeInfo is used to indicate details of type information which is to be included in serialization and de-serialization.
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.
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);
@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 :)
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:
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.
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,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With