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

Json.Decode.float chokes on Infinity #496

Closed
philderbeast opened this Issue Jan 31, 2016 · 3 comments

Comments

Projects
None yet
4 participants
@philderbeast

philderbeast commented Jan 31, 2016

> import Json.Decode exposing (..)
> decodeString float "1.0"
Ok 1 : Result.Result String Float
> decodeString float "Infinity"
Err ("Unexpected token I") : Result.Result String Float

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.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 31, 2016

Contributor

Related: https://github.com/elm-lang/core/pull/342.

Also, what about NaN?

Contributor

jvoigtlaender commented Jan 31, 2016

Related: https://github.com/elm-lang/core/pull/342.

Also, what about NaN?

@lukewestby

This comment has been minimized.

Show comment
Hide comment
@lukewestby

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.

Member

lukewestby commented Aug 17, 2016

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.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

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!

Member

evancz commented Aug 17, 2016

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!

@evancz evancz closed this Aug 17, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment