问题遇到的现象和发生背景
gson已经注册了registerTypeAdapter(Object.class, new TypeAdapter(),但是debug的时候没有还是走的依赖包里自带的read方式
Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapter(new TypeToken<Map<String, Object>>() {}.getType(), new TypeAdapter<Object>() {
private final TypeAdapter<Object> delegate = new Gson().getAdapter(Object.class);
@Override
public void write(JsonWriter out, Object value) throws IOException {
delegate.write(out, value);
}
@Override
public Object read(JsonReader in) throws IOException {
JsonToken token = in.peek();
switch (token) {
case BEGIN_ARRAY:
List<Object> list = new ArrayList<Object>();
in.beginArray();
while (in.hasNext()) {
list.add(read(in));
}
in.endArray();
return list;
case BEGIN_OBJECT:
Map<String, Object> map = new LinkedTreeMap<String, Object>();
in.beginObject();
while (in.hasNext()) {
map.put(in.nextName(), read(in));
}
in.endObject();
return map;
case STRING:
return in.nextString();
case NUMBER:
//return in.nextDouble();
String n = in.nextString();
if (n.indexOf('.') != -1) {
return in.nextDouble();
}
return in.nextLong();
case BOOLEAN:
return in.nextBoolean();
case NULL:
in.nextNull();
return null;
default:
throw new IllegalStateException();
}
}
}).create();