Close

Jackson JSON - Using @JsonIgnoreType to ignore whole class

[Updated: Apr 25, 2018, Created: Apr 24, 2018]

The annotation @JsonIgnoreType can be used to ignore the whole class, i.e. to ignore all properties of that class. However, that only applies to the places where that type is used as property.

Examples

Using @JsonIgnoreType

@JsonIgnoreType
public class Address {
  private String street;
  private String city;
    .............
}
public class Employee {
  private String name;
  private String dept;
  private Address address;
    .............
}

In above example, serializing Employee object to JSON (and vice versa) will ignore the 'address' property.

Serializing/deserializing To JSON

public class ExampleMain {
    public static void main(String[] args) throws IOException {
        Address address = new Address();
        address.setStreet("421 Sun Ave");
        address.setCity("Yellow Town");
        Employee employee = new Employee();
        employee.setName("Trish");
        employee.setDept("Admin");
        employee.setAddress(address);
        System.out.println("-- before conversion --");
        System.out.println(employee);

        //convert to json
        System.out.println("-- to json --");
        String jsonString = toJson(employee);
        System.out.println(jsonString);
        //convert to object;
        System.out.println("-- to object --");
        Employee e = toEmployee(jsonString);
        System.out.println(e);

        //address will be ignore here too
        String s = "{\"name\":\"Trish\",\"dept\":\"Admin\","
                + "\"address\":{\"street\":\"421 Sun Ave\",\"city\":\"Yellow Town\"}}";
        System.out.println("raw json: "+s);
        System.out.println("-- to object --");
        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);
    }
}
-- before conversion --
Employee{name='Trish', dept='Admin', address=Address{street='421 Sun Ave', city='Yellow Town'}}
-- to json --
{"name":"Trish","dept":"Admin"}
-- to object --
Employee{name='Trish', dept='Admin', address=null}
raw json: {"name":"Trish","dept":"Admin","address":{"street":"421 Sun Ave","city":"Yellow Town"}}
-- to object --
Employee{name='Trish', dept='Admin', address=null}

Serializing/deserializing of Address object itself can be done without any property being ignored:

public class ExampleMain2 {
    public static void main(String[] args) throws IOException {
        Address address = new Address();
        address.setStreet("421 Sun Ave");
        address.setCity("Yellow Town");

        System.out.println("-- before conversion --");
        System.out.println(address);

        //convert to json
        System.out.println("-- to json --");
        String jsonString = toJson(address);
        System.out.println(jsonString);
        //convert to object;
        System.out.println("-- to object --");
        Address a = toAddress(jsonString);
        System.out.println(a);
    }

    private static Address toAddress(String jsonData) throws IOException {
        ObjectMapper om = new ObjectMapper();
        return om.readValue(jsonData, Address.class);
    }

    private static String toJson(Address address) throws IOException {
        ObjectMapper om = new ObjectMapper();
        return om.writeValueAsString(address);
    }
}
-- before conversion --
Address{street='421 Sun Ave', city='Yellow Town'}
-- to json --
{"street":"421 Sun Ave","city":"Yellow Town"}
-- to object --
Address{street='421 Sun Ave', city='Yellow Town'}

Example Project

Dependencies and Technologies Used:

  • jackson-databind 2.9.5: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 9
  • Maven 3.3.9

@JsonCreator Examples Select All Download
  • jackson-json-ignore-type-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Address.java

    See Also