목표는 Collection 인터페이스를 구현하는 클래스를 JSON 배열 대신 JSON 객체로 직렬화하는 것입니다.GSON을 사용하여 JsonArray 대신 JsonObject로 컬렉션을 serialize
가public class CollectionImplementation implements Collection<String> {
private final List<String> _wrappedList = new LinkedList<>();
private String _otherField = "asdf";
@Override
public boolean add(String e) {
return _wrappedList.add(e);
}
@Override
...
}
new Gson().toJson(collectionImplementationInstance)
의 결과이다 : 다른 필드 그리워
["str1","str2",...]
다음 클래스는 simplyfied 예이다. 대신 다음을 얻으려고합니다 :
{"_wrappedList":["str1","str2"],"_otherField":"asdf"}
수동으로 모든 필드를 JSON에 추가하지 않고 방법이 있습니까?
CollectionTypeAdapterFactory가 JSON 배열을 만드는 유형 어댑터를 생성하고 ReflectiveTypeAdapterFactory가 내 목표로 연결되는 유형 어댑터를 생성한다는 것을 알았습니다. 다른 유형의 어댑터에 위임하는 방법에 대해 알고 있지만 ReflectiveTypeAdapterFactory에서 유형 어댑터의 유효한 인스턴스를 얻으려면 어떻게해야합니까? – efzwo