Skip to content

Commit 1f7f806

Browse files
committed
Fixed serialization for custom ints and floats.
[GitHub issue #57](#57) Fixed serialization for objects that subclass `int` or `float`: Previously we would use the objects __str__ implementation, but that might result in an illegal JSON5 value if the object had customized __str__ to return something illegal. Instead, we follow the lead of the `JSON` module and call `int.__repr__` or `float.__repr__` directly. While I was at it, I added tests for dumps(-inf) and dumps(nan) when those were supposed to be disallowed by `allow_nan=False`.
1 parent 4b8217b commit 1f7f806

4 files changed

Lines changed: 48 additions & 20 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ To run the tests, setup a venv and install the required dependencies with
5757

5858
## Version History / Release Notes
5959

60+
* v0.9.9 (2022-08-01)
61+
* [GitHub issue #57](https://github.com/dpranke/pyjson5/issues/57)
62+
Fixed serialization for objects that subclass `int` or `float`:
63+
Previously we would use the objects __str__ implementation, but
64+
that might result in an illegal JSON5 value if the object had
65+
customized __str__ to return something illegal. Instead,
66+
we follow the lead of the `JSON` module and call `int.__repr__`
67+
or `float.__repr__` directly.
68+
* While I was at it, I added tests for dumps(-inf) and dumps(nan)
69+
when those were supposed to be disallowed by `allow_nan=False`.
6070
* v0.9.8 (2022-05-08)
6171
* [GitHub issue #47](https://github.com/dpranke/pyjson5/issues/47)
6272
Fixed error reporting in some cases due to how parsing was handling

‎json5/lib.py‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,36 @@ def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent,
264264
s = u'false'
265265
elif obj is None:
266266
s = u'null'
267+
elif obj == math.inf:
268+
if allow_nan:
269+
s = u'Infinity'
270+
else:
271+
raise ValueError()
272+
elif obj == -math.inf:
273+
if allow_nan:
274+
s = u'-Infinity'
275+
else:
276+
raise ValueError()
277+
elif isinstance(obj, float) and math.isnan(obj):
278+
if allow_nan:
279+
s = u'NaN'
280+
else:
281+
raise ValueError()
267282
elif isinstance(obj, str_types):
268283
if (is_key and _is_ident(obj) and not quote_keys
269284
and not _is_reserved_word(obj)):
270285
return True, obj
271286
return True, _dump_str(obj, ensure_ascii)
272-
elif isinstance(obj, float):
273-
s = _dump_float(obj,allow_nan)
274287
elif isinstance(obj, int):
275-
s = str(obj)
288+
# Subclasses of `int` and `float` may have custom
289+
# __repr__ or __str__ methods, but the `JSON` library
290+
# ignores them in order to ensure that the representation
291+
# are just bare numbers. In order to match JSON's behavior
292+
# we call the methods of the `float` and `int` class directly.
293+
s = int.__repr__(obj)
294+
elif isinstance(obj, float):
295+
# See comment above for int
296+
s = float.__repr__(obj)
276297
else:
277298
s = None
278299

@@ -403,20 +424,6 @@ def _dump_array(obj, skipkeys, ensure_ascii, check_circular, allow_nan,
403424
end_str + u']')
404425

405426

406-
def _dump_float(obj, allow_nan):
407-
if allow_nan:
408-
if math.isnan(obj):
409-
return 'NaN'
410-
if obj == float('inf'):
411-
return 'Infinity'
412-
if obj == float('-inf'):
413-
return '-Infinity'
414-
elif math.isnan(obj) or obj == float('inf') or obj == float('-inf'):
415-
raise ValueError('Out of range float values '
416-
'are not JSON compliant')
417-
return str(obj)
418-
419-
420427
def _dump_str(obj, ensure_ascii):
421428
ret = ['"']
422429
for ch in obj:

‎json5/version.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
VERSION = '0.9.8'
15+
VERSION = '0.9.9'

‎tests/lib_test.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,20 @@ def __len__(self):
347347
self.assertEqual(json5.dumps(MyArray()), '[0, 1, 1]')
348348

349349
def test_custom_numbers(self):
350+
# See https://github.com/dpranke/pyjson5/issues/57: we
351+
# need to ensure that we use the bare int.__repr__ and
352+
# float.__repr__ in order to get legal JSON values when
353+
# people have custom subclasses with customer __repr__ methods.
354+
# (This is what JSON does and we want to match it).
350355
class MyInt(int):
351-
pass
356+
def __repr__(self):
357+
return 'fail'
352358

353359
self.assertEqual(json5.dumps(MyInt(5)), '5')
354360

355361
class MyFloat(float):
356-
pass
362+
def __repr__(self):
363+
return 'fail'
357364

358365
self.assertEqual(json5.dumps(MyFloat(0.5)), '0.5')
359366

@@ -427,6 +434,10 @@ def test_numbers(self):
427434

428435
self.assertRaises(ValueError, json5.dumps,
429436
float('inf'), allow_nan=False)
437+
self.assertRaises(ValueError, json5.dumps,
438+
float('-inf'), allow_nan=False)
439+
self.assertRaises(ValueError, json5.dumps,
440+
float('nan'), allow_nan=False)
430441

431442
def test_null(self):
432443
self.check(None, 'null')

0 commit comments

Comments
 (0)