因为所有数组项都是相关的。您不能只删除数组中间的项目。如果一个数组至少有一个元素,包括null,它仍然会被写出,因为它不是空的。
非空通常是指键值对(在对象中),其中值为空。这将告诉编组器完全忽略这对。
反序列化 JSON 后,您需要以编程方式过滤掉数组中的 null 项。
故障
myArray : null // (Ignore) null
myArray : [] // (Ignore) not-null & empty
myArray : [null] // (Include) not-null & not-empty
使用自定义的 JsonSerializer。
StringArraySerializer(序列化器)
import java.io.IOException;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
// Derived from: http://stackoverflow.com/a/18645677
public class StringArraySerializer extends JsonSerializer<Object> {
@Override
public boolean isEmpty(SerializerProvider provider, Object value) {
String[] arr = (String[]) value;
return arr == null || arr.length == 0 || (arr.length == 1 && arr[0] == null);
}
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String[] arr = (String[]) value;
jgen.writeStartArray();
for (String item : arr) {
if (item != null) {
jgen.writeString(item);
}
}
jgen.writeEndArray();
}
}
应用(驱动程序)
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
public class App {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false); // Deprecated
Info[] list = {
new Info("Array contains null.", new String[] { "foo", null, "bar" }),
new Info("Array only contains null.", new String[] { null }),
new Info("Array is empty.", new String[] { }),
new Info("Array is null.", null)
};
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
信息(POJO)
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Info {
private String description;
@JsonSerialize(using = StringArraySerializer.class)
private String[] items;
public Info(String description, String[] items) {
super();
this.description = description;
this.items = items;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String[] getItems() {
return items;
}
public void setItems(String[] items) {
this.items = items;
}
}
输出
[ {
"description" : "Array contains null.",
"items" : [ "foo", "bar" ]
}, {
"description" : "Array only contains null."
}, {
"description" : "Array is empty."
}, {
"description" : "Array is null."
} ]
依赖项(Gradle)
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile 'com.fasterxml.jackson.core:jackson-core:2.9.0.pr1'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0.pr1'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.0.pr1'
}