杰克逊在将包含空值的Optional序列化为YAML时。

huangapple go评论65阅读模式
英文:

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 &lt;=&gt; 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(&quot;QueueName&quot;)
    private String queueName;

    @JsonProperty(&quot;RedrivePolicy&quot;)
    private RedrivePolicy redrivePolicy;

    public Optional&lt;RedrivePolicy&gt; 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对象。带有nullOptional被视为空,并且我们可以对其使用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&lt;Object&gt; getRedrivePolicy() {
	return Optional.ofNullable(redrivePolicy);
}

huangapple
  • 本文由 发表于 2020年8月26日 00:00:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63582778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定