Jackson
dependency
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
Test Code
class JsonTest() {
private final String sampleString = "{\\"name\\":\\"John\\", \\"age\\":30, \\"car\\":null}";
private final Map<String, Object> sampleMap = new HashMap<>();
@BeforeEach
void init() {
sampleMap.put("name", "John");
sampleMap.put("age", 30);
sampleMap.put("car", null);
}
@Test
void JsonSimple() throws org.json.simple.parser.ParseException {
// parse to Map & String
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> stringToMap = objectMapper.readValue(sampleString, Map.class);
String mapToString = objectMapper.writeValueAsString(sampleMap);
System.out.printf("mapToString By Jackson : %s%n", mapToString);
assertEquals(sampleMap, stringToMap);
}
}
특징
Fast Json
dependency
implementation 'com.alibaba:fastjson:2.0.8'
Test Code
class JsonTest() {
private final String sampleString = "{\\"name\\":\\"John\\", \\"age\\":30, \\"car\\":null}";
private final Map<String, Object> sampleMap = new HashMap<>();
@BeforeEach
void init() {
sampleMap.put("name", "John");
sampleMap.put("age", 30);
sampleMap.put("car", null);
}
@Test
void JsonSimple() throws org.json.simple.parser.ParseException {
// parse to Map & String By JSON
Map<String, Object> stringToMap = JSON.parseObject(sampleString);
String mapToString = JSON.toJSONString(sampleMap);
System.out.printf("mapToString By FastJson : %s%n", mapToString);
assertEquals(sampleMap, stringToMap);
// parse to Map & String By JSONObject
Map<String, Object> stringToMap2 = com.alibaba.fastjson.JSONObject.parseObject(sampleString);
Map<String, Object> stringToMap3 = com.alibaba.fastjson2.JSONObject.parseObject(sampleString);
String mapToString2 = com.alibaba.fastjson.JSONObject.toJSONString(sampleMap);
String mapToString3 = com.alibaba.fastjson2.JSONObject.toJSONString(sampleMap);
System.out.printf("mapToString By FastJSONObject1 : %s%n", mapToString2);
System.out.printf("mapToString By FastJSONObject2 : %s%n", mapToString3);
assertEquals(sampleMap, stringToMap2);
assertEquals(sampleMap, stringToMap3);
// make Json
com.alibaba.fastjson2.JSONObject jsonObject = new com.alibaba.fastjson2.JSONObject();
jsonObject.put("name", "John");
jsonObject.put("age", 30);
jsonObject.put("car", null);
com.alibaba.fastjson2.JSONObject jsonObject2 = new com.alibaba.fastjson2.JSONObject()
.fluentPut("name", "John")
.fluentPut("age", 30)
.fluentPut("car", null);
assertEquals(sampleMap, jsonObject);
assertEquals(sampleMap, jsonObject2);
}
}
특징
Json Smart
dependency
implementation 'net.minidev:json-smart:2.4.8'
Test Code
class JsonTest() {
private final String sampleString = "{\\"name\\":\\"John\\", \\"age\\":30, \\"car\\":null}";
private final Map<String, Object> sampleMap = new HashMap<>();
@BeforeEach
void init() {
sampleMap.put("name", "John");
sampleMap.put("age", 30);
sampleMap.put("car", null);
}
@Test
void JsonSimple() throws org.json.simple.parser.ParseException {
// parse to Map & String
String mapToString = net.minidev.json.JSONObject.toJSONString(sampleMap);
net.minidev.json.parser.JSONParser jsonParser = new JSONParser(-1);
Map<String, Object> stringToMap = (Map<String, Object>) jsonParser.parse(sampleString);
System.out.printf("mapToString By JsonSmart : %s%n", mapToString);
assertEquals(sampleMap, stringToMap);
// make Json
net.minidev.json.JSONObject jsonObject = new net.minidev.json.JSONObject()
.appendField("name", "John")
.appendField("age", 30)
.appendField("car", null);
assertEquals(sampleMap, jsonObject);
}
}
특징
Json Simple
dependency
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
Test Code
class JsonTest() {
private final String sampleString = "{\\"name\\":\\"John\\", \\"age\\":30, \\"car\\":null}";
private final Map<String, Object> sampleMap = new HashMap<>();
@BeforeEach
void init() {
sampleMap.put("name", "John");
sampleMap.put("age", 30);
sampleMap.put("car", null);
}
@Test
void JsonSimple() throws org.json.simple.parser.ParseException {
// parse to Map & String
String mapToString = org.json.simple.JSONObject.toJSONString(sampleMap);
String mapToString2 = new org.json.simple.JSONObject(sampleMap).toJSONString();
org.json.simple.parser.JSONParser jsonParser = new org.json.simple.parser.JSONParser();
Map<String, Object> stringToMap = (Map<String, Object>) jsonParser.parse(sampleString);
System.out.printf("mapToString By JsonSimple JSONObject static method : %s%n", mapToString);
System.out.printf("mapToString By JsonSimple JSONObject method : %s%n", mapToString2);
assertEquals(sampleMap, stringToMap);
// make Json
org.json.simple.JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "John");
jsonObject.put("age", 30);
jsonObject.put("car", null);
assertEquals(sampleMap, jsonObject);
}
}
assertEquals(sampleMap, stringToMap)
실패
→ JSONParser 로 parse 시 age: 30의 타입이 Integer가 아닌 Long으로 변환되어 실패하게 됨.
특징