Jackson JSON - Using @JsonCreator to define constructors or factory methods for deserialization.

[Updated: Jan 12, 2018, Created: Jan 12, 2018]

For deserialization (reading) of JSON to POJOs, we can use @JsonCreator annotation to define constructors and factory methods for instantiating new instances of the associated classes. This can be particularly useful for immutable objects.

Examples

Using @JsonCreator on Constructor

public class JsonCreatorConstructorExample {
    public static void main(String[] args) throws IOException {
        System.out.println("-- writing --");
        Employee employee = new Employee("Trish", "Admin");
        //convert to json
        String jsonString = toJson(employee);
        System.out.println(jsonString);
        System.out.println("-- reading --");
        Employee e = toEmployee(jsonString);
        System.out.println(e);
    }

    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);
    }

    private static class Employee {
        private String name;
        private String dept;

        @JsonCreator
        public Employee(@JsonProperty("name") String name, @JsonProperty("dept") String dept) {
            System.out.println("'constructor invoked'");
            this.name = name;
            this.dept = dept;
        }

        public String getName() {
            return name;
        }

        public String getDept() {
            return dept;
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", dept='" + dept + '\'' +
                    '}';
        }
    }
}
-- writing --
'constructor invoked'
{"name":"Trish","dept":"Admin"}
-- reading --
'constructor invoked'
Employee{name='Trish', dept='Admin'}

Using @JsonCreator on Factory method

public class JsonCreatorFactoryMethodExample {
    public static void main(String[] args) throws IOException {
        System.out.println("-- writing --");
        Employee employee = Employee.createEmployee("Trish", "Admin");
        //convert to json
        String jsonString = toJson(employee);
        System.out.println(jsonString);

        System.out.println("-- reading --");
        Employee e = toEmployee(jsonString);
        System.out.println(e);
    }

    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);
    }

    private static class Employee {
        private String name;
        private String dept;

        @JsonCreator
        public static Employee createEmployee(@JsonProperty("name") String name,
                                              @JsonProperty("dept") String dept) {
            System.out.println("'factory method invoked'");
            Employee employee = new Employee();
            employee.name = name;
            employee.dept = dept;
            return employee;
        }

        public String getName() {
            return name;
        }

        public String getDept() {
            return dept;
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", dept='" + dept + '\'' +
                    '}';
        }
    }
}
-- writing --
'factory method invoked'
{"name":"Trish","dept":"Admin"}
-- reading --
'factory method invoked'
Employee{name='Trish', dept='Admin'}

Example Project

Dependencies and Technologies Used :

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

@JsonCreator Examples Select All Download
  • jackson-creator-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also