Skip to content

Allow parsing Nan/Infinity as double value. #1540

Closed

Description

@amosbird

Is your feature request related to a problem? Please describe.
A portion of geometric JSON data contains NaN/Infinity as double values, which currently simdjson fails to parse.

Describe the solution you'd like
Support parsing NaN/Infinity as double value, a subset of relaxed JSON syntax. If performance will be affected, the feature may live under some option.

Describe alternatives you've considered
I cannot think of any alternatives.

Activity

  1. lemire commented on Apr 7, 2021

    @lemire
    Member

    For people reading this, we follow RFC 8259, see https://tools.ietf.org/html/rfc8259

    It states:

    Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.

    Here is the result in Python...

    >>> import json
    >>> json.loads("{\"x\":inf}")
    json.decoder.JSONDecodeError: Expecting value: line 1 column 6 (char 5)
    >>> json.loads("{\"x\":infinity}")
    json.decoder.JSONDecodeError: Expecting value: line 1 column 6 (char 5)
    

    Result in JavaScript:

    ❯ node
    Welcome to Node.js v15.12.0.
    Type ".help" for more information.
    > JSON.parse("{\"x\":1}")
    { x: 1 }
    > JSON.parse("{\"x\":inf}")
    Uncaught SyntaxError: Unexpected token i in JSON at position 5
    > JSON.parse("{\"x\":infinity}")
    Uncaught SyntaxError: Unexpected token i in JSON at position 5
    

    @amosbird Are you willing to implement and test support for the format that you describe knowing that it is not valid JSON and does not follow the specification?

  2. amosbird commented on Apr 7, 2021

    @amosbird
    ContributorAuthor

    Thanks for the detailed explanation. I agree we should stick with the specification.

  3. lemire commented on Apr 7, 2021

    @lemire
    Member

    @amosbird Thanks for your comments.

  4. memetb commented on Dec 26, 2021

    @memetb

    Just a minor point on the above conversation that python's json does in fact support infinity, even though json's RFC is blind to it.

     import json
     json.loads("{\"x\":inf}") # doesn't work
     => json.decoder.JSONDecodeError: Expecting value: line 1 column 6 (char 5)
     json.loads("{\"x\":infinity}") # doesn't work
     => json.decoder.JSONDecodeError: Expecting value: line 1 column 6 (char 5)
     json.loads("{\"x\":Infinity}") # <-- works
     => {'x': inf}
     json.dumps({"x": float("inf")})
     => '{"x": Infinity}'
     json.dumps({"x": float("-inf")})
     => '{"x": -Infinity}'
     json.dumps({"x": float("nan")})
     => '{"x": NaN}'
    

    There is a larger conversation which is not part of simdjson as a library as to whether json should have supported the full IEEE 754 floating point spec, which does in fact have -inf, +inf and NaN's as valid floating point values.

    So my +1 on a request to one day maybe support the option, but I understand that it is a wish list item.

    @lemire: to answer your request here:

    amosbird Are you willing to implement and test support for the format that you describe knowing that it is not valid JSON and does not follow the specification?

    I'm not sure whether that was a rhetorical question, but I would be willing to dedicate time if you can recommend an architectural way to achieve it. Would it be in the form of a configurable token for the parser object? Clearly, this is not a spec driven token, so it is by definition application defined. The above values are what python happens to use. It could alternatively be in the form of a "dialect" enumeration.

  5. jkeiser commented on Dec 27, 2021

    @jkeiser
    Member

    I think we're on board for non standard features, as long as they are somewhat common and can be turned off. Trailing commas and comments come to mind as valuable and reasonably common additions to the spec.

  6. memetb commented on Dec 30, 2021

    @memetb

    I would be ready to tackle this sometime in the new year. What is the desired architectural approach here? Templated mixin? Dialect enum passed to constructor?

  7. lemire commented on Dec 30, 2021

    @lemire
    Member

    Python is a particular case, its behaviour is documented as non-standard compliant:

    The RFC does not permit the representation of infinite or NaN number values. Despite that, by default, this module accepts and outputs Infinity, -Infinity, and NaN as if they were valid JSON number literal values

    It is important to understand that this implies that the output can often not be read by other parsers. So it is an interoperability issue.

    JavaScript:

    > JSON.parse("{\"x\":Infinity}")
    Uncaught SyntaxError: Unexpected token I in JSON at position 5
    > JSON.parse("{\"x\":infinity}")
    Uncaught SyntaxError: Unexpected token i in JSON at position 5
    > JSON.parse("{\"x\":inf}")
    Uncaught SyntaxError: Unexpected token i in JSON at position 5

    In Go:

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    )
    
    type employee struct {
    	X float64 `json:"x"`
    }
    
    func main() {
            var emp employee
            Data := []byte(`{
            "x": Infinity
            }`)
    	err := json.Unmarshal(Data, &emp)
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Printf("emp Struct: %#v\n", emp)
    }

    It fails.


    How this could be used in simdjson is somewhat secondary, but some ground rules can be set:

    1. This should come at no performance (or memory) cost to users who do not need this feature.
    2. It should be well tested.
    3. It should be well documented.
    4. It should not inconvenience users who do not need this feature.
  8. memetb commented on Dec 30, 2021

    @memetb

    Thanks @lemire: I think the 4 ground rules you set out could be "fairly easily" achievable given the very limited scope of the feature. I will have some spare cycles in the near future and will make an attempt.

  9. lemire commented on Dec 30, 2021

    @lemire
    Member

    It does involve adding new atom types to JSON. JSON recognizes null, true, false, numbers, strings. One needs to add entirely new atoms. It is not trivial. E.g., it is not a few hours of work. This requires probably a few days, I would estimate, if one must adds tests and so forth.

  10. codeinred commented on Feb 15, 2024

    @codeinred
    Contributor

    I just want to comment - there is continued interest in this feature! We were hoping to switch to simdjson, but lack of support for NaN ended up being a showstopper.

    Having a macro that enabled support would be amazing

  11. codeinred commented on Feb 15, 2024

    @codeinred
    Contributor

    Since this issue was closed, could I open a new one requesting optional support behind a feature flag?

  12. lemire commented on Feb 15, 2024

    @lemire
    Member

    @codeinred As a general rule, we follow the specification. The JSON RFC states:

    Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.

    If you (or a colleague) want to implement this feature as an optional (disabled by default) feature, then it is likely we will accept it if it is well done.

    But it is not and it is not going to be on our roadmap. Unless, of course, the JSON RFC changes.

    To put it another way, if you need non-standard features, you need to bear the cost of these non-standard features.

    I am marking this issue as resolved.

  13. locked as resolved and limited conversation to collaborators on Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions