Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As described in #983, the JSON output is currently generating invalid JSON when non-finite floats (like NaN or Infinity) are included.
As @sbrugman suggested, I added a config option to switch between three behaviours:
PYTHON_NATIVE
: Use Python's default behaviour and generate the invalid JSON (as before)NULL
: Encode non-finite numbers as null values.STRING
: Stringify non-finite numbers (for example, NaN becomes the string "nan")I made the null output the new default because it seems less surprising to encounter a null value when parsing numbers in the JSON compared to a "nan" string. I see that there is also an argument to make the old behaviour the default, to ensure 100% compatibility with existing code. Please let me know what you think should be the default.
Summary of the changes
JsonNonFiniteEncoding
with the three options listed abovejson_non_finite_encoding
field in theSettings
classencode_it
method that already manages the JSON-encoding of all values, I added special handling for non-finite floats, which encodes the values according to the configuration.The changes should not break anything except that the JSON output will now contain null values instead of the raw "NaN" entries that violate the standard.
Example usage
Gives the following output:
When choosing the
NULL
config option, it would be:And with the
PYTHON_NATIVE
option it would be: