Ignoring New Fields on JSON Objects Using Jackson

Ignoring new fields on JSON objects using Jackson

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;

Ignore a nested json field when deserializing object

The solution was to add this line

objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

How to ignore certain fields with Jackson's ObjectMapper.readerForUpdating

I fixed the problem by configuring the ObjectMapper like this (not sure if these are all needed though):

om.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
om.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);

And on the Message class for the properties needed:

@JsonIgnore on the setter (excludes it when parsing to the Java object)

@JsonProperty on the getter (includes it when parsing to the JSON object)

Ignore empty elements from json body using jackson

EDIT

This posts seems to be a duplicate.

EDIT 2

Use @JsonView instead of @JsonFilter

Below is sample code that uses @JsonView to print different views of the same JSON object:

{"subject":"math","marks":"100"}

and

{"subject":"math","marks":"100","student":{"name":"x","class":"8"}}

public class Views {
public static class Filtered {}
public static class All extends Filtered {}
}

public class Student {
private String name;

@JsonProperty("class")
private String clazz;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getClazz() {
return clazz;
}

public void setClazz(String clazz) {
this.clazz = clazz;
}
}

public class Score {
@JsonView(Views.Filtered.class)
private String subject;

@JsonView(Views.Filtered.class)
private String marks;

@JsonView(Views.All.class)
private Student student;

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getMarks() {
return marks;
}

public void setMarks(String marks) {
this.marks = marks;
}

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}
}

public class JsonTest {
public static void main(String[] args) throws JsonProcessingException {
Student student = new Student();
student.setName("x");
student.setClazz("8");

Score score = new Score();
score.setSubject("math");
score.setMarks("100");
score.setStudent(student);

ObjectMapper mapper = new ObjectMapper();

//do not serialize student property
System.out.println(mapper.writerWithView(Views.Filtered.class).writeValueAsString(score));

//also serialize student property
System.out.println(mapper.writeValueAsString(score));
}
}

Ignore fields from Java object dynamically while sending as JSON from Spring MVC

Add the @JsonIgnoreProperties("fieldname") annotation to your POJO.

Or you can use @JsonIgnore before the name of the field you want to ignore while deserializing JSON. Example:

@JsonIgnore
@JsonProperty(value = "user_password")
public String getUserPassword() {
return userPassword;
}

GitHub example



Related Topics



Leave a reply



Submit