기본적으로 가능한 한 적은 두통으로 JSON에 직렬화하려는 일부 Java 객체가 있습니다. 지금 저는 Tomcat, Jersey, Genson을 사용하고 있습니다.Genson은 개체의 중첩 된 필드를 처리 할 수 있습니까?
나는 이런 식으로 뭔가가 Genson으로 (이것은 물론, 장난감 예입니다) 작동하지 않는 것을 발견했습니다
{"set1":["1","11"],"set0":["0"],"set2":["2","222","22"]}
{"mapOfSets":{"empty":false}}
:
는
public class Main {
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.mapOfSets = new HashMap<>();
Set<String> set0 = new HashSet<>();
set0.add("0");
Set<String> set1 = new HashSet<>();
set1.add("1");
set1.add("11");
Set<String> set2 = new HashSet<>();
set2.add("2");
set2.add("22");
set2.add("222");
mc.mapOfSets.put("set0", set0);
mc.mapOfSets.put("set1", set1);
mc.mapOfSets.put("set2", set2);
try {
String json1 = new Genson().serialize(mc.mapOfSets);
System.out.println(json1);
String json = new Genson().serialize(mc);
System.out.println(json);
} catch (TransformationException | IOException e) {
e.printStackTrace();
}
}
}
class MyClass {
public Map<String, Set<String>> mapOfSets;
}
위의 출력은 이것이다
Genson에 대한 멋진 점은 WebContent 폴더에 그냥 놓은 것입니다. Jersey와 번들로 묶인 것이 아니라 추가 구성이 필요 없습니다. 위와 같은 객체를 각 스키마에 대한 일종의 변환기를 작성하지 않고도 JSON에 직렬화하는 방법은 정말 간단합니다. Genson 대신 저지와 함께 사용하는 것이 좋지만 Genson은 나를 실패하지 않았습니다. 단점이 있습니다.
그래서 어떻게 Genson을 올바르게 직렬화하도록 마사지합니까? 또는 이런 종류의 것을 손쉽게 처리하는 라이브러리는 무엇입니까?
감사합니다.
릴리스 완료, 내 편집 참조 – eugen