英文:
Jackson serialising Optional with null value for YAML
问题
目前我正在使用YAMLFactory来配置ObjectMapper以进行Pojos <=> YAML的序列化和反序列化,然而尽管尝试了在Jackson中常用的技巧,但在序列化过程中它仍然会写入null值。
在类级别或字段级别使用@JsonInclude(JsonInclude.Include.NON_NULL)进行注解对此没有影响。我还尝试过在类上使用@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL),但也没有任何影响。在使用YAMLFactory时,应该如何实现此功能呢?
我找到了一个类似的问题,但用例似乎不太相同。明确地说,我正在尝试完全省略该字段。
编辑:这里有一个示例(我还在使用Lombok)
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class QueueProperties extends Properties {
@JsonProperty("QueueName")
private String queueName;
@JsonProperty("RedrivePolicy")
private RedrivePolicy redrivePolicy;
public Optional<RedrivePolicy> getRedrivePolicy() {
return Optional.ofNullable(redrivePolicy);
}
}
序列化时:
Properties:
QueueName: 471416d1-3643-4d5a-a033-65f22757dcaf-chargeback-paypal-ingestion-ingest_dispute
RedrivePolicy: null
ObjectMapper配置:
ObjectMapper mapper = new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES));
mapper.registerModule(new Jdk8Module());
英文:
Currently I am using the YAMLFactory to configure the ObjectMapper to serialise and deserialise Pojos <=> YAML, however it writes null values in serialisation despite attempting the usual tricks in Jackson.
Annotating with @JsonInclude(JsonInclude.Include.NON_NULL) on the class level or the field level has no affect. I have also tried annotating classes with @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) with no affect either. How do you achieve this when using the YAMLFactory?
I have found a similar question but the use case does not appear to be the same. To be clear I am trying to omit the field altogether.
Edit: Here is an example (I am also using Lombok)
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class QueueProperties extends Properties {
@JsonProperty("QueueName")
private String queueName;
@JsonProperty("RedrivePolicy")
private RedrivePolicy redrivePolicy;
public Optional<RedrivePolicy> getRedrivePolicy() {
return Optional.ofNullable(redrivePolicy);
}
}
when serialized:
Properties:
QueueName: 471416d1-3643-4d5a-a033-65f22757dcaf-chargeback-paypal-ingestion-ingest_dispute
RedrivePolicy: null
ObjectMapper configuration:
ObjectMapper mapper = new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES));
mapper.registerModule(new Jdk8Module());
答案1
得分: 3
getRedrivePolicy获取方法总是返回not-null对象,即使Optional可以引用null。在这种情况下,您应该跳过empty对象。带有null的Optional被视为空,并且我们可以对其使用JsonInclude.Include.NON_EMPTY。您可以保留类级别的@JsonInclude(JsonInclude.Include.NON_NULL),并仅为给定的getter添加NON_EMPTY:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public Optional<Object> getRedrivePolicy() {
return Optional.ofNullable(redrivePolicy);
}
英文:
getRedrivePolicy getter method always returns not-null object, even so, Optional could reference to null. In this case, you should skip empty objects. Optional with null is considered as empty and we can use JsonInclude.Include.NON_EMPTY for it. You can keep @JsonInclude(JsonInclude.Include.NON_NULL) on class level and add NON_EMPTY only for given getter:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public Optional<Object> getRedrivePolicy() {
return Optional.ofNullable(redrivePolicy);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论