Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current JSON outputs for Infinity, -Infinity, and NaN are not valid JSON. New option to allow output them as String. #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion simplejson/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
indent=None, separators=None, encoding='utf-8', default=None,
use_decimal=True, namedtuple_as_object=True,
tuple_as_array=True, bigint_as_string=False,
big_floats_as_string=False,
item_sort_key=None, for_json=False, ignore_nan=False,
int_as_string_bitcount=None, iterable_as_array=False):
"""Constructor for JSONEncoder, with sensible defaults.
Expand Down Expand Up @@ -203,6 +204,10 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
or lower than -2**53 will be encoded as strings. This is to avoid the
rounding that happens in Javascript otherwise.

If big_floats_as_string is true (not the default), Infinity, -Infinity,
and NaN will be encoded as strings. This is to avoid the parser error
in valid JSON decoders.

If int_as_string_bitcount is a positive number (n), then int of size
greater than or equal to 2**n or lower than or equal to -2**n will be
encoded as strings.
Expand Down Expand Up @@ -232,6 +237,7 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
self.tuple_as_array = tuple_as_array
self.iterable_as_array = iterable_as_array
self.bigint_as_string = bigint_as_string
self.big_floats_as_string = big_floats_as_string
self.item_sort_key = item_sort_key
self.for_json = for_json
self.ignore_nan = ignore_nan
Expand Down Expand Up @@ -321,7 +327,7 @@ def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding):
return _orig_encoder(o)

def floatstr(o, allow_nan=self.allow_nan, ignore_nan=self.ignore_nan,
_repr=FLOAT_REPR, _inf=PosInf, _neginf=-PosInf):
_repr=FLOAT_REPR, _inf=PosInf, _neginf=-PosInf, big_floats_as_string=self.big_floats_as_string):
# Check for specials. Note that this type of test is processor
# and/or platform-specific, so do tests which don't depend on
# the internals.
Expand All @@ -344,6 +350,8 @@ def floatstr(o, allow_nan=self.allow_nan, ignore_nan=self.ignore_nan,
raise ValueError(
"Out of range float values are not JSON compliant: " +
repr(o))
elif big_floats_as_string:
text = self.encode(text)

return text

Expand Down
4 changes: 4 additions & 0 deletions simplejson/tests/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def test_degenerates_deny(self):
for f in (PosInf, NegInf, NaN):
self.assertRaises(ValueError, json.dumps, f, allow_nan=False)

def test_big_floats_as_string(self):
for num in [NaN, PosInf, NegInf]:
self.assertEqual(repr(float(json.loads(json.dumps(num, big_floats_as_string=True)))), repr(num))

def test_floats(self):
for num in [1617161771.7650001, math.pi, math.pi**100,
math.pi**-100, 3.1]:
Expand Down