如何禁用/启用jackson SerializationFeature.WRAP_ROOT_VALUE?

时间:2015-10-06 14:18:02

标签: java android jackson retrofit

我使用JSONAPI,所以我需要包装一些类,但不是所有类,如:

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

有哪种方法可以动态地或从类本身禁用tis功能?,

我试试这个:

包装/解包我这样做:

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();

我需要一些POJO禁用该功能,这可能吗?。

谢谢。

1 个答案:

答案 0 :(得分:1)

目前,没有。这是在FasterXML/jackson-databind#1022下跟踪的 作为一种解决方法,您可以创建两个不同的改造实例,一个具有启用root的转换器工厂,另一个不支持。