Skip to content

Add support for parsing NaN and Infinity as requested in #1540, #2414, and #2540 - #2696

Merged
lemire merged 5 commits into
simdjson:masterfrom
codeinred:alecto/nan-inf
May 4, 2026
Merged

lemire merged 5 commits into
simdjson:masterfrom
codeinred:alecto/nan-inf

Conversation

@codeinred

@codeinred codeinred commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary: Add support for parsing NaN and Infinity as requested in #1540, #2414, and #2540

Description

I added support for parsing NaN and Infinity when parsing doubles, or when using get_double_in_string(). Support for NaN and Infinity is gated behind SIMDJSON_ENABLE_NAN_INF. Tests were added for this feature.

SIMDJSON_ENABLE_NAN_INF is disabled by default, but may be enabled by passing -DSIMDJSON_ENABLE_NAN_INF=ON to CMake.

When SIMDJSON_ENABLE_NAN_INF is enabled, parsing is permissive:

  • nan is matched (case-insensitive)
  • inf is matched (case-insensitive).
  • Infinity is matched, as produced by python (requires exact match)
  • infinity is matched (case-insensitive)

A - may appear before nan and infinity. This has no effect on the value of the NaN, but produces -inf when applied to infinity.

NaN and Infinity are only parsed if standard number parsing fails (eg, if the first character is not a digit in parse_double.

NaN and Infinity may also appear in strings, in which case they can be extracted with parse_double_in_string.

In both cases NaN and Infinity are only parsed if regular number parsing would otherwise fail. Any impact on performance should therefore be negligible.

In atom handling, if it fails to match a null atom, it will then check for a nan atom.

Relevant issues

#1540 and #2540. Also mentioned as a feature request in #2414.

Type of change

  • Bug fix
  • Optimization
  • New feature
  • Refactor / cleanup
  • Documentation / tests
  • Other (please describe):

How to verify / test

Additional tests were added for this feature. These are:

  • tests/dom/nan_inf_tests.cpp
  • tests/ondemand/ondemand_nan_inf_tests.cpp

A few tests which previously checked for failures when NaN/Infinity appeared were updated to validate that a NaN or Infinity value is correctly parsed (iff SIMDJSON_ENABLE_NAN_INF is enabled).

Checklist before submitting

  • I added/updated tests covering my change (if applicable)
  • Code builds locally and passes my check
  • Documentation / README updated if needed
  • Commits are atomic and messages are clear
  • I linked the related issue (if applicable)

Documentation: I still need to update documentation. If you're happy with the design choices made by my PR, I can do so.

Note: To make the change smaller, I did not update singleheader/simdjson.h or singleheader/simdjson.cpp. I can update these if needed.

AI Disclosure

Claude was used to update CMake to add the SIMDJSON_ENABLE_NAN_INF feature flag. Claude was also used to write an initial version of the ondemand and nan/inf tests, which then underwent revision and review.

I requested that Claude adhere to simdjson conventions with regard to CMake and with regard to the tests, and based on my review I believe this to be the case.

Comment thread CMakeLists.txt Resolved
Comment thread CMakeLists.txt Resolved
Comment thread include/simdjson/common_defs.h Resolved
@lemire

lemire commented Apr 24, 2026

Copy link
Copy Markdown
Member

@codeinred Just so you understand why it needs to be disabled by default:

Capture d’écran, le 2026-04-24 à 12 18 16

Anyone producing JSON with Infinity or some such is producing broken JSON that will not be parseable in, say, JavaScript. It defeats the purpose of JSON as a universal/portable format. Once we start breaking the spec, all sorts of nasty issues come up. So I strongly discourage it.

I am fine with disabled-by-default features though.

double num;
double num = 0.0;
#if SIMDJSON_ENABLE_NAN_INF
ASSERT_EQUAL( val.get(num), simdjson::error_code::SUCCESS );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ASSERT_EQUAL( val.get(num), simdjson::error_code::SUCCESS );
ASSERT_SUCCESS( val.get(num));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here the construction does not work. ASSERT_TRUE is a macro, and the PR passes a macro inside the macro. It gets too complicated.

What you want to do is start with the condition

#if SIMDJSON_ENABLE_NAN_INF
ASSERT_TRUE(te...
#else  // SIMDJSON_ENABLE_NAN_INF
ASSERT_TRUE(te...
#endif  // SIMDJSON_ENABLE_NAN_INF

@lemire

lemire commented Apr 24, 2026

Copy link
Copy Markdown
Member

@codeinred If you look at my comments, the Visual Studio errors ought to be easy to fix.

Otherwise, this looks like good work to me.

@lemire

lemire commented Apr 24, 2026 •

Copy link
Copy Markdown
Member

@codeinred Note that I don't care whether infinity is supported or not, your PR description is clear, but I encourage you to write it out in the code.

@codeinred
codeinred force-pushed the alecto/nan-inf branch 2 times, most recently from cf20e88 to 08af4ad Compare April 27, 2026 21:18
@codeinred

Copy link
Copy Markdown
Contributor Author

Hi @lemire! I appreciate all of your feedback. I have updated my PR with the following changes:

  • I updated the CMake so that extended parser support for NaN/Inf is now disabled by default, as requested.

  • I updated the parser to support case-insensitive parsing of infinity (8 characters) in addition to the shorter version.

  • I updated the tests to check for case-insensitive infinity

  • Per your recommendation, I fixed the use of #if SIMDJSON_ENABLE_NAN_INF inside a macro argument, and I've confirmed that the library compiles and tests pass with MSVC (I tested Visual Studio 2022, but I expect it to work on earlier versions as well).

    This last suggestion was particularly helpful, and saved me some time debugging - thank you!

I've cleaned up the commit history as well, and I attempted to update a few CI/CD workflows in order to also test the library with support for NaN/Infinity enabled. This is the top commit.

image

This was my attempt to save you the work of updating CI, but please feel free to discard that commit if you do not like the changes. Please let me know if you have additional feedback!

@lemire

lemire commented May 4, 2026

Copy link
Copy Markdown
Member

Will be part of our next major release.

@lemire
lemire merged commit b9b20be into simdjson:master May 4, 2026
112 checks passed
@codeinred
codeinred deleted the alecto/nan-inf branch May 5, 2026 14:02
pitrou pushed a commit to apache/arrow that referenced this pull request Jul 28, 2026
### Rationale for this change

This is part of the work for #35460. Back in January I started working on this change-set, but after discussions came up regarding the use of the `simdjson::dom` vs `simdjson::ondemand` front-end the development went stale. I saw that @ Reranko05 started the work on #35460 and already made great progress, well done!

I still wanted to put up this change-set as a possible view-point, but feel free to disregard @ Reranko05 if you already started your own work.

### What changes are included in this PR?

This changes `from_string.cc` to use simdjson instead of RapidJSON.

### Are there any user-facing changes?

Yes, the API has changed to no longer allow non-utf8 compatible strings. `simdjson` does not support non-compliant inputs and does not intent to support it in the future [[1]](simdjson/simdjson#1691 (comment)) [[2]](https://github.com/simdjson/simdjson/blob/8e6bac94877f2d3d026000d36ce81e0aaf38d26f/doc/basics.md?plain=1#L3701) [[3]](simdjson/simdjson#2125)

All call-sites in tests only used a transformation to non-utf8 json as an intermediate result to easily go from `string -> Array`, so I changed these to use different helper methods instead.

Another difficult problem arises in the handling of `NaN` and `Inf` literals in inputs. `simdjson` follows the json standard, whereas `RapidJSON` implemented an extension where the following is a valid input:
```
{"x": Inf, "y": -Inf, "z": NaN, "n": Infinity, "m": -Infinity}
```

We need to decide whether to be breaking here or not. This PR now contains a non-breaking version of the change. This can be quite elegantly added by using the public `ondemand` api's function to retrieve the raw token string in the `ConvertNumber` function.

Alternatively, we can wait for the next `simdjson` major release, which will ship with support for a `SIMDJSON_ENABLE_NAN_INF` compilation flag as per [this pull request](simdjson/simdjson#2696). (this was only merged to simdjson main)

### Are these changes tested?

Yes

* GitHub Issue: #50627

Authored-by: Alexander Taepper <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants