gson: Double cannot be casted to Integer.
Long cannot be casted to Integer, Integer cannot be casted to Short, etc. Could you fix it in https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/internal/bind/ObjectTypeAdapter.java? When serializing Map<String, Object> and deserializing, it throws Exception. I can’t edit this, because I can’t edit Spigot/Bukkit. If JSON save Integer, it should read Integer, not make Double unnecessarily.
I maked it in issue, because I had to download sources GSON and edit this:.
case NUMBER:
try {
return in.nextInt();
} catch(NumberFormatException exception) {
}
try {
return in.nextLong();
} catch(NumberFormatException exception) {
}
return in.nextDouble();
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 23
Could the convention be that anything missing a
.
is an integer and anything with a.
is a double?Is there a way to override this behavior?
No. Some keys is String, some Double, some Float, some Map. Map is parsed by Bukkit for ItemStack, so I can’t modify it. 7 paź 2015 22:14 “Jesse Wilson” notifications@github.com napisał(a):
I think you want to decode it as a
Map<String, Integer>
. No action to take here.@swankjesse It is definitely not the best. Crucial difference is double is a floating point. Right now gson is forcing any numerical value to have fractional part. There’s no integers support. Which is ridiculous.
I wrote, that serializing and deserializing Object doesn’t work correctly. Try serialize Integer number, then deserialize it as fromJson(…, Object.class). It will return double. Why double, not Integer?
I think that it should convert it default to integer, no double. Integer can be casted to double, but double cannot be casted to Integer.