Jackson JSON - Using @JsonIgnore and @JsonIgnoreProperties to ignore properties
阿新 • • 發佈:2018-12-27
|
|
Following example shows how to use @JsonIgnore Example
public class Employee { private String name; private String dept; @JsonIgnore private String address; ............. } public class ExampleMain { public static void main(String[] args) throws IOException { Employee employee = new Employee(); employee.setName("Trish"); employee.setDept("Admin"); employee.setAddress("421 Moon Hill"); //convert to json String jsonString = toJson(employee); System.out.println(jsonString); //convert to object; Employee e = toEmployee(jsonString); System.out.println(e); //address here will be ignore too String s = "{\"name\":\"Trish\",\"dept\":\"Admin\", \"address\":\"xyz Street\"}"; System.out.println("JSON input: "+s); Employee e2 = toEmployee(s); System.out.println(e2); } private static Employee toEmployee(String jsonData) throws IOException { ObjectMapper om = new ObjectMapper(); return om.readValue(jsonData, Employee.class); } private static String toJson(Employee employee) throws IOException { ObjectMapper om = new ObjectMapper(); return om.writeValueAsString(employee); } } {"name":"Trish","dept":"Admin"} Employee{name='Trish', dept='Admin', address='null'} JSON input: {"name":"Trish","dept":"Admin", "address":"xyz Street"} Employee{name='Trish', dept='Admin', address='null'}
@JsonIgnoreProperties ExampleThis annotation can be used to ignore multiple properties with one declaration: @JsonIgnoreProperties({"dept", "address"}) public class Employee2 { private String name; private String dept; private String address; ............. } public class ExampleMain2 { public static void main(String[] args) throws IOException { Employee2 employee = new Employee2(); employee.setName("Trish"); employee.setDept("Admin"); employee.setAddress("421 Moon Hill"); //convert to json String jsonString = toJson(employee); System.out.println(jsonString); //convert to object; Employee2 e = toEmployee(jsonString); System.out.println(e); //address/dept here will be ignored even they are in the source JSON String s = "{\"name\":\"Trish\",\"dept\":\"Admin\", \"address\":\"xyz Street\"}"; System.out.println("JSON input: "+s); Employee2 e2 = toEmployee(s); System.out.println(e2); } private static Employee2 toEmployee(String jsonData) throws IOException { ObjectMapper om = new ObjectMapper(); return om.readValue(jsonData, Employee2.class); } private static String toJson(Employee2 employee) throws IOException { ObjectMapper om = new ObjectMapper(); return om.writeValueAsString(employee); } } {"name":"Trish"} Employee{name='Trish', dept='null', address='null'} JSON input: {"name":"Trish","dept":"Admin", "address":"xyz Street"} Employee{name='Trish', dept='null', address='null'} Ignoring Unknown Properties@JsonIgnoreProperties#ignoreUnknown can be used to ignore a JSON property if is not defined in the POJO: @JsonIgnoreProperties(ignoreUnknown = true) public class Employee3 { private String name; private String dept; private String address; ............. } public class ExampleMain3 { public static void main(String[] args) throws IOException { String jsonString = "{\"name\":\"Trish\",\"phone\":\"111-111-1111\"}"; System.out.println(jsonString); //convert to object; Employee3 e = toEmployee(jsonString); System.out.println(e); } private static Employee3 toEmployee(String jsonData) throws IOException { ObjectMapper om = new ObjectMapper(); return om.readValue(jsonData, Employee3.class); } } {"name":"Trish","phone":"111-111-1111"} Employee{name='Trish', dept='null', address='null'} Without |