Skip to content

Commit 568c245

Browse files
committed
NaN, Infinity, -Infinity
1 parent f40c15a commit 568c245

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ the standard library.
1212
It differs in behavior from other Python JSON libraries in supporting
1313
datetimes, not supporting subclasses without a `default` hook,
1414
serializing UTF-8 to bytes rather than escaped ASCII (e.g., "好" rather than
15-
"\\\u597d") by default, having strict UTF-8 conformance, not supporting pretty
15+
"\\\u597d") by default, having strict UTF-8 conformance, having strict JSON
16+
conformance on NaN/Infinity/-Infinity, not supporting pretty
1617
printing, and not supporting all standard library options.
1718

1819
It supports CPython 3.6 and 3.7.
@@ -109,7 +110,8 @@ def loads(obj: Union[bytes, str]) -> Union[dict, list, int, float, str, None]: .
109110
`loads()` deserializes JSON to Python objects.
110111

111112
It raises `JSONDecodeError` if given an invalid type or invalid
112-
JSON.
113+
JSON. This includes if the input contains `NaN`, `Infinity`, or `-Infinity`,
114+
which the standard library allows, but is not valid JSON.
113115

114116
`JSONDecodeError` is a subclass of `ValueError`. This is for
115117
compatibility with the standard library.

test/test_type.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ def test_null_array(self):
108108
self.assertEqual(orjson.dumps(obj), ref)
109109
self.assertEqual(orjson.loads(ref), obj)
110110

111+
def test_nan(self):
112+
"""
113+
NaN is not valid JSON
114+
"""
115+
with self.assertRaises(orjson.JSONDecodeError):
116+
orjson.loads('[NaN]')
117+
118+
def test_infinity(self):
119+
"""
120+
Infinity, -Infinity is not valid JSON
121+
"""
122+
with self.assertRaises(orjson.JSONDecodeError):
123+
orjson.loads('[Infinity]')
124+
with self.assertRaises(orjson.JSONDecodeError):
125+
orjson.loads('[-Infinity]')
126+
111127
def test_int_64(self):
112128
"""
113129
int 64-bit

0 commit comments

Comments
 (0)