Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to @Deprecated SerializationFeature.WRITE_EMPTY_JSON_ARRAYS

I'm having difficulties figuring out the correct way to fix the deprecation warnings on SerializationFeature.WRITE_EMPTY_JSON_ARRAYS.

Javadocs state that

Since 2.8 there are better mechanism for specifying filtering; specifically using com.fasterxml.jackson.annotation.JsonFormat or configuration overrides.

but I would assume that

ObjectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);

is a configuration override, although the line above triggers a deprecation warning.

What are other alternatives that do not pollute model classes with yet another annotation? I want to configure the behaviour globally.

like image 957
namero999 Avatar asked Aug 08 '16 21:08

namero999


2 Answers

At the class level, you can use the @JsonInclude like:

@JsonInclude( JsonInclude.Include.NON_EMPTY )
public class MyClass ...

Also, at the mapper level you can do something like:

mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
like image 59
Derek Avatar answered Oct 20 '22 15:10

Derek


The Javadoc is just plain wrong.

  1. "using com.fasterxml.jackson.annotation.JsonFormat": There are plenty of cases where annotations are patently NOT better, most notably when you can't annotate the POJOS because you don't own them, or when you need to override the annototations to get a different behavior than you usually want.

  2. "configuration overrides": That would be great, but to my knowledge there are no other overrides that will take precedence over annotations.

From the user perspective, the only thing bad about the deprecated option is that it is deprecated, and thus might not be supported in the future. That is of course a real concern - you might one day have to choose between a vital security update and keeping your code working without a rewrite.

like image 1
Joachim Lous Avatar answered Oct 20 '22 14:10

Joachim Lous