Skip to content

Feature Request: Add Optional Support for Exotic NaN/Infinity Formats in JSON Parsing #2414

Closed

Description

Support for Parsing Exotic NaN, Inf, and -Inf as an Optional Mode

The simdjson library currently adheres strictly to the JSON specification (RFC 8259), which does not allow special floating-point values like NaN, Inf, or -Inf.

However, many real-world datasets (e.g., from scientific computing, machine learning, or legacy systems) use these non-standard representations for special floating-point values.

Proposal
Add an optional parsing mode (e.g., via a flag or parser configuration) to allow parsing of the following exotic float literals:

json
Copy
Edit
{
"value1": NaN,
"value2": Inf,
"value3": -Inf
}
These would be interpreted internally as:

NaN → std::numeric_limits::quiet_NaN()

Inf → std::numeric_limits::infinity()

-Inf → -std::numeric_limits::infinity()

Implementation Ideas
This feature should be opt-in only to preserve spec-compliant behavior by default.

Interoperability with other JSON-like serializers that support exotic floats.

Scientific data interchange formats (e.g., MATLAB-exported JSON, Octave, Scilab, Nelson, etc...).

ECMA-262 JSON.parse disallows these, but many ecosystems use relaxed parsers (yyjson, rapidjson)

Backward Compatibility
There should be no change to existing behavior unless the new parsing flag is enabled.

PS: I would be interested to use simdjson with Nelson (https://nelson-lang.github.io/nelson-gitbook/releases/en_US/v1.14.0/json/index.html)

Activity

  1. lemire commented on Aug 13, 2025

    @lemire
    Member

    If I load you proposed JSON is JavaScript, I get...

    Image

    If you load it in Python, you get:

    Image

    I have written on the topic:

    If your tool generates JSON that cannot be parsed by Python and JavaScript out of the box, please do not call it JSON.

    The standard approach is to embed your special values in strings, as follows;

    {"value1": "NaN","value2": "Inf","value3": "-Inf"}

    Now, in simdjson, we do have non-standard extensions, hidden behind macros that are disabled by default. They are user-contributed. Thus, if this feature is valuable to you, I invite you or someone you work with to prepare a pull request. We will give it full consideration.

  2. lemire commented on Aug 14, 2025

    @lemire
    Member

    Note that the On-Demand API is already rich enough that you can provide your own parsing routines.

        padded_string json = "{\"key\": NaN}"_padded;
        simdjson::ondemand::parser parser;
        simdjson::ondemand::document doc = parser.iterate(json);
        simdjson::ondemand::object object = doc.get_object();
        simdjson::ondemand::value val = object["key"];
        try {
          double num = val.get_double();
        } catch (const simdjson::simdjson_error& e) {
          // e == simdjson::error_code::INCORRECT_TYPE
          std::string_view str = val.raw_json_token();
          // str == "NaN"
        }
  3. Nelson-numerical-software commented on Aug 15, 2025

    @Nelson-numerical-software
    Author

    Thanks for your tips !!!
    Work in progress (impressive) with exotic behavior implemented in nelson.

    jsonFilename = websave([tempdir(),'/test.json'], 'https://raw.githubusercontent.com/miloyip/nativejson-benchmark/refs/heads/master/data/canada.json');
    jsonContent = fileread(jsonFilename);  
    tic();st = jsondecode(jsonContent);toc()  
    Image
  4. lemire commented on Aug 15, 2025

    @lemire
    Member
  5. Nelson-numerical-software commented on Dec 10, 2025

    @Nelson-numerical-software
    Author

    fixed for me with tips #2540 is more revelant

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions