3232 #ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
3333
3434INFINITY = float ('inf' )
35- FLOAT_REPR = repr
3635
3736def py_encode_basestring (s ):
3837 """Return a JSON representation of a Python string
@@ -221,7 +220,7 @@ def iterencode(self, o, _one_shot=False):
221220 _encoder = encode_basestring
222221
223222 def floatstr (o , allow_nan = self .allow_nan ,
224- _repr = FLOAT_REPR , _inf = INFINITY , _neginf = - INFINITY ):
223+ _repr = float . __repr__ , _inf = INFINITY , _neginf = - INFINITY ):
225224 # Check for specials. Note that this type of test is processor
226225 # and/or platform-specific, so do tests which don't depend on the
227226 # internals.
@@ -268,6 +267,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
268267 list = list ,
269268 str = str ,
270269 tuple = tuple ,
270+ _intstr = int .__str__ ,
271271 ):
272272
273273 if _indent is not None and not isinstance (_indent , str ):
@@ -309,10 +309,10 @@ def _iterencode_list(lst, _current_indent_level):
309309 # Subclasses of int/float may override __str__, but we still
310310 # want to encode them as integers/floats in JSON. One example
311311 # within the standard library is IntEnum.
312- yield buf + str ( int ( value ) )
312+ yield buf + _intstr ( value )
313313 elif isinstance (value , float ):
314314 # see comment above for int
315- yield buf + _floatstr (float ( value ) )
315+ yield buf + _floatstr (value )
316316 else :
317317 yield buf
318318 if isinstance (value , (list , tuple )):
@@ -359,7 +359,7 @@ def _iterencode_dict(dct, _current_indent_level):
359359 # also allow them. Many encoders seem to do something like this.
360360 elif isinstance (key , float ):
361361 # see comment for int/float in _make_iterencode
362- key = _floatstr (float ( key ) )
362+ key = _floatstr (key )
363363 elif key is True :
364364 key = 'true'
365365 elif key is False :
@@ -368,7 +368,7 @@ def _iterencode_dict(dct, _current_indent_level):
368368 key = 'null'
369369 elif isinstance (key , int ):
370370 # see comment for int/float in _make_iterencode
371- key = str ( int ( key ) )
371+ key = _intstr ( key )
372372 elif _skipkeys :
373373 continue
374374 else :
@@ -389,10 +389,10 @@ def _iterencode_dict(dct, _current_indent_level):
389389 yield 'false'
390390 elif isinstance (value , int ):
391391 # see comment for int/float in _make_iterencode
392- yield str ( int ( value ) )
392+ yield _intstr ( value )
393393 elif isinstance (value , float ):
394394 # see comment for int/float in _make_iterencode
395- yield _floatstr (float ( value ) )
395+ yield _floatstr (value )
396396 else :
397397 if isinstance (value , (list , tuple )):
398398 chunks = _iterencode_list (value , _current_indent_level )
@@ -419,10 +419,10 @@ def _iterencode(o, _current_indent_level):
419419 yield 'false'
420420 elif isinstance (o , int ):
421421 # see comment for int/float in _make_iterencode
422- yield str ( int ( o ) )
422+ yield _intstr ( o )
423423 elif isinstance (o , float ):
424424 # see comment for int/float in _make_iterencode
425- yield _floatstr (float ( o ) )
425+ yield _floatstr (o )
426426 elif isinstance (o , (list , tuple )):
427427 yield from _iterencode_list (o , _current_indent_level )
428428 elif isinstance (o , dict ):
0 commit comments