8

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.

Community
  • 1
  • 1
nebiros
  • 717
  • 10
  • 30

2 Answers2

2

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.

2

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,
)
edwardmp
  • 6,339
  • 5
  • 50
  • 77