Gson gson = new Gson();
System.out.println(gson.fromJson("1", Object.class)); //output:1.0
System.out.println(gson.fromJson("1", String.class)); //output:1
System.out.println(gson.fromJson("1", Integer.class)); //output:1
我正在尝试自定义一个反序列化器来修复它,但仍然无法正常工作:
Gson gson = new GsonBuilder().registerTypeAdapter(Object.class,new JsonDeserializer<Object>() {
@Override
public Object deserialize(JsonElement json, Type typeOfT,JsonDeserializationContext context)throws JsonParseException {
return json.getAsInt();
}
}).create();
System.out.println(gson.fromJson("1", Object.class)); //still 1.0
我在这里做错了吗?