json-lib, com.fasterxml.jackson 둘의 차이점을 비교해본다.
별다른 옵션 없는 심플한 상황이다.
json-lib의 경우 Object형의 NULL 변환에 문제가 있어보인다.
[JsonTest .java]
import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.io.IOException; import org.junit.Test; import com.fasterxml.jackson.databind.ObjectMapper; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class JsonTest { @Test public void test_jsonlib_BeanCase() { try { TestBean inBean = new TestBean(); inBean.setA("a Value"); inBean.setB(""); inBean.setC(null); //inBean.setD(); inBean.setInt11(1); //inBean.setInt12(); inBean.setInt21(1); inBean.setInt21(null); inBean.setBool11(true); //inBean.setBool12(); inBean.setBool21(true); inBean.setBool21(null); JSONObject jsonObj = JSONObject.fromObject( JSONSerializer.toJSON(inBean) ); String jsonStr = jsonObj.toString(); System.out.println("jsonStr: " + jsonStr); jsonObj = (JSONObject) JSONObject.fromObject(jsonStr); TestBean outBean = (TestBean) JSONObject.toBean(jsonObj, TestBean.class); System.out.println(outBean); assertEquals(inBean.getA(), outBean.getA()); assertEquals(inBean.getB(), outBean.getB()); //assertEquals(inBean.getC(), outBean.getC()); // 문제 소지 //assertEquals(inBean.getD(), outBean.getD()); // 문제 소지 assertEquals(outBean.getC(), ""); assertEquals(outBean.getD(), ""); assertEquals(inBean.getInt11(), outBean.getInt11()); assertEquals(inBean.getInt12(), outBean.getInt12()); //assertEquals(inBean.getInt21(), outBean.getInt21()); // 문제 소지(아예 변환 안됨) //assertEquals(inBean.getInt22(), outBean.getInt22()); // 문제 소지(아예 변환 안됨) assertEquals(outBean.getInt21(), new Integer(0)); assertEquals(outBean.getInt22(), new Integer(0)); assertEquals(inBean.isBool11(), outBean.isBool11()); assertEquals(inBean.isBool12(), outBean.isBool12()); //assertEquals(inBean.getBool21(), outBean.getBool21()); // 문제 소지(아예 변환 안됨) //assertEquals(inBean.getBool22(), outBean.getBool22()); // 문제 소지(아예 변환 안됨) assertEquals(outBean.getBool21(), new Boolean(false)); assertEquals(outBean.getBool22(), new Boolean(false)); } catch (Exception e) { System.err.println("-- e: " + e); } } @Test public void test_jackson_BeanCase() { try { ObjectMapper jacksonMapper = new ObjectMapper(); TestBean inBean = new TestBean(); inBean.setA("a Value"); inBean.setB(""); inBean.setC(null); //inBean.setD(); inBean.setInt11(1); //inBean.setInt12(); inBean.setInt21(1); inBean.setInt21(null); inBean.setBool11(true); //inBean.setBool12(); inBean.setBool21(true); inBean.setBool21(null); System.out.println(inBean); String jsonStr = jacksonMapper.writeValueAsString(inBean); System.out.println("jsonStr: " + jsonStr); TestBean outBean = jacksonMapper.readValue(jsonStr, TestBean.class); assertEquals(inBean.getA(), outBean.getA()); assertEquals(inBean.getB(), outBean.getB()); assertEquals(inBean.getC(), outBean.getC()); assertEquals(inBean.getD(), outBean.getD()); assertEquals(outBean.getC(), null); assertEquals(outBean.getD(), null); assertEquals(inBean.getInt11(), outBean.getInt11()); assertEquals(inBean.getInt12(), outBean.getInt12()); assertEquals(inBean.getInt21(), outBean.getInt21()); assertEquals(inBean.getInt22(), outBean.getInt22()); assertEquals(inBean.isBool11(), outBean.isBool11()); assertEquals(inBean.isBool12(), outBean.isBool12()); assertEquals(inBean.getBool21(), outBean.getBool21()); assertEquals(inBean.getBool22(), outBean.getBool22()); } catch (IOException e) { System.err.println("-- e: " + e); } } }
[TestBean.java]
import lombok.Getter; import lombok.Setter; @Setter @Getter public class TestBean { String a; String b; String c; String d; int int11; int int12; Integer int21; Integer int22; boolean bool11; boolean bool12; Boolean bool21; Boolean bool22; }
[build.gradle]
sourceCompatibility='1.7' dependencies { //compile group: 'commons-collections', name: 'commons-collections', version: '3.2' //compile 'org.apache.maven.plugins:maven-war-plugin:+' compile 'net.sf.json-lib:json-lib:2.4:jdk15@jar' compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.6.4' compile 'commons-lang:commons-lang:2.6' compile 'commons-logging:commons-logging:1.2' compile 'commons-collections:commons-collections:3.2.2' compile 'commons-beanutils:commons-beanutils:1.9.2' compile 'net.sf.ezmorph:ezmorph:1.0.6' testCompile 'junit:junit:4.11' testCompile 'org.hamcrest:hamcrest-all:1.3' }