Gson (클래스는 그냥 이름과 HashMap 임)과 올바르게 직렬화되지 않는 클래스가 있으므로 HashMap에서 이름과 키, 값 쌍을 인쇄하기 위해 사용자 정의 serializer를 작성했습니다.Gson Custom Serializer가 호출되지 않았습니다.
public JsonElement serialize(SpecificationGroupList sgl, Type typeofT,
JsonSerializationContext context) {
System.out.println("here");
JsonObject ret = new JsonObject();
ret.addProperty("GroupName", sgl.getGroupName());
JsonArray jsonArray = new JsonArray();
ret.add("SpecificationPartList", jsonArray);
for (Entry<String, String> entry : sgl.getSpecificationPairList().entrySet()) {
JsonObject temp = new JsonObject();
temp.addProperty(entry.getKey(), entry.getValue());
jsonArray.add(temp);
}
return ret;
}
가 적절하게 인쇄 얻으려면, 나는 사용자 정의 시리얼을 등록했지만 내가 클래스를 인쇄 할 갈 때, 실제로 시리얼 라이저를 사용하지 않습니다. 나는 serializer 인쇄가 "여기"에 있고 결코 인쇄하지 않기 때문에 알 수 있습니다.
또한 실제로는 전체 개체를 인쇄하고 직렬화하는 것이 예상대로 작동하지 않으며 개체를 직접 인쇄하려고하지도 않습니다.
{
"ItemNumber": "22-148-842",
"NeweggItemNumber": "N82E16822148842",
"Title": "Seagate Savvio 15K.3 ST9300653SS 300GB 15000 RPM 2.5\" SAS 6Gb/s Internal Enterprise Hard Drive -Bare Drive",
"specificationGroupList": []
}
sgl[]
모든 도움을 주시면 감사하겠습니다. 물론
인쇄중인 항목마다 사양 배열이 비어 있지 않은지 확인할 수 있습니다. – Perception