spray-json icon indicating copy to clipboard operation
spray-json copied to clipboard

Float JSON serialization outputs approxiamte value

Open frozenspider opened this issue 11 years ago • 1 comments

Using BasicFormats.FloatJsonFormat to serialize 1.85f of type Float yields 1.850000023841858 in the resulting JSON. This is caused by the fact that JsNumber has no apply for Float, just for Double. Proposal: add JsNumber.apply(Float).

Worst thing here is that there is no way to override/hide implicit BasicFormats.FloatJsonFormat since it's an object and not a value.

frozenspider avatar Jul 15 '14 07:07 frozenspider

Came across this behaviour as well. The issue appears to be in JsNumber.apply (https://github.com/spray/spray-json/blob/master/src/main/scala/spray/json/JsValue.scala#L97) where the Float should be converted to a BigDecimal using BigDecimal.decimal rather than BigDecimal (see documentation there). Got a PR that adds a apply method for Floats to JsNumber: https://github.com/spray/spray-json/pull/241

lars-n avatar Nov 14 '17 19:11 lars-n