gpt4 book ai didi

java - 使用 JsonIgnoreProperties 特定属性反序列化属性仅存在于 JSON 中

转载 作者:搜寻专家 更新时间:2023-11-01 02:57:08 24 4
gpt4 key购买 nike

我偶然发现了一些添加了 JsonIgnoreProperties 的代码到类中不存在但存在于 JSON 中的属性,例如:

@JsonIgnoreProperties({"ignoreprop"})
public class VO {
public String prop;
}

JSON

{ "prop":"1", "ignoreprop":"9999"}

我想知道忽略属性在性能方面是否有任何优势,还是只是冗余代码?

Annotation that can be used to either suppress serialization of properties (during serialization), or ignore processing of JSON properties read (during deserialization).

编辑

忽略所有特定属性是否有优势(有@JsonIgnoreProperties(ignoreUnknown=true))?

最佳答案

I wonder if ignoring properties has any advantage

是的,它被大量用于服务的前向兼容性。假设您有服务 A 和 B。目前 A 使用一些 JSON 对象向 B 发送请求。
现在您想要支持 JSON 中的新属性。如果你有这个特性,你就可以让 A 在 B 知道如何处理之前开始发送新的属性。 解耦这两个服务的开发过程。

ignoring specific property over all

这种情况确实有一些小的性能优势。首先,它不会尝试解析这个可以是简单字符串或复杂对象/数组的属性。其次,它可以帮助您避免处理异常。认为以下所有都是有效的调用,你只关心 prop:

{ "prop":"1", "ignoreprop":"9999"}

{ "prop":"1", "ignoreprop":{ "a": { "key": "value", "foo": false }}}

{ "prop":"1", "ignoreprop":[1,2,3,4,5,6..... 1000000]}

关于java - 使用 JsonIgnoreProperties 特定属性反序列化属性仅存在于 JSON 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53389302/

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