Repository navigation
Add support for parsing NaN and Infinity as requested in #1540, #2414, and #2540 - #2696
Conversation
|
@codeinred Just so you understand why it needs to be disabled by default:
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 ); |
There was a problem hiding this comment.
| ASSERT_EQUAL( val.get(num), simdjson::error_code::SUCCESS ); | |
| ASSERT_SUCCESS( val.get(num)); |
There was a problem hiding this comment.
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|
@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. |
|
@codeinred Note that I don't care whether |
8582e79 to
b5c269b
Compare
cf20e88 to
08af4ad
Compare
08af4ad to
426fe81
Compare
|
Hi @lemire! I appreciate all of your feedback. I have updated my PR with the following changes:
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.
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! |
|
Will be part of our next major release. |
### 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]>


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 behindSIMDJSON_ENABLE_NAN_INF. Tests were added for this feature.SIMDJSON_ENABLE_NAN_INFis disabled by default, but may be enabled by passing-DSIMDJSON_ENABLE_NAN_INF=ONto CMake.When
SIMDJSON_ENABLE_NAN_INFis enabled, parsing is permissive:nanis matched (case-insensitive)infis matched (case-insensitive).Infinityis matched, as produced by python (requires exact match)infinityis matched (case-insensitive)A
-may appear before nan and infinity. This has no effect on the value of the NaN, but produces-infwhen 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
nullatom, it will then check for ananatom.Relevant issues
#1540 and #2540. Also mentioned as a feature request in #2414.
Type of change
How to verify / test
Additional tests were added for this feature. These are:
tests/dom/nan_inf_tests.cpptests/ondemand/ondemand_nan_inf_tests.cppA 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_INFis enabled).Checklist before submitting
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.horsingleheader/simdjson.cpp. I can update these if needed.AI Disclosure
Claude was used to update CMake to add the
SIMDJSON_ENABLE_NAN_INFfeature 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.