summaryrefslogtreecommitdiffstats
path: root/Lib/json/encoder.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/json/encoder.py')
-rw-r--r--Lib/json/encoder.py29
1 files changed, 18 insertions, 11 deletions
diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py
index 0513838..d596489 100644
--- a/Lib/json/encoder.py
+++ b/Lib/json/encoder.py
@@ -7,6 +7,10 @@ try:
except ImportError:
c_encode_basestring_ascii = None
try:
+ from _json import encode_basestring as c_encode_basestring
+except ImportError:
+ c_encode_basestring = None
+try:
from _json import make_encoder as c_make_encoder
except ImportError:
c_make_encoder = None
@@ -28,9 +32,8 @@ for i in range(0x20):
#ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
INFINITY = float('inf')
-FLOAT_REPR = repr
-def encode_basestring(s):
+def py_encode_basestring(s):
"""Return a JSON representation of a Python string
"""
@@ -39,6 +42,9 @@ def encode_basestring(s):
return '"' + ESCAPE.sub(replace, s) + '"'
+encode_basestring = (c_encode_basestring or py_encode_basestring)
+
+
def py_encode_basestring_ascii(s):
"""Return an ASCII-only JSON representation of a Python string
@@ -214,7 +220,7 @@ class JSONEncoder(object):
_encoder = encode_basestring
def floatstr(o, allow_nan=self.allow_nan,
- _repr=FLOAT_REPR, _inf=INFINITY, _neginf=-INFINITY):
+ _repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY):
# 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.
@@ -261,6 +267,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
list=list,
str=str,
tuple=tuple,
+ _intstr=int.__str__,
):
if _indent is not None and not isinstance(_indent, str):
@@ -302,10 +309,10 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
# Subclasses of int/float may override __str__, but we still
# want to encode them as integers/floats in JSON. One example
# within the standard library is IntEnum.
- yield buf + str(int(value))
+ yield buf + _intstr(value)
elif isinstance(value, float):
# see comment above for int
- yield buf + _floatstr(float(value))
+ yield buf + _floatstr(value)
else:
yield buf
if isinstance(value, (list, tuple)):
@@ -352,7 +359,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
# also allow them. Many encoders seem to do something like this.
elif isinstance(key, float):
# see comment for int/float in _make_iterencode
- key = _floatstr(float(key))
+ key = _floatstr(key)
elif key is True:
key = 'true'
elif key is False:
@@ -361,7 +368,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
key = 'null'
elif isinstance(key, int):
# see comment for int/float in _make_iterencode
- key = str(int(key))
+ key = _intstr(key)
elif _skipkeys:
continue
else:
@@ -382,10 +389,10 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
yield 'false'
elif isinstance(value, int):
# see comment for int/float in _make_iterencode
- yield str(int(value))
+ yield _intstr(value)
elif isinstance(value, float):
# see comment for int/float in _make_iterencode
- yield _floatstr(float(value))
+ yield _floatstr(value)
else:
if isinstance(value, (list, tuple)):
chunks = _iterencode_list(value, _current_indent_level)
@@ -412,10 +419,10 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
yield 'false'
elif isinstance(o, int):
# see comment for int/float in _make_iterencode
- yield str(int(o))
+ yield _intstr(o)
elif isinstance(o, float):
# see comment for int/float in _make_iterencode
- yield _floatstr(float(o))
+ yield _floatstr(o)
elif isinstance(o, (list, tuple)):
yield from _iterencode_list(o, _current_indent_level)
elif isinstance(o, dict):