-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Double cannot be casted to Integer. #692
Comments
@swankjesse In our original parser, I tried to convert any number to its smallest form first. So, first see if it fits in short, int and long, then try double. |
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. |
It may be because of routine, which converts all numbers to double. |
Double by default is the best for simplicity. Things like equals() break if we use a mix of different types 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. |
There's int support, as long as you provide the target type. When you ask Gson to decode an object without a target type, it needs a policy and Gson’s policy is to be consistent with JavaScript. |
(Arguing int vs. double is particularly awkward because there's no data loss.) |
Double cannot be casted to Integer. I have Object in main app and plugin
|
@swankjesse Why there's should be data loss? If value has "." ( |
@swankjesse I do understand you stand for simplicity. But treating numeric without floating point as a numeric with floating point has a serious impact on business logic. There's not much difference between Integer and Long from business logic point of view, but difference between Long and Double is huge. ( |
Why are you decoding it as |
Ie. if you want type safety, provide the types! |
But I don't know, what's type is it. I need Object, which can be Integer.
|
@swankjesse I'm deserializing it as a Map (and it is actually a map). |
@swankjesse: I'm afraid you do not understand my problem with Properties. I'm providing the types when serializing it, and gson correctly serializes integers as integers. But when I'm deserializing my Properties back (let's say, from json received from server), all integers are becoming doubles, and that's breaking a lot of things. I have to check every entry which I expect to be integer and convert it from double to integer. This particular method we're talking about is meant to deserialize maps, and it is definitely broken, as it considers any number in map being a floating point. Using most specific type instead of most generic type is more correct, imho. |
Could the convention be that anything missing a |
@oleersoy: yup, I'm using exactly that logic in my custom adapter. |
I think you want to decode it as a |
No. Some keys is String, some Double, some Float, some Map. Map is parsed
|
Values* |
It may be check while converting string to number: if (number . contains( "." ) || number . contains( "e" ) || number . contains( "E" )) { |
Or:
|
Is there a way to override this behavior? |
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:.
The text was updated successfully, but these errors were encountered: