gpt4 book ai didi

java - setSerializationInclusion(Include.NON_EMPTY) 不排除数组中的空值

转载 作者:行者123 更新时间:2023-11-30 10:11:25 25 4
gpt4 key购买 nike

我已经测试了几种从输出文件 (Json) 中删除 java 对象中的 Null 值的方法。但是,似乎还有一些遗漏了。我正在使用 2.9.6

我试过以下方法:

objectMapper.setSerializationInclusion(Include.NON_NULL);
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
@JsonInclude(Include.NON_NULL);

这是我的代码:

  public static String convertToJson(Hospital patient) {
try {
if (patient != null) {
String json = objectMapper.writeValueAsString(patient);
return json;
}
} catch (IOException e) {
throw new RuntimeException("Error converting person to Json " + e);
}
return null;
}

这些已从输出文件中删除了大约 2150 个空值。但是,结果仍然有一些以以下形式存在:

"patient" : {
"corporateEntityCode" : "OH",
"groupNumber" : "123ABCD",
"patientIdentifier" : 12345678,
"patientPreexistingDiagnosticCode" : [ null, null, null, null, null, null ],
"patientPreexistingEffectiveDate" : [ null, null, null, null, null, null ],
"patientSubscriptNumber" : "01",
"subscriberNumber" : "0000000123456789"
}

这是一个存在的问题吗?还是我没有正确实现?

最佳答案

这似乎不是库中存在的问题,但令人惊讶的是,当使用 Include.NON_NULL 时,它会对 Map 产生影响,忽略valuenull 但它对 ArrayCollection 没有任何影响。无论如何,您可以使用自定义序列化程序很容易地实现您想要的。我创建了一个不同的模型只是为了向您展示如何去做。该示例涵盖了 Arrays 和任何扩展 Collection 的类的自定义序列化。

模型类医院:

public class Hospital {
private List<String> patients;
private String[] doctors;

public List<String> getPatients() {
return patients;
}

public void setPatients(List<String> patients) {
this.patients = patients;
}

public String[] getDoctors() {
return doctors;
}

public void setDoctors(String[] doctors) {
this.doctors = doctors;
}
}

自定义序列化程序忽略扩展 Collection 的类中的 null 值:

 class CollectionIgnoreNullSerializer extends StdSerializer<Collection> {
public CollectionIgnoreNullSerializer() {
this(null);
}

public CollectionIgnoreNullSerializer(Class<Collection> t) {
super(t);
}

@Override
public void serialize(Collection list, JsonGenerator jsonGenerator, SerializerProvider unused) throws IOException {
jsonGenerator.writeStartArray();

list.forEach(e -> {
try {
if (e != null) {
jsonGenerator.writeObject(e);
}
} catch (IOException e1) {
e1.printStackTrace();
}
});

jsonGenerator.writeEndArray();
}
}

自定义序列化程序以忽略 Array 中的 null 值:

class ArrayIgnoreNullSerializer extends StdSerializer<Object[]> {
public ArrayIgnoreNullSerializer() {
this(null);
}

public ArrayIgnoreNullSerializer(Class<Object[]> t) {
super(t);
}

@Override
public void serialize(Object[] array, JsonGenerator jsonGenerator, SerializerProvider unused) throws IOException {
jsonGenerator.writeStartArray();

for (int i = 0; i < array.length; i++) {
try {
if (array[i] != null) {
jsonGenerator.writeObject(array[i]);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}

jsonGenerator.writeEndArray();
}
}

main 只是为了测试它:

public static void main(String[] args) throws IOException {
Hospital hospital = new Hospital();

List<String> patients = Arrays.asList(null, "Bob", "Tom", null, null);
String[] doctors = new String[]{null, "Dr. Grimmer", "Dr. Who", null};

hospital.setPatients(patients);
hospital.setDoctors(doctors);

ObjectMapper mapper = new ObjectMapper();

SimpleModule module = new SimpleModule();
module.addSerializer(Collection.class, new CollectionIgnoreNullSerializer());
// You could apply the custom serialization just to Lists:
// module.addSerializer(List.class, new CollectionIgnoreNullSerializer());

module.addSerializer(String[].class, new ArrayIgnoreNullSerializer());
// You could cover other type of Arrays like this:
// module.addSerializer(Integer[].class, new ArrayIgnoreNullSerializer());
mapper.registerModule(module);

System.out.println(mapper.writeValueAsString(hospital));
}

这是序列化的 json:

{"patients":["Bob","Tom"],"doctors":["Dr. Grimmer","Dr. Who"]}

关于java - setSerializationInclusion(Include.NON_EMPTY) 不排除数组中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52448016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com