There is a JSON string
{"uuid": "a1e55ef2-3a58-40d7-9ca2-49b0c3aa653c","args": {"ids": [5,77,999], type: "item_delete"}}
which I'm trying to deserialize to the following class
class Foo {
String uuid;
Map<String,Object> args;
}
with this code
public static void main(String[] args) {
String commandJson = ...;
Gson gson = new Gson();
Foo foo = gson.fromJson(commandJson, Foo.class);
}
Then, in the debugger I can see that ids
property is represented as ArrayList<Double>
, thus instead of 5,77,999
I can see 5.0,77.0,999.0
.
I tried to customize GsonBuilder with a lot of different JsonDeserializer
s and TypeAdapter
s, for example
GsonBuilder gsonBuilder = new GsonBuilder()
.registerTypeHierarchyAdapter(Double.class, new JsonDeserializer<Integer>() { // also tried Number.class
@Override
public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null; // No implementation in this example as it is not even called
}
});
Gson gson = gsonBuilder.create();
or
class IntegerTypeAdapter extends TypeAdapter<Integer> {
@Override
public void write(JsonWriter out, Integer value) throws IOException {
System.out.println("hello from write");
}
@Override
public Integer read(JsonReader in) throws IOException {
System.out.println("hello from read");
return null; // I didn't implement it as it looks like it is not even called
}
}
public static void main(String[] args) {
GsonBuilder gsonBuilder = new GsonBuilder()
.registerTypeAdapter(Integer.class, new IntegerTypeAdapter());
Gson gson = gsonBuilder.create();
}
registered with registerTypeAdapter
and registerTypeHierarchyAdapter
methods but it looks like their code is not event executed. I tried to print something from inside my TypeAdapter
or JsonDeserializer
code - it is not printed, and code execution is not stopped at breakpoints inside their code.
Gson version: 2.2.4