Jackson 注释:JsonIgnoreProperties(ignoreUnknown=true) 和 JsonInclude(Include.NON_EMPTY) 的区别 [英] Jackson Annotations: Difference Between JsonIgnoreProperties(ignoreUnknown=true) and JsonInclude(Include.NON_EMPTY)

查看:26
本文介绍了Jackson 注释:JsonIgnoreProperties(ignoreUnknown=true) 和 JsonInclude(Include.NON_EMPTY) 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇 Jackson 注释 @JsonIgnoreProperties(ignoreUnknown=true) 和 @JsonInclude(Include.NON_EMPTY) 在类级别上有区别吗?一个只是另一个的更新版本吗?谢谢!

I'm curious is there a difference between Jackson annotations @JsonIgnoreProperties(ignoreUnknown=true) and @JsonInclude(Include.NON_EMPTY) at the class level? Is one just a newer version of the other? Thanks!

jackson 文档 声明:

ignoreUnknown 定义是否可以忽略的属性反序列化期间任何无法识别的属性.

ignoreUnknown Property that defines whether it is ok to just ignore any unrecognized properties during deserialization.

这和空属性一样吗?

推荐答案

简答:

  1. @JsonIgnoreProperties(ignoreUnknown=true) 仅适用于将 JSON 反序列化为 Java 对象 (POJO).如果您的 POJO 不包含 JSON 包含的某些属性,它们将被忽略并且不会引发错误.
  2. 另一方面 @JsonInclude(Include.NON_EMPTY) 用于将 POJO 序列化为 JSON,它说,跳过以下 POJO 属性:

  1. @JsonIgnoreProperties(ignoreUnknown=true) is applicable at deserialization of JSON to Java object (POJO) only. If your POJO does not contain certain properties that JSON does contain, they are ignored and no error is thrown.
  2. On the other hand @JsonInclude(Include.NON_EMPTY) is used at serialization of POJO to JSON and it says, skip POJO properties that are:

null 或被认为是空的内容不包括在内.的定义空性是特定于数据类型的.

null or what is considered empty are not to be included. Definition of emptiness is data type-specific.

长答案:

@JsonInclude

仅在序列化时使用.它表示如果所讨论的属性(或所有属性)的值等于某个值(nullempty - 无论这意味着什么,或默认值)此属性未序列化.

It is used at serialization time only. It says that if the value of a property (or all properties) in question is equal to a certain value (null, empty - whatever that means, or a default value) this property is not serialized.

没有这个注解,属性值总是被序列化.注释有助于减少传输的属性数量(如果接收端不存在属性默认值,则必须指定该值).

Without this annotation, the property value is always serialized. The annotation helps to reduce the number of transferred properties (Property default value must be specified when it is not present on the receiving side).

示例:

public class Person {
    public String firstName = "Mark";
    public String middleName;
    public String lastName = "Watney";
}

ObjectMapper mapper = new ObjectMapper();
Person p = new Person();
System.out.println(mapper.writeValueAsString(p));

产生以下输出:

{"firstName":"Mark", "middleName":null, "lastName":"Watney"}

但是如果 Person@JsonInclude(Include.NON_EMPTY) 注释,则输出中会省略 middleName 因为它的值为empty""(在本例中为 null):

But if Person is annotated with @JsonInclude(Include.NON_EMPTY), middleName is omitted from the output because its value is "empty" (null in this case):

@JsonInclude(Include.NON_EMPTY)
public static class Person {
    [....]
}

控制台输出为:{"firstName":"Mark", "lastName":"Watney"}

@JsonIgnoreProperties

用于在序列化和反序列化中忽略某些属性而不管其值:

Is used to ignore certain properties in serialization and deserialization regardless of its values:

防止指定字段被序列化或反序列化(即不包含在 JSON 输出中;或者即使包含它们也被设置):@JsonIgnoreProperties({ "internalId", "secretKey" })

to prevent specified fields from being serialized or deserialized (i.e. not include in JSON output; or being set even if they were included): @JsonIgnoreProperties({ "internalId", "secretKey" })

无一例外地忽略 JSON 输入中的任何未知属性:@JsonIgnoreProperties(ignoreUnknown=true)

To ignore any unknown properties in JSON input without exception: @JsonIgnoreProperties(ignoreUnknown=true)

如果 JSON 输入是:

If JSON input is:

{
    "firstName": "Homer",
    "middleName": "Jay",
    "lastName": "Simpson"
}

类是:

public class Person {
    public String firstName;
    public String lastName;
}

反序列化 mapper.readValue(json, Person.class) 会产生 UnrecognizedPropertyException 异常:

Deserialization mapper.readValue(json, Person.class) will produce UnrecognizedPropertyException exception:

线程main"中的异常com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 "middleName" .....

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "middleName" .....

因为属性 middleName 不是 Person 类的一部分.

Because property middleName is not part of the Person class.

但是如果类 Person@JsonIgnoreProperties(ignoreUnknown=true) 注释,则在反序列化时将忽略未知属性(如 middleName)进入POJO.

But if class Person was annotated with @JsonIgnoreProperties(ignoreUnknown=true), unknown properties ( like middleName) would be ignored at deserialization into POJO.

@JsonIgnoreProperties(ignoreUnknown=true)
public class person {
    [...]
}

另一个常见用例是抑制敏感属性的序列化,例如密码:

Another common use case is to suppress serialization of sensitive properties, like for example password:

@JsonIgnoreProperties("password")
public static class User {
    public String login = "simpsonh";
    public String password = "D00nut";
    public String firstName = "Homer";
    public String middleName = "Jay";
    public String lastName = "Simpson";
}

现在,如果您序列化 User 类,则输出中将省略密码:

Now if you serialize User class , password will be omitted from the output:

User u = new User();
System.out.println(mapper.writeValueAsString(u));

控制台输出:

{
    "login":"simpsonh",
    "firstName":"Homer",
    "middleName":"Jay",
    "lastName":"Simpson"
}

这篇关于Jackson 注释:JsonIgnoreProperties(ignoreUnknown=true) 和 JsonInclude(Include.NON_EMPTY) 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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