on
Generating Json using Jackson
JSON is the number one format when it comes to exchanging data over the Web. In this tutorial, we will go through how to generate JSON content, using Jackson library.
First of all, there are two different ways of generating JSON with Jackson:
- by mapping an object
- Using a stream
Let’s try out both of them.
The maven dependency for Jackson is :
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
We are using an object such as :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class Concept {
private String conceptName;
private int score;
private List conceptOwnerList;
private Map conceptDataDictionary;
public Concept(String name){
this.conceptName = name;
conceptOwnerList = new ArrayList();
conceptDataDictionary = new HashMap();
}
public String getConceptName() {
return conceptName;
}
public void setConceptName(String conceptName) {
this.conceptName = conceptName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public List getConceptOwner() {
return conceptOwnerList;
}
public void addConceptOwner(String conceptOwner) {
conceptOwnerList.add(conceptOwner);
}
public Map getConceptDataDictionary() {
return conceptDataDictionary;
}
public void addConceptToDataDictionary(String entry, String definition) {
conceptDataDictionary.put(entry, definition);
}
}
Let’s populate our object with some sample data, and serialize it to JSON using ObjectMapper.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Concept concept = new Concept("MyConcept");
concept.setScore(50);
concept.addConceptOwner("tester1");
concept.addConceptOwner("tester2");
concept.addConceptOwner("tester3");
concept.addConceptToDataDictionary("Peniciline", "group of antibiotics");
concept.addConceptToDataDictionary("Amoxiline", "antibiotic");
concept.addConceptToDataDictionary("Morphine", "pain killer");
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
String json = mapper.writeValueAsString(concept);
System.out.println(json);
Result:
{
"conceptName": "MyConcept",
"score": 50,
"conceptOwner": ["tester1", "tester2", "tester3"],
"Peniciline": "group of antibiotics",
"Amoxiline": "antibiotic",
"Morphine": "pain killer"
}
Some useful annotations for object properties:
@JsonAnyGetter: Annotation to add to the getter of a Map. It serializes the content of the map instead of using the map as an object itself.
@JsonIgnore: Ignores a property when serializing.
@JsonProperty: uses “someproperty” as key value instead of the property name.
A JSON stream can be useful when the developper needs to build the content interactively:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(new PrintWriter(System.out));
generator.writeStartObject();
generator.writeFieldName("JsonTest");
generator.writeString("using a stream json generator");
generator.writeFieldName("ArrayKey");
// start an array
generator.writeStartArray();
generator.writeString("object 1");
generator.writeString("object 2");
generator.writeString("object 3");
generator.writeEndArray();
generator.writeEndObject();
generator.close();
Result:
{
"JsonTest": "using a stream json generator",
"ArrayKey": ["object 1", "object 2", "object 3"]
}