【问题标题】:Jackson mapper: ignore certain properties when writing?Jackson mapper:写作时忽略某些属性?
【发布时间】:2013-03-20 20:59:57
【问题描述】:

我使用 JSON 映射器从字符串中读取对象,然后从对象中写入字符串。我希望只有在写作时才能忽略某些属性。最简单的方法是什么?

【问题讨论】:

标签: java json jackson


【解决方案1】:

在界面上可以使用@JsonIgnoreProperties(ignoreUnknown=true)注解忽略任何没有指定的字段。

例如:

@JsonIgnoreProperties(ignoreUnknown=true)
public static interface Point {
    double getLatitude();

    double getLongitude();
}

这将忽略使用 Point 接口序列化的任何其他字段。

【讨论】:

  • 这没有成功。当我读取 json 对象并将其写入字符串时,它包含所有未设置的“属性:null”
【解决方案2】:

您可以使用@JsonProperty(access = Access.WRITE_ONLY) 忽略序列化的属性。

您可以在下面的链接中详细了解有关此属性的更多信息。

https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonProperty.Access.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多