Skip to content

Commit

Permalink
Fix encoding of infinity (ultrajson#80).
Browse files Browse the repository at this point in the history
Infinity was being encoded as 'Inf' which, whilst the JSON spec doesn't include
any non-finite floats, differs from the conventions in other JSON libraries,
JavaScript of using 'Infinity'. It also differs from what `ujson.loads()`
expects so that `ujson.loads(ujson.dumps(math.inf))` raises an exception.

Closes ultrajson#80.
  • Loading branch information
bwoodsend committed Aug 6, 2022
1 parent bcdc041 commit 38b4e91
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs)

if (encoder.allowNan)
{
csInf = "Inf";
csInf = "Infinity";
csNan = "NaN";
}

Expand Down
4 changes: 2 additions & 2 deletions tests/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@ def test_encode_no_assert(test_input):
(1.0, "1.0"),
(OrderedDict([(1, 1), (0, 0), (8, 8), (2, 2)]), '{"1":1,"0":0,"8":8,"2":2}'),
({"a": float("NaN")}, '{"a":NaN}'),
({"a": float("inf")}, '{"a":Inf}'),
({"a": -float("inf")}, '{"a":-Inf}'),
({"a": float("inf")}, '{"a":Infinity}'),
({"a": -float("inf")}, '{"a":-Infinity}'),
],
)
def test_encode(test_input, expected):
Expand Down

0 comments on commit 38b4e91

Please sign in to comment.