简体   繁体   中英

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',

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

it throws a JsonMappingException saying it cannot deserialize. 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. I think there's a new way in 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. I think it will just set it to the Java default.

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. However, if the ObjectMapper configuration is set to:

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 . To ignore the unknown property, the object mapper configuration needs to be configured as below

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 . 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.

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',

{
  name : John
}

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

it throws a JsonMappingException saying it cannot deserialize. Is there an annotation to ignore missing fields during deserialization?

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM