Jackson @JsonCreator example – Deserialize with @JsonCreator

In this tutorial, we’re gonna look at ways to deserialize a Json string using @JsonCreator .

I. Maven Dependency

    
        com.fasterxml.jackson.core
        jackson-databind
        2.9.4
    

II. @JsonCreator

@JsonProperty is used to indicate the property name in JSON. This annotation can be used on a contructor or a factory method.

1. On Contructor

Customer class:

package com.javasampleapproach.jackson.creator;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Customer {
    private String id;
    private String name;
    private String address;

    public Customer() {
    }

    @JsonCreator
    public Customer(
            @JsonProperty("id") String id, 
            @JsonProperty("fullname") String name,
            @JsonProperty("location") String address) {
        System.out.println("run constructor...");
        this.id = id;
        this.name = name;
        this.address = address;
    }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
    }
}

App class for testing:

package com.javasampleapproach.jackson.creator;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class App {

    public static void main(String[] args) throws IOException {

        ObjectMapper om = new ObjectMapper();

        String jsonString = "{\"id\":\"1\", \"fullname\":\"Adam Johnson\", \"location\":\"Texas\"}";
        System.out.println("input json: " + jsonString);

        Customer customer = om.readValue(jsonString, Customer.class);
        System.out.println("print: " + customer);
    }
}

>> Result:

input json: {"id":"1", "fullname":"Adam Johnson", "location":"Texas"}
run constructor...
print: Customer [id=1, name=Adam Johnson, address=Texas]
2. On Factory Method

Customer class:

package com.javasampleapproach.jackson.creator;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Customer {
    private String id;
    private String name;
    private String address;

    public Customer() {
    }
    
    @JsonCreator
    public static Customer createCustomer(
            @JsonProperty("id") String id, 
            @JsonProperty("fullname") String name,
            @JsonProperty("location") String address) {
        System.out.println("run factory method...");
        Customer customer = new Customer();
        customer.id = id;
        customer.name = name;
        customer.address = address;
        return customer;
    }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
    }
}

App class for testing:

package com.javasampleapproach.jackson.creator;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class App {

    public static void main(String[] args) throws IOException {

        ObjectMapper om = new ObjectMapper();

        String jsonString = "{\"id\":\"1\", \"fullname\":\"Adam Johnson\", \"location\":\"Texas\"}";
        System.out.println("input json: " + jsonString);

        Customer customer = om.readValue(jsonString, Customer.class);
        System.out.println("print: " + customer);
    }
}

>> Result:

input json: {"id":"1", "fullname":"Adam Johnson", "location":"Texas"}
run factory method...
print: Customer [id=1, name=Adam Johnson, address=Texas]


By grokonez | February 7, 2018.


Related Posts


1 thought on “Jackson @JsonCreator example – Deserialize with @JsonCreator”

Got Something To Say:

Your email address will not be published. Required fields are marked *

*