0

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 JsonDeserializers and TypeAdapters, 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

humkins
  • 9,635
  • 11
  • 57
  • 75
  • It's worth pointing out that the JSON format itself doesn't have an integer type. The primitive number type in ECMAScript (and thus in JSON which is a subset of its syntax) is an IEEE-754 double-precision floating point number. So in effect all numeric values in JSON *are* `Double`s. – Daniel Pryden Apr 24 '16 at 14:43
  • OK. I can use doubles from deserialized code, but then what if I need to serialize it again to JSON? Object ids represented as doubles (with a dot and zero) won't work. – humkins Apr 24 '16 at 14:47
  • I don't know what you're asking. A `double` value of `42.0` **is** a `double` value of `42` (and `42.00`, etc.). If you care about the string representation then you need to be serializing these things as strings in the first place, not numbers. – Daniel Pryden Apr 24 '16 at 14:55
  • @gumkins http://stackoverflow.com/a/36529534/1032167 you can probably try simething like this – varren Apr 24 '16 at 15:36

0 Answers0