De-serializing JSON String Using Jackson ObjectMapper


In java serializing and de-serializing JSON is very straight forward using ObjectMapper class of Jackson lib.

For this you need jackson-core-*.*.*.jar, jackson-databind-*.*.*.jar and jackson-annotations-*.*.*.jar libraries. You can add these lib reference using maven build or directly by downloading them to you PC.

Maven entries for jackson lib

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.3.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.3.2</version>
</dependency>

How to desterilize JSON String

Suppose you have following JSON string, double quoted string.

"{"moduleName":"Account","moduleTitle":"Add New Account","moduleDescription":"Create new Account","fields":[{"name":"name","caption":"Enter User name","type":"string","formControl":"text","required":false,"requiredMsg":""},{"name":"password","caption":"Enter password","type":"string","formControl":"password","required":false,"requiredMsg":""}]}"

To de-serialize this you need to write java classes “Field” and “Module” like given below.

package c4c.org.triode516.models;

public class Field{
    private String name;
    private String caption;
    private String type;
    private String formControl;
    private boolean required;
    private String requiredMsg;
    
    public Field()
    {
        
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCaption() {
        return caption;
    }
    public void setCaption(String caption) {
        this.caption = caption;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getFormControl() {
        return formControl;
    }
    public void setFormControl(String formControl) {
        this.formControl = formControl;
    }
    public boolean isRequired() {
        return required;
    }
    public void setRequired(boolean required) {
        this.required = required;
    }
    public String getRequiredMsg() {
        return requiredMsg;
    }
    public void setRequiredMsg(String requiredMsg) {
        this.requiredMsg = requiredMsg;
    }

}

package c4c.org.triode516.models;

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Module {
    private List<Field> fields;

    private String moduleName;
    private String moduleDescription;
    private String moduleTitle;
    public Module() {

    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getModuleDescription() {
        return moduleDescription;
    }

    public void setModuleDescription(String moduleDescription) {
        this.moduleDescription = moduleDescription;
    }

    public List<Field> getFields() {
        return fields;
    }

    public void setFields(List<Field> fields) {
        this.fields = fields;
    }

    @JsonCreator
    public static Module Create(String jsonString) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        Module module = null;
        module = mapper.readValue(jsonString, Module.class);
        return module;
    }

    public String getModuleTitle() {
        return moduleTitle;
    }

    public void setModuleTitle(String moduleTitle) {
        this.moduleTitle = moduleTitle;
    }
}

 

Now Create an instance of ObjectMapper and call readValue method.

ObjectMapper mapper = new ObjectMapper();

Module module = mapper.readValue(data, Module.class);

Done .

But you in case if you are trying for desterilize quoted JSON string then you will get following exception.

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class c4c.org.triode516.models.Module] from String value; no single-String constructor/factory method

To fix this exception you need to give string parameter constructor or one static factory method with single string parameter with attribute @JsonCreator.

@JsonCreator
public static Module Create(String jsonString) throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    Module module = null;
    module = mapper.readValue(jsonString, Module.class);
    return module;
}