简体   繁体   English

杰克逊·梅珀(Jackson Mapper):写作时忽略某些属性?

[英]Jackson mapper: ignore certain properties when writing?

I use JSON mapper to read an object from a string and then write the string from the object. 我使用JSON映射器从字符串中读取对象,然后从对象中写入字符串。 I would like to be able to ignore some properties only when writing. 我希望仅在编写时才能忽略某些属性。 What is the easiest way to accomplish this? 最简单的方法是什么?

On the interface you can use the @JsonIgnoreProperties(ignoreUnknown=true) annotation to ignore any fields that have not been specified. 在界面上,您可以使用@JsonIgnoreProperties(ignoreUnknown=true)批注忽略所有未指定的字段。

For example: 例如:

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

    double getLongitude();
}

This will ignore any other fields that are serialized using the Point interface. 这将忽略使用Point接口序列化的任何其他字段。

You can use @JsonProperty(access = Access.WRITE_ONLY) to ignore a property for serialization. 您可以使用@JsonProperty(access = Access.WRITE_ONLY)忽略要序列化的属性。

You can fine more information about this property at the below link. 您可以在下面的链接中获取有关此属性的更多信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM