在 Java 中的 Jackson JSON 反序列化期间忽略缺少的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20578846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:54:08  来源:igfitidea点击:

Ignore missing properties during Hymanson JSON deserialization in Java

javajsonserializationHymanson

提问by user379151

In the example

在示例中

class Person {
   String name;
   int age;
}

If the JSON object has a missing property 'age',

如果 JSON 对象缺少属性“年龄”,

{
  name : John
}

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

it throws a JsonMappingExceptionsaying it cannot deserialize. Is there an annotationto ignore missing fields during deserialization?

它抛出一个JsonMappingException说法,它不能反序列化。反序列化过程中是否有注释可以忽略缺失的字段?

Thanks

谢谢

采纳答案by pedorro

I think what you want is

我想你想要的是

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

that's the Hymanson 1.x way. I think there's a new way in 2.x. Something like

这就是 Hymanson 1.x 的方式。我认为 2.x 中有一种新方法。就像是

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

These will tell Hymanson to only serialize values that are not null, and don't complain when deserializing a missing value. I think it will just set it to the Java default.

这些将告诉 Hymanson 仅序列化非空值,并且在反序列化缺失值时不要抱怨。我认为它只会将其设置为 Java 默认值。

回答by drembert

I think you would want to use the @JsonIgnore annotation: https://fasterxml.github.io/Hymanson-annotations/javadoc/2.2.0/com/fasterxml/Hymanson/annotation/JsonIgnore.html

我想你会想要使用@JsonIgnore 注释:https://fasterxml.github.io/Hymanson-annotations/javadoc/2.2.0/com/fasterxml/Hymanson/annotation/JsonIgnore.html

回答by Gustavo Matias

@JsonIgnoreProperties(ignoreUnknown = true)on the class level worked for me.

@JsonIgnoreProperties(ignoreUnknown = true)在课堂上为我工作。

回答by F?rat Kü?üK

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

回答by Moisés

Modern versions (2.9.6) of Hymanson libraries will ignore missing creator properties by default. However, if the ObjectMapper configuration is set to:

默认情况下,Hyman逊库的现代版本 (2.9.6) 将忽略缺少的创建者属性。但是,如果 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.

如果缺少其中一个属性,您将收到反序列化错误。

回答by mbonness

You could also change your class to use an Integer instead of an int, in which case Hymanson will handle null/missing "age" values properly. Just adding this answer for those looking for an alternative that doesn't involve using annotations.

您还可以更改您的类以使用整数而不是整数,在这种情况下,Hyman逊将正确处理空/缺失的“年龄”值。只需为那些寻找不涉及使用注释的替代方案的人添加这个答案。

class Person {
   String name;
   Integer age;
}