HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaCritical

Ignoring new fields on JSON objects using Jackson

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
ignoringjacksonusingobjectsfieldsnewjson

Problem

I'm using Jackson JSON library to convert some JSON objects to POJO classes on an android application. The problem is, the JSON objects might change and have new fields added while the application is published, but currently it will break even when a simple String field is added, which can safely be ignored.

Is there any way to tell Jackson to ignore newly added fields? (e.g. non-existing on the POJO objects)? A global ignore would be great.

Solution

Jackson provides an annotation that can be used on class level (JsonIgnoreProperties).

Add the following to the top of your class (not to individual methods):

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {
    ...
}


Depending on the jackson version you are using you would have to use a different import in the current version it is:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


in older versions it has been:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

Code Snippets

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {
    ...
}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

Context

Stack Overflow Q#5455014, score: 1070

Revisions (0)

No revisions yet.