-
Notifications
You must be signed in to change notification settings - Fork 760
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
Moshi incorrectly deserializes an integer into a double-value #192
Comments
JSON only has numbers. Not integers or doubles. And since numbers can have decimals they are always represented as doubles in Java. |
Yep! And if you really want, you can get a
|
The pure JSON does not distinguish between integers and doubles, correct. But I see a subtle difference in the notation that can be used to separate integers from doubles. And converting from Java to JSON with moshi this is currently already implemented:
double: 1234.567 -> 1234.567 So an integer can be identified by a missing decimal point. Otherwise the 2nd and 3rd line would have to be converted the same. So the conversion back would be: If it is a number and there is no decimal point convert to long otherwise to double. The problem that I try to solve is to synchronize abitrary datatables between systems with JSON. So I know that the names of the fields are strings, but I know nothing about the types of the fields. And as integers can not always be correctly mapped to doubles I have to distinguish between those types. At the moment I don't know how to write an adapter that returns more than one type. So I would have to use Object as return type. But what would be the input type? (I refer to the examples in the documentation with the @FromJSON annotations) |
You can read numbers in Moshi as strings and decode to your preferred format using your preferred rules. |
Maybe I did not get your point. |
I've a scenario where I cannot enforce a type so, things like JsonAdapter<Map<String, Integer>> adapter = ... are not a viable option. Would be possible to handle non-floating point numbers as int, long or BigInteger as Jackson does? |
In Moshi 1.6.0, you will be able to do this. https://github.com/square/moshi/pull/316/files#diff-377a789dd290133f2491a9d6b5d3bad8R221 |
I am deserializing a map with name/value pairs where the values can have any value-type. So no class with members of a defined type can be given to deserialize into.
I can deserialize into a Map<String,Object> (the only solution), but then all numbers are deserialized into doubles. The underlying problem is that integers are wrongly convertet to doubles.
A simple call to
demonstrates this. Here a double is returned, whereas the argument describes an integer.
The text was updated successfully, but these errors were encountered: