Jackson - serializing a list containing null elements

I am using Jackson 2.4 to serialize objects to JSON.
When I serialize a list of objects with some elements equal to zero, the result string JSON

contains some "null" strings.

How to prevent serialization of elements "null"

? Is there any configuration for ObjectMapper

? I have already installed "setSerializationInclusion(Include.NON_NULL)"

!

Here is my code:

List<String> strings = new ArrayList<>();
strings.add("string 1");
strings.add("string 2");
strings.add(null);
strings.add(null);

      

After serialization, I got this:

[string 1, string 2, null, null]

      

How do I get a JSON string without "null"?

[string 1, string 2]

      

+3


source to share


4 answers


Using @JsonInclude annotation.

@JsonInclude(Include.NON_NULL)
class Foo {
  String bar;

}

      

Edit

Also you can create your own serializer.
For example:

public static void main(String[] args) throws JsonProcessingException {

        List<String> strings = new ArrayList<>();
        strings.add("string 1");
        strings.add("string 2");
        strings.add(null);
        strings.add(null);

        ObjectMapper mapper=new ObjectMapper();
        mapper.getSerializerProvider().setNullValueSerializer(new NullSerializer());
        System.out.println(mapper.writeValueAsString(strings));
    }

      

NullSerializer.java



class NullSerializer extends JsonSerializer<Object>
{
  @Override
  public void serialize(Object obj, JsonGenerator jsonGen, SerializerProvider unused) 
      throws IOException, JsonProcessingException
  {
    jsonGen.writeFieldName("");
  }
}

      

Will be printed

["string 1","string 2","",""]

      

then you can remove jsonGen.writeFieldName (""); for print

["string 1","string 2"]

      

+2


source


try using below

@JsonInclude(Include.NON_NULL)

      



also see for a more detailed explanation.

+1


source


You can prevent this by:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) private String checkNullThenSerialize;

put it in a member ad.

or you can supply the class as well:

@JsonInclude(Include.NON_NULL)
class CheckNullThenSerialize{
  private String fieldData;
  private String fieldNull;
}

      

You can also try Include.NON_EMPTY

from @JsonInclude

change to Include.NON_NULL

.

For ObjectMapper

( Any null field in any class serialized through this handler will be ignored ):

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);

      

Take a look here for details on this topic.

0


source


The suggestion to implement a custom NullSerializer is a great idea, in my case I don't want all null values ​​to be ignored (like external collections), so with a little tweak you can work with both.

class NullSerializer extends JsonSerializer<Object>{
    @Override
    public void serialize(Object obj, JsonGenerator jsonGen, SerializerProvider unused) 
  throws IOException, JsonProcessingException {
        //any null outside of a collection will be serialized (as long as SerilizationInclusion allows nulls)
        if (!jsonGen.getOutputContext().inArray()) {
            jsonGen.writeNull();
        }
    }
}

      

Using this custom NullSerializer, serializing the following class:

class MyTest {
    public List<String> list = Arrays.asList("hello", null, "world", null);
    public Object goodNull = null;
}

      

Produces JSON:

{ "list":["hello","world"], "goodNull":null}

      

0


source







All Articles