question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A floating point number with all zeros after the point cannot be deserialized as Double or Float

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
zigzagocommented, Oct 13, 2020
0reactions
lamba92commented, Oct 14, 2020

Turns out it was the IntelliJ MongoDB console/driver that converted the item to INT32 when inserting it. When using KMongo to insert the item it is correctly stored as Double.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found