仅在JSON中使用JsonIgnoreProperties特定属性反序列化属性 [英] Use of JsonIgnoreProperties specific property deserialize properties exists only in JSON

查看:1431
本文介绍了仅在JSON中使用JsonIgnoreProperties特定属性反序列化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一些添加将JsonIgnoreProperties 设置为在类中不存在但在JSON中存在的属性,例如:

I stumbled upon some code that adds JsonIgnoreProperties to a property that doesn't exists in class, but exists in JSON, e.g.:

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

JSON

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

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

I wonder if ignoring properties has any advantage(s) performance-wise or is it just redundant code?

可用于抑制属性的序列化(在序列化期间)或忽略对JSON属性读取的处理(在反序列化期间)的注释.

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))?

Is there an advantage(s) ignoring specific property over all (with @JsonIgnoreProperties(ignoreUnknown=true))?

推荐答案

我想知道忽略属性是否有任何优势

I wonder if ignoring properties has any advantage

是的,它在服务中的前向兼容性中被大量使用.假设您有服务A和服务B.当前,服务A使用一些JSON对象将请求发送到服务B.
现在,您想在JSON中支持新属性.如果您具有此功能,则可以让A在B知道如何处理它之前就开始发送新属性. 解耦这两项服务的开发过程.

Yes, it is used a lot for forward-compatibility in services. Let's say you have Services A and B. Currently A sends requests to B with some JSON objects.
Now you want to support a new property in the JSON. If you have this feature you are able to let A start sending the new property before B knows how to handle it. Decoupling the development processes of those two services.

忽略全部

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

This case does have some minor performance advantages. First, it doesn't try to parse this property which can be a simple string or complex object/array. Second, it helps you avoid handling an exception. Think that all the following can be valid calls and you only care about prop:

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

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

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

这篇关于仅在JSON中使用JsonIgnoreProperties特定属性反序列化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆