January 12, 2017

Jackson Standard JSON library for Java

Introduction

Jackson is build up by the following modules:

  • Streaming: "jackson-core"
  • Annotations: "jackson-annotations"
  • Databind: "jackson-databind" implements data-binding (and object serialization)

Maven

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.5</version>
</dependency>
$ mvn dependency:tree
...
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.5:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.8.5:compile
...

Jackson JSON Parser and Generator

To do JSON parsing and generation you only need one class com.fasterxml.jackson.databind.ObjectMapper.

To parse a json file.

{
    "name": "volvo",
    "weight": 350,
    "active": true,
    "lastUsed": "2017-01-17",
    "properties": {
        "name A": "gold",
        "name B": "silver"
    }
}
public class Vehicle {
    private String name;
    private int weight;
    private boolean active;
    private Date lastUsed;
    private Map<String, String> properties = new HashMap<>();

    public Vehicle() {
    }

    public Vehicle(String name, int weight, boolean active, Date lastUsed, Map<String, String> properties) {
        this.name = name;
        this.weight = weight;
        this.active = active;
        this.lastUsed = lastUsed;
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "Vehicle [name=" + name + ", weight=" + weight + ", active=" + active + ", lastUsed=" + lastUsed
                + ", properties=" + properties + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Date getLastUsed() {
        return lastUsed;
    }

    public void setLastUsed(Date lastUsed) {
        this.lastUsed = lastUsed;
    }

    public Map<String, String> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, String> properties) {
        this.properties = properties;
    }
}
ObjectMapper mapper = new ObjectMapper();

String file = JacksonTest.class.getClassLoader().getResource("Vehicle.json").getFile();
Vehicle vehicle = mapper.readValue(new File(file), Vehicle.class);
System.out.println(vehicle);

And to generate json.

Vehicle newVehicle = new Vehicle("saab", 200, true, new Date(), new HashMap<String, String>() {
    {
        put("Code A", "foo");
        put("Code B", "bar");
        put("Code C", "code");
    }
});
String json = mapper.writeValueAsString(newVehicle);
System.out.println(json);

Different ObjectMapper#readValue(...) and ObjectMapper#writeValue(...)

Different parse methods:

  • com.fasterxml.jackson.databind.ObjectMapper.readValue(File, Class<T>)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(URL, Class<T>)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(String, Class<T>)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(Reader, Class<T>)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(InputStream, Class<T>)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(byte[], Class<T>)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(DataInput, Class<T>)

Different generation methods:

  • com.fasterxml.jackson.databind.ObjectMapper.writeValue(File, Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValue(OutputStream, Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValue(DataOutput, Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValue(Writer, Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes(Object)

Configure ObjectMapper

mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
mapper.setSerializationInclusion(Include.NON_EMPTY);

No comments: