A floating point number with all zeros after the point cannot be deserialized as Double or Float
See original GitHub issueI have a collection where the property price is a kotlin.Float. By chance one document has "price": 2.0" and when inserted in the database, MongoDB shows the value correctly as double but the JSON returned has "price: 2". When deserializing that exact document the runtime throws:
org.bson.BsonInvalidOperationException: readDouble can only be called when CurrentBSONType is DOUBLE, not when CurrentBSONType is INT32.
at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:690)
at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:722)
at org.bson.AbstractBsonReader.readDouble(AbstractBsonReader.java:317)
at com.github.jershell.kbson.FlexibleDecoder.decodeFloat(BsonFlexibleDecoder.kt:101)
at kotlinx.serialization.encoding.AbstractDecoder.decodeFloatElement(AbstractDecoder.kt:53)
at it.belabs.beristo.data.food.Menu$Section$Course$AdditionalIngredient$$serializer.deserialize(Menu.kt)
at it.belabs.beristo.data.food.Menu$Section$Course$AdditionalIngredient$$serializer.deserialize(Menu.kt:36)
Somehow the price of that document has been interpreted by the library as an INT32 of some sort due to the .0 missing from the MongoDB BSON deserialization. I was not able to find any workaround aside from storing price as String and manage that stuff at runtime, which is awful to say the least.
Not sure if it’s a MongoDB Java library issue or a KMongo one tho.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
c# - Json.Net not serializing decimals the same way twice
When JsonTextReader encounters a floating-point value, it parses it to either ... When deserializing directly to a decimal or double member, ...
Read more >How to configure the serialization of floats? | ArduinoJson 6
When you call serializeJson() , ArduinoJson converts each floating point value into a string with a variable number of decimal places. This behavior...
Read more >15. Floating Point Arithmetic: Issues and Limitations — Python ...
Floating -point numbers are represented in computer hardware as base 2 (binary) fractions. For example, the decimal fraction 0.125 has value 1/10 +...
Read more >Chr.Avro Types and conversions
NET type to "array" if any of the following is true: The type is a one-dimensional or ... When special floating-point values are...
Read more >Floating-point numeric types - C# reference - Microsoft Learn
Learn about the built-in C# floating-point types: float, double, and decimal.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Ok but it works also with kotlinx. serialization - see my unit test https://github.com/Litote/kmongo/blob/master/kmongo-core-tests/src/main/kotlin/org/litote/kmongo/issues/Issue232FloatSerialization.kt
Turns out it was the IntelliJ MongoDB console/driver that converted the item to
INT32when inserting it. When using KMongo to insert the item it is correctly stored as Double.