0
我正在關注this教程,以便在Windows Azure Mobile Android中實現自定義序列化器。我試圖使用代碼,但我得到一個E變量的錯誤。我應該如何將這個變量E傳遞給這個類?自定義序列化器GSON
public class CollectionSerializer implements JsonSerializer<Collection>, JsonDeserializer<Collection>{
public JsonElement serialize(Collection collection, Type type,
JsonSerializationContext context) {
JsonArray result = new JsonArray();
for(E item : collection){
result.add(context.serialize(item));
}
return new JsonPrimitive(result.toString());
}
@SuppressWarnings("unchecked")
public Collection deserialize(JsonElement element, Type type,
JsonDeserializationContext context) throws JsonParseException {
JsonArray items = (JsonArray) new JsonParser().parse(element.getAsString());
ParameterizedType deserializationCollection = ((ParameterizedType) type);
Type collectionItemType = deserializationCollection.getActualTypeArguments()[0];
Collection list = null;
try {
list = (Collection)((Class<?>) deserializationCollection.getRawType()).newInstance();
for(JsonElement e : items){
list.add((E)context.deserialize(e, collectionItemType));
}
} catch (InstantiationException e) {
throw new JsonParseException(e);
} catch (IllegalAccessException e) {
throw new JsonParseException(e);
}
return list;
}
}
你得到了什麼錯誤? –
無法解析符號'E' –
當然,您還沒有在方法中聲明任何類型參數。該方法不是通用的。所以'E'不能解決。你爲什麼要強調「E」? –