How to disable SerializationFeature.WRAP_ROOT_VALUE for some specific POJOs?

3,009 views
Skip to first unread message

Juan Felipe Alvarez Saldarriaga

unread,
Oct 6, 2015, 12:07:39 AM10/6/15
to jackson-user
I'm using JSONAPI, so I need to wrap some classes, but not all classes, there's a way to disable tis feature dynamically or from the class itself?, 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. 

Tatu Saloranta

unread,
Oct 6, 2015, 3:44:56 PM10/6/15
to jackso...@googlegroups.com
No, there is no way to dynamically change that on per-class basis.

It is possible to enable/disable wrapping for individual calls, however, by constructing ObjectReader/ObjectWriter with enabled/disabled SerializationFeature/DeserializationFeature, if that would help.

-+ Tatu +-



--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Juan Felipe Alvarez Saldarriaga

unread,
Oct 8, 2015, 10:56:12 PM10/8/15
to jackson-user
Thank you, this question is better elaborate http://stackoverflow.com/questions/32972321/how-to-disable-enable-jackson-serializationfeature-wrap-root-value.

You have any example?, I try this:


And using that ObjectMapper here:

JacksonConverterFactory jacksonConverterFactory = JacksonConverterFactory.create(myObjectMapper);

But, _readMapAndClose method is never called.

Tatu Saloranta

unread,
Oct 9, 2015, 1:30:43 PM10/9/15
to jackso...@googlegroups.com
One problem is that you can not reliably change settings of `ObjectMapper`, due to concurrency limitations. I don't know if that is what happens here, but you really really should not try that on a multi-threaded system; value may or may not be visible across threads, and could cause inconsistent handling.

However: it is perfectly legal to change setting for `ObjectWriter`s and `ObjectReader`s, using `with(feature)` and `without(feature)` methods: they will not modify state of reader/writer instance, but create newly configured ones. That approach should work.
Note, too, that you can also change the root name being used via these objects with method `withRootName()`, not just whether wrapping is used.

I do not know how easy it would be to do that within an existing framework; many tend to only allow configuring of `ObjectMapper`, and not allow easy use of `ObjectReader` / `ObjectWriter`.

-+ Tatu +-

Reply all
Reply to author
Forward
0 new messages