3030for i in range (0x20 ):
3131 ESCAPE_DCT .setdefault (chr (i ), '\\ u{0:04x}' .format (i ))
3232 #ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
33+ del i
3334
3435INFINITY = float ('inf' )
3536
@@ -71,7 +72,7 @@ def replace(match):
7172 c_encode_basestring_ascii or py_encode_basestring_ascii )
7273
7374class JSONEncoder (object ):
74- """Extensible JSON <http ://json.org> encoder for Python data structures.
75+ """Extensible JSON <https ://json.org> encoder for Python data structures.
7576
7677 Supports the following objects and types by default:
7778
@@ -107,8 +108,8 @@ def __init__(self, *, skipkeys=False, ensure_ascii=True,
107108 """Constructor for JSONEncoder, with sensible defaults.
108109
109110 If skipkeys is false, then it is a TypeError to attempt
110- encoding of keys that are not str, int, float or None. If
111- skipkeys is True, such items are simply skipped.
111+ encoding of keys that are not str, int, float, bool or None.
112+ If skipkeys is True, such items are simply skipped.
112113
113114 If ensure_ascii is true, the output is guaranteed to be str
114115 objects with all incoming non-ASCII characters escaped. If
@@ -173,7 +174,7 @@ def default(self, o):
173174 else:
174175 return list(iterable)
175176 # Let the base class default method raise the TypeError
176- return JSONEncoder .default(self, o)
177+ return super() .default(o)
177178
178179 """
179180 raise TypeError (f'Object of type { o .__class__ .__name__ } '
@@ -243,15 +244,18 @@ def floatstr(o, allow_nan=self.allow_nan,
243244 return text
244245
245246
246- if (_one_shot and c_make_encoder is not None
247- and self .indent is None ):
247+ if self .indent is None or isinstance (self .indent , str ):
248+ indent = self .indent
249+ else :
250+ indent = ' ' * self .indent
251+ if _one_shot and c_make_encoder is not None :
248252 _iterencode = c_make_encoder (
249- markers , self .default , _encoder , self . indent ,
253+ markers , self .default , _encoder , indent ,
250254 self .key_separator , self .item_separator , self .sort_keys ,
251255 self .skipkeys , self .allow_nan )
252256 else :
253257 _iterencode = _make_iterencode (
254- markers , self .default , _encoder , self . indent , floatstr ,
258+ markers , self .default , _encoder , indent , floatstr ,
255259 self .key_separator , self .item_separator , self .sort_keys ,
256260 self .skipkeys , _one_shot )
257261 return _iterencode (o , 0 )
@@ -271,9 +275,6 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
271275 _intstr = int .__repr__ ,
272276 ):
273277
274- if _indent is not None and not isinstance (_indent , str ):
275- _indent = ' ' * _indent
276-
277278 def _iterencode_list (lst , _current_indent_level ):
278279 if not lst :
279280 yield '[]'
@@ -344,7 +345,6 @@ def _iterencode_dict(dct, _current_indent_level):
344345 _current_indent_level += 1
345346 newline_indent = '\n ' + _indent * _current_indent_level
346347 item_separator = _item_separator + newline_indent
347- yield newline_indent
348348 else :
349349 newline_indent = None
350350 item_separator = _item_separator
@@ -377,6 +377,8 @@ def _iterencode_dict(dct, _current_indent_level):
377377 f'not { key .__class__ .__name__ } ' )
378378 if first :
379379 first = False
380+ if newline_indent is not None :
381+ yield newline_indent
380382 else :
381383 yield item_separator
382384 yield _encoder (key )
@@ -403,7 +405,7 @@ def _iterencode_dict(dct, _current_indent_level):
403405 else :
404406 chunks = _iterencode (value , _current_indent_level )
405407 yield from chunks
406- if newline_indent is not None :
408+ if not first and newline_indent is not None :
407409 _current_indent_level -= 1
408410 yield '\n ' + _indent * _current_indent_level
409411 yield '}'
0 commit comments