Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upJson.Decode.float chokes on Infinity #496
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
Jan 31, 2016
Contributor
Related: https://github.com/elm-lang/core/pull/342.
Also, what about NaN?
|
Related: https://github.com/elm-lang/core/pull/342. Also, what about NaN? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
lukewestby
Aug 17, 2016
Member
A quick note about JSON.parse given that JS is Elm's only backend right now:
The algorithm used in JSON.parse expects that incoming text conform to the definition of JSON in ECMA-404 which explicitly disallows non-digit number values, including Infinity and NaN.
One option to get around this using a Json.Decode.Decoder would be to write a decoder which attempts to parse a field as a Float, and then falls back to trying to find "Infinity" as a String, returning 1/0 as the decoder result. Such a decoder might be a good addition to elm-community/json-extra.
|
A quick note about The algorithm used in One option to get around this using a |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Aug 17, 2016
Member
I like @lukewestby's way of handling this.
Fundamentally, using JSON.parse will trigger the error before any decoders have a chance to run, so I'm not sure what can actually be fixed though.
Makes sense to continue discussion in json-extra for now though!
|
I like @lukewestby's way of handling this. Fundamentally, using Makes sense to continue discussion in |
philderbeast commentedJan 31, 2016
There's a long discussion of whether JSON does/should support infinity here ...
http://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript
Elm does have the isInfinite function. I'm using FsPickler and it converts F# infinity to "Infinity" when encoding to JSON so that's one JSON encoder that includes infinity.