• 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);
          }
      }
      
    • 특징

      • 장점
      • 단점
        • toJson 시 Parser 객체 생성 필요
        • toString 시 Parser 객체 생성 필요
        • 자체 Json Object 인 JSONPObject / JsonNode 가 있지만 사용하기 어려움
  • 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);
          }
      }
      
    • 특징

      • 장점
        • toString 시 Parser 없이 static method 로 파싱 가능
        • toJson 시 Parser 없이 static method 로 파싱 가능
        • json 객체 생성 시 메소드 체이닝을 위한 fluentPut() 메소드 존재
      • 단점
        • deserialize 기본설정이 null 무시이기 때문에 null 파싱을 위해서는 Feature (JSONWriter / JSONReader) 로 설정을 해주어야 함
  • 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);
          }
      }
      
    • 특징

      • 장점
        • toString 시 Parser 없이 static method 로 파싱 가능
        • json 객체 생성 시 메소드 체이닝 가능
      • 단점
        • toJson 시 Parser 객체 생성 필요
  • 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으로 변환되어 실패하게 됨.

      Untitled

    • 특징

      • 장점
        • toString 시 Parser 없이 static method 로 파싱 가능
      • 단점
        • toJson 시 Parser 객체 생성 필요
        • 파싱 시 다운캐스팅을 해주어야 함
        • 파싱 시 타입이 바뀜
        • json 객체 생성 시 메소드 체이닝 불가