GSON を使用してシリアル化したいと思います。
"starterItems": {
"Appeltaart": 3,
"Soap_50": 3
}
...グアバにImmutableMap
:
private ImmutableMap<String,Integer> starterItems;
通常の GSON マップ解析を使用して、次のように結果の不変コピーを作成するだけだと思いました。
gb.registerTypeAdapter(ImmutableMap.class, new JsonDeserializer<ImmutableMap>() {
@SuppressWarnings("unchecked")
@Override public ImmutableMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return ImmutableMap.copyOf((Map) context.deserialize(json, Map.class));
}
});
しかしさすがにこれは単純すぎる(型情報がない)。エラーが発生します:
com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object {"Appeltaart":3,"Soap_50":3} given the type interface java.util.Map
やりたいことはできますか?