32
32
#ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
33
33
34
34
INFINITY = float ('inf' )
35
- FLOAT_REPR = repr
36
35
37
36
def py_encode_basestring (s ):
38
37
"""Return a JSON representation of a Python string
@@ -221,7 +220,7 @@ def iterencode(self, o, _one_shot=False):
221
220
_encoder = encode_basestring
222
221
223
222
def floatstr (o , allow_nan = self .allow_nan ,
224
- _repr = FLOAT_REPR , _inf = INFINITY , _neginf = - INFINITY ):
223
+ _repr = float . __repr__ , _inf = INFINITY , _neginf = - INFINITY ):
225
224
# Check for specials. Note that this type of test is processor
226
225
# and/or platform-specific, so do tests which don't depend on the
227
226
# internals.
@@ -268,6 +267,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
268
267
list = list ,
269
268
str = str ,
270
269
tuple = tuple ,
270
+ _intstr = int .__str__ ,
271
271
):
272
272
273
273
if _indent is not None and not isinstance (_indent , str ):
@@ -309,10 +309,10 @@ def _iterencode_list(lst, _current_indent_level):
309
309
# Subclasses of int/float may override __str__, but we still
310
310
# want to encode them as integers/floats in JSON. One example
311
311
# within the standard library is IntEnum.
312
- yield buf + str ( int ( value ) )
312
+ yield buf + _intstr ( value )
313
313
elif isinstance (value , float ):
314
314
# see comment above for int
315
- yield buf + _floatstr (float ( value ) )
315
+ yield buf + _floatstr (value )
316
316
else :
317
317
yield buf
318
318
if isinstance (value , (list , tuple )):
@@ -359,7 +359,7 @@ def _iterencode_dict(dct, _current_indent_level):
359
359
# also allow them. Many encoders seem to do something like this.
360
360
elif isinstance (key , float ):
361
361
# see comment for int/float in _make_iterencode
362
- key = _floatstr (float ( key ) )
362
+ key = _floatstr (key )
363
363
elif key is True :
364
364
key = 'true'
365
365
elif key is False :
@@ -368,7 +368,7 @@ def _iterencode_dict(dct, _current_indent_level):
368
368
key = 'null'
369
369
elif isinstance (key , int ):
370
370
# see comment for int/float in _make_iterencode
371
- key = str ( int ( key ) )
371
+ key = _intstr ( key )
372
372
elif _skipkeys :
373
373
continue
374
374
else :
@@ -389,10 +389,10 @@ def _iterencode_dict(dct, _current_indent_level):
389
389
yield 'false'
390
390
elif isinstance (value , int ):
391
391
# see comment for int/float in _make_iterencode
392
- yield str ( int ( value ) )
392
+ yield _intstr ( value )
393
393
elif isinstance (value , float ):
394
394
# see comment for int/float in _make_iterencode
395
- yield _floatstr (float ( value ) )
395
+ yield _floatstr (value )
396
396
else :
397
397
if isinstance (value , (list , tuple )):
398
398
chunks = _iterencode_list (value , _current_indent_level )
@@ -419,10 +419,10 @@ def _iterencode(o, _current_indent_level):
419
419
yield 'false'
420
420
elif isinstance (o , int ):
421
421
# see comment for int/float in _make_iterencode
422
- yield str ( int ( o ) )
422
+ yield _intstr ( o )
423
423
elif isinstance (o , float ):
424
424
# see comment for int/float in _make_iterencode
425
- yield _floatstr (float ( o ) )
425
+ yield _floatstr (o )
426
426
elif isinstance (o , (list , tuple )):
427
427
yield from _iterencode_list (o , _current_indent_level )
428
428
elif isinstance (o , dict ):
0 commit comments