You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"myId":7000000000000125407
The JSON is a little bit more complex so a Map is used as the basic data type.
Unfortunately, the value for my given ID will be a different one after parsing since Gson casts to double directly which leads to the following value in my map:
Unfortunately, the JSON is supplied by a 3rd party system and cannot be modified, so I cannot add quotes. A way to enforce Strings for defined types would be great.
The text was updated successfully, but these errors were encountered:
When no data type is specified, gson reads al numbers as doubles (see: com.google.gson.internal.bind.ObjectTypeAdapter:78-79) and there is no way to plug a custom ObjectTypeAdapter since it is designed taht way (see: com.google.gson.internal.bind.ObjectTypeAdapter:198-208)
I think there should be a way to define a custom ObjectTypeAdapter
In the meantime I managed to come with the following workaround.
Define a custom empty class that will be used as a flag for deserialization.
Copy the implementation of ObjectTypeAdpater and modify the deserialization. (I went for bigdecimals)
Register a typeAdapterFactory that instantiates your ObjectTypeAdapter for the flag class in the GsonBuilder creating your gson object.
Deserialize with Object o = gson.fromJson(reader, YourFlagClass.class) (thanks type erasure)
I had to twist the API way too much for something that should be easily achievable.
Hi,
my REST response contains something like
"myId":7000000000000125407
The JSON is a little bit more complex so a Map is used as the basic data type.
Unfortunately, the value for my given ID will be a different one after parsing since Gson casts to double directly which leads to the following value in my map:
(double)7000000000000125407l -> 7.0000000000001249E18
Unfortunately, the JSON is supplied by a 3rd party system and cannot be modified, so I cannot add quotes. A way to enforce Strings for defined types would be great.
The text was updated successfully, but these errors were encountered: