简体   繁体   English

在 Java Jackson JSON 反序列化过程中忽略丢失的属性

[英]Ignore missing properties during Jackson JSON deserialization in Java

In the example在例子中

class Person {
   String name;
   int age;
}

If the JSON object has a missing property 'age',如果 JSON object 缺少属性“年龄”,

{
    "name": "John"
}
Person person = objectMapper.readValue(jsonFileReader, Person.class);

it throws a JsonMappingException saying it cannot deserialize.它抛出一个JsonMappingException说它不能反序列化。 Is there an annotation to ignore missing fields during deserialization?在反序列化过程中是否有注释来忽略丢失的字段?

I think what you want is我想你想要的是

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Person {
  ...
}

that's the Jackson 1.x way.这就是 Jackson 1.x 的方式。 I think there's a new way in 2.x.我认为 2.x 中有一种新方法。 Something like类似的东西

@JsonInclude(Include.NON_NULL)
public class Person {
  ...
}

These will tell Jackson to only serialize values that are not null, and don't complain when deserializing a missing value.这些将告诉 Jackson 仅序列化非空值,并且在反序列化缺失值时不要抱怨。 I think it will just set it to the Java default.我认为它只会将其设置为 Java 默认值。

Annotation based approach is a better way for ignoring but If needed.基于注释的方法是忽略但如果需要的更好方法。 The manual way of deserialization:手动反序列化方式:

ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Person       person = mapper.readValue(jsonFileReader, Person.class);

Modern versions (2.9.6) of Jackson libraries will ignore missing creator properties by default.默认情况下,杰克逊库的现代版本 (2.9.6) 将忽略缺少的创建者属性。 However, if the ObjectMapper configuration is set to:但是,如果 ObjectMapper 配置设置为:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true);

Then you'll get a deserialization error if one of the properties is missing.如果缺少其中一个属性,您将收到反序列化错误。

In Deserialization, two things may happen.在反序列化中,可能会发生两件事。

we have a new field in JSON String, not in Java Bean , that will be an unknown property for the object mapper .我们在 JSON String 中有一个新字段,而不是在 Java Bean中,这将是object 映射器的未知属性 To ignore the unknown property, the object mapper configuration needs to be configured as below要忽略未知属性,object 映射器配置需要配置如下

ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

If a field is not in JSON String but in Java Bean , then that field will be treated as a missing property by the object mapper .如果某个字段不在 JSON String 中,而是在 Java Bean中,则该字段将被object 映射器视为缺失属性 To not fail on missing property, the object mapper configuration needs to be configured as below but this is the default behavior of Modern versions (2.9.6) of Jackson libraries.为了不因缺少属性而失败,object 映射器配置需要如下配置,但这是 Jackson 库的现代版本 (2.9.6) 的默认行为。

ObjectMapper().configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, false)

You could also change your class to use an Integer instead of an int, in which case Jackson will handle null/missing "age" values properly.您还可以更改您的类以使用整数而不是整数,在这种情况下,杰克逊将正确处理空/缺失的“年龄”值。 Just adding this answer for those looking for an alternative that doesn't involve using annotations.只需为那些寻找不涉及使用注释的替代方案的人添加这个答案。

class Person {
   String name;
   Integer age;
}

如果你用 @JsonProperty(access = JsonProperty.Access.READ_ONLY) 注释年龄,它只会在反序列化过程中被忽略。

In the example在这个例子中

class Person {
   String name;
   int age;
}

If the JSON object has a missing property 'age',如果JSON对象缺少属性“ age”,

{
  name : John
}

Person person = objectMapper.readValue(jsonFileReader, Person.class);

it throws a JsonMappingException saying it cannot deserialize.它抛出一个JsonMappingException说它不能反序列化。 Is there an annotation to ignore missing fields during deserialization?是否有注释可在反序列化期间忽略缺少的字段?

Thanks谢谢

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

相关问题 如何在 jackson 反序列化期间忽略 json 数组的类型信息? - How to ignore type information on json arrays during jackson deserialization? 从JSON反序列化期间如何忽略具有空值的属性 - How to ignore properties with empty values during deserialization from JSON 在 jackson json 反序列化为 Java 中的 JSONObject 类期间忽略键的缺失值 - Ignore missing values of key during jackson json deserialize to JSONObject class in java 杰克逊动态过滤反序列化过程中的属性 - Jackson Dynamic filtering of properties during deserialization 杰克逊在反序列化期间对丢失的财产投掷了NPE - Jackson throws NPE during deserialization on missing property 在 Java Jackson 的反序列化过程中,我可以忽略基于 getter 的只写属性吗? - Can I ignore a getter-based write-only property during deserialization in Java Jackson? jackson 反序列化期间 json 中存在多个别名 - Multiple aliases present in json during jackson deserialization 使用杰克逊处理JSON反序列化中缺少的类 - Handling missing classes in json deserialization using jackson 如何告诉杰克逊在反序列化期间忽略空对象? - How to tell Jackson to ignore empty object during deserialization? 杰克逊反序列化忽略属性不适用于@JsonView - Jackson deserialization ignore properties does not work properly with @JsonView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM