The effective way how to handle the Json type data

1. Cofiguration of the json environment.

build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13

dependencies {

...

compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8'

...

}

We have to import the libraries for handling the json data effectively. and The above are the code to add the dependencies for json handling.

1
2
3
4
5
6
7
8
9
10
11

import lombok.Data;
import java.util.List;

@Data
public class PersonVO {
String name;
int age;
List<String> friends;
}

We need a some VO object to know the way how to handle the Json either. so The PersonVO Object is for that.

2. Object -> Json String

JSON Simple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

// JSONObject -> JSON String.

JSONArray jsonArray = new JSONArray();
jsonArray.add("Hwanghyun");
jsonArray.add("Seungyeun");
jsonArray.add("Haein");

JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "kidongyun");
jsonObject.put("age", 20);
jsonObject.put("friends", jsonArray);

return jsonObject.toJSONString();

It’s the way how you convert the type of json data to String using JSON Simple library. But as you can see, this library should use the JSONArray for the list type. and use the JSONObject for converting the Json data. importantly, This object is not a common thing. so It’s too difficult to transform to another common object when you use this library. so Eventually this library isn’t useful than the below. because This library doesn’t support the function of VO Object mapping directly. And This is more slower than the Gson or the Jackson library. So I really don’t recommend to use this library.

Gson

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// Map -> JSON String.

Map<String, Object> personMap = new HashMap<>();
List<String> friendList = new ArrayList<>();

friendList.add("HwangHyeun");
friendList.add("Seungyeun");
friendList.add("JuKyu");

personMap.put("name", "Kidong");
personMap.put("age", 26);
personMap.put("friends", friendList);

Gson gson = new Gson();

return gson.toJson(personMap);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

// VO -> JSON String.

List<String> friendList = new ArrayList<>();

friendList.add("HwangHyeun");
friendList.add("Seungyeun");
friendList.add("JuKyu");

PersonVO personVO = new PersonVO();

personVO.setName("kidong");
personVO.setAge(26);
personVO.setFriends(friendList);

Gson gson = new Gson();

return gson.toJson(personVO);

The main advantage of this library is you can use the common type of Java like List<> or Map<> for converting the Json data. and This library is really small, light compared with another Json libraries. You can use the VO type object and It’s really simple to use the library.

Jackson

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

// Map -> JSON String.

Map<String, Object> personMap = new HashMap<>();
List<String> friendList = new ArrayList<>();

friendList.add("HwangHyeun");
friendList.add("Seungyeun");
friendList.add("JuKyu");

personMap.put("name", "Kidong");
personMap.put("age", 26);
personMap.put("friends", friendList);

ObjectMapper mapper = new ObjectMapper();

try {
return mapper.writeValueAsString(personMap);
} catch(JsonProcessingException e) {
e.printStackTrace();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// VO -> JSON String.

List<String> friendList = new ArrayList<>();

friendList.add("HwangHyeun");
friendList.add("Seungyeun");
friendList.add("JuKyu");

PersonVO personVO = new PersonVO();

personVO.setName("kidong");
personVO.setAge(26);
personVO.setFriends(friendList);

try {
return mapper.writeValueAsString(personVO);
} catch(JsonProcessingException e) {
e.printStackTrace();
}

3. JSON String -> Object

Gson

1
2
3
4
5
6
7
8
9
10

// JSON String -> Map

String json = {"name":"Kidong","age":26,"friends":["HwangHyeun","Seungyeun","JuKyu"]};

Map<String, Object> map = new HashMap<>();
map = gson.fromJson(json, map.getClass());

return map;

1
2
3
4
5
6
7
8
9
10

// JSON String -> VO

String json = {"name":"Kidong","age":26,"friends":["HwangHyeun","Seungyeun","JuKyu"]};

PersonVO personVO = new PersonVO();
personVO = gson.fromJson(json, PersonVO.class);

return personVO;

Jackson

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

// JSON String -> Map

String json = {"name":"Kidong","age":26,"friends":["HwangHyeun","Seungyeun","JuKyu"]};

ObjectMapper mapper = new ObjectMapper();

try {
Map<String, Object> map = mapper.readValue(json, Map.class);
} catch(IOException e) {
e.printStackTrace();
}

return map;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

// JSON String -> VO

String json = {"name":"Kidong","age":26,"friends":["HwangHyeun","Seungyeun","JuKyu"]};

ObjectMapper mapper = new ObjectMapper();

try {
PersonVO personVo = new PersonVO();
personVO = mapper.readValue(json, PersonVO.class);
} catch(IOException e) {
e.printStackTrace();
}

return personVO;

Gson and Jackson are really similar things. So You can think the differences between these two libraries. Actually The usage of the two is almostly same. but You have to know whether is more effectively as the many situation. Jackson library is more heavier than Gson. and If you have to parse a lot of the Json data then It’s more better idea to choose this library then Gson. Jackson is more powerful, flexible and is known to many developer. but It’s not small thing. If you need to parse just a little Json data then I recommend to use the Gson library. in this case, It’s more faster than the Jackson.

Jackson with Generic Type

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
45

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.stereotype.Component;

@Getter
@Setter
@ToString
@Component
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response<T> {
private int errorCode;
private String errorDesc;
private T data;

public static class Builder<T> {
private final Response<T> response;

public Builder() {
this.response = new Response<>();
}

public Builder<T> errorCode(int val) {
this.response.errorCode = val;
return this;
}

public Builder<T> errorDesc(String val) {
this.response.errorDesc = val;
return this;
}

public Builder<T> data(T val) {
this.response.data = val;
return this;
}

public Response<T> build() {
return response;
}
}
}

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

import com.sun.istack.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@ToString
public class PenaltyRes {
@NotNull
private double totalPenalty;

private List<PenaltyTicket> penaltyTickets;

public static class Builder {
private final PenaltyRes penaltyRes;

public Builder() {
this.penaltyRes = new PenaltyRes();
this.penaltyRes.penaltyTickets = new ArrayList<>();
}

public Builder totalPenalty(double val) {
this.penaltyRes.totalPenalty = val;
return this;
}

public Builder penaltyTickets(List<PenaltyTicket> val) {
this.penaltyRes.penaltyTickets = val;
return this;
}

public PenaltyRes build() {
return this.penaltyRes;
}
}
}

1
2
3

Response<PenaltyRes> res = new ObjectMapper().readValue(result, new TypeReference<Response<PenaltyRes>>(){});

You should use th TypeReference Object when you want to parse the object including the generic type.

Share