-
Notifications
You must be signed in to change notification settings - Fork 111
JacksonHowToIgnoreUnknown
(aka "How to ignore unknown properties")
When binding JSON data into Java objects, all JSON properties need to map to properties of Java object to bind to. If an unknown property is encountered, an exception is thrown. But sometimes it is necessary to ignore unknown JSON properties when reading content to bind to existing Java objects: either you do not control incoming data (or Java objects), don't know all properties there could be, or you are only interested in subset of properties anyway.
There are multiple ways you can handle such situations.
It is possible to define a 2-argument method to be called whenever otherwise unknown property is encountered, like so:
// note: name does not matter; never auto-detected, need to annotate // (also note that formal argument type #1 must be "String"; second one is usually / "Object", but can be something else -- as long as JSON can be bound to that type) @JsonAnySetter public void handleUnknown(String key, Object value) { // do something: put to a Map; log a warning, whatever }
If so, this method will be called once per unknown property. This is useful if you intend to do something for properties of various names, and do not want to add separate setter methods. A downside is that you have to use common value type (often just Object.class).
Note that you can only have one such method per class; adding annotation for two methods will result in exception when constructing deserializer.
This annotation has been supported since JacksonRelease10 (although earlier javadocs claimed it was not supported)
It is also possible to register a DeserializationProblemHandler (by calling ObjectMapper.getDeserializationConfig().addHandler()). When added, handler's handleUnknownProperty method will be called once for each unknown property.
This method is most useful when you just want to log a warning for unknown properties: that is, when these properties are probably problematic, but should not cause a hard data binding failure. Rather you want to be informed so that input data can be corrected later on.
It is also possible to just get rid of exceptions for any unknown properties. To do this, just disable DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES (see JacksonFeaturesDeserialization for details):
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
This is a good approach if the default behavior should be just to ignore any unknown properties. It will make handling similar to other "laissez-faire" frameworks, such as JAXB.
This feature is available with JacksonRelease12 and later.
JacksonRelease13 adds new annotation, @JsonIgnoreProperties which can be used to annotate classes, to:
- Ignore specified list of properties for the annotated class, or
- Define that any unknown properties for the annotated class are to be ignored (property ignoreUnknown, set to true)
This gives you more control over which properties to ignore and for which types, especially when used with JacksonMixInAnnotations.
Back to JacksonDocumentation
CategoryJackson