Java

[Java] JSON 변환 net.sf.json-lib

snail voyager 2023. 8. 18. 01:19
728x90
반응형

gradle

implementation 'net.sf.json-lib:json-lib:2.4:jdk15@jar'
implementation 'commons-lang:commons-lang:2.6'
implementation 'commons-logging:commons-logging:1.2'
implementation 'commons-collections:commons-collections:3.2.2'
implementation 'commons-beanutils:commons-beanutils:1.9.4'
implementation 'net.sf.ezmorph:ezmorph:1.0.6'

Object to Json

User user = new User("Chris", 34);

Car car1 = new Car();
car1.setName("K5");
car1.setCarNumber("1234");
car1.setType("SUV");

Car car2 = new Car();
car2.setName("K7");
car2.setCarNumber("1111");
car2.setType("SEDAN");

List<Car> carsList = Arrays.asList(car1, car2);
user.setCars(carsList);

JSONObject car1Json = JSONObject.fromObject(car1);
System.out.println("## Object -> JSON Car : " + car1Json);
//{"carNumber":"1234","name":"K5","type":"SUV"}

JSONObject userJson = JSONObject.fromObject(user);
System.out.println("## Object -> JSON User : " + userJson);
//{"cars":[{"carNumber":"1234","name":"K5","type":"SUV"},{"carNumber":"1111","name":"K7","type":"SEDAN"}],"name":"Chris","age":34}

String jsonString = userJson.toString();
System.out.println("## JSON String : " + jsonString);
////{"cars":[{"carNumber":"1234","name":"K5","type":"SUV"},{"carNumber":"1111","name":"K7","type":"SEDAN"}],"name":"Chris","age":34}

Text Json to Object

JSONObject object2 = JSONObject.fromObject(jsonString);
System.out.println("## String -> JSONObject : " + object2);
//{"cars":[{"carNumber":"1234","name":"K5","type":"SUV"},{"carNumber":"1111","name":"K7","type":"SEDAN"}],"name":"Chris","age":34}

JSONArray carListJson = object2.getJSONArray("cars");
System.out.println("## JSONArray Cars : " + carListJson);
//[{"carNumber":"1234","name":"K5","type":"SUV"},{"carNumber":"1111","name":"K7","type":"SEDAN"}]

JSONObject carObject = carListJson.getJSONObject(0);    //첫번째 인덱스
System.out.println("## JSONObject Car : " + carObject);
//{"carNumber":"1234","name":"K5","type":"SUV"}

Car carClass = new Car();
carClass.setCarNumber(carObject.getString("carNumber"));
carClass.setName(carObject.getString("name"));
carClass.setType(carObject.getString("type"));
System.out.println("## JSONObject -> Class : " + carClass);
//Car{name='K5', carNumber='1234', type='SUV'}

Car carBean = (Car) JSONObject.toBean(carObject, Car.class);
System.out.println(carBean);
//Car{name='K5', carNumber='1234', type='SUV'}

Json 변환 이슈

  • String 필드 값에 "{}" 으로 입력하면 Json 변환 시 빈 객체로 인식해서 "{}" 가 아닌 {} 으로 생성
  • 동일한 net.sf.json-lib 에서 JSON을 객체로 변환하는건 문제 없지만
  • 다른 라이브러리 Jackson을 사용하여 Json -> Object 로 변환 시 {} 을 객체로 인식하고 String 필드에 파싱 시 에러 발생
JSONObject object = new JSONObject();
object.put("carNumber", "0987");
object.put("name", "{}");       //문자열 "{}"을 빈 객체로 변환
System.out.println("## JSON : " + object);
//{"carNumber":"0987","name":{}}


//Jackson Text Json -> Object
ObjectMapper objectMapper = new ObjectMapper();
try {
    Car jacksonCar = objectMapper.readValue(object.toString(), Car.class);
    log.info(jacksonCar.toString());
} catch (JsonProcessingException e) {
    log.info(e.getMessage(), e);
    //Exception in thread "main" java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
    throw new RuntimeException(e);
}

 

 

https://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/package-tree.html

 

728x90
반응형