Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Lib/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None,
default=None, sort_keys=False, **kw):
default=None, sort_keys=False, list_oneline=False, **kw):
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
``.write()``-supporting file-like object).

Expand Down Expand Up @@ -155,6 +155,9 @@ def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,

If *sort_keys* is true (default: ``False``), then the output of
dictionaries will be sorted by key.

If *list_oneline* is true (default: ``False``), then lists/tuples will be
encoded as arrays on a single line.

To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
``.default()`` method to serialize additional types), specify it with
Expand All @@ -172,8 +175,8 @@ def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
cls = JSONEncoder
iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
check_circular=check_circular, allow_nan=allow_nan, indent=indent,
separators=separators,
default=default, sort_keys=sort_keys, **kw).iterencode(obj)
separators=separators, default=default, sort_keys=sort_keys,
list_oneline=list_oneline, **kw).iterencode(obj)
# could accelerate with writelines in some versions of Python, at
# a debuggability cost
for chunk in iterable:
Expand All @@ -182,7 +185,7 @@ def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,

def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None,
default=None, sort_keys=False, **kw):
default=None, sort_keys=False, list_oneline=False, **kw):
"""Serialize ``obj`` to a JSON formatted ``str``.

If ``skipkeys`` is true then ``dict`` keys that are not basic types
Expand Down Expand Up @@ -217,6 +220,9 @@ def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,

If *sort_keys* is true (default: ``False``), then the output of
dictionaries will be sorted by key.

If *list_oneline* is true (default: ``False``), then lists/tuples will be
encoded as arrays on a single line.

To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
``.default()`` method to serialize additional types), specify it with
Expand All @@ -235,7 +241,7 @@ def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
skipkeys=skipkeys, ensure_ascii=ensure_ascii,
check_circular=check_circular, allow_nan=allow_nan, indent=indent,
separators=separators, default=default, sort_keys=sort_keys,
**kw).encode(obj)
list_oneline=list_oneline, **kw).encode(obj)


_default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None)
Expand Down
18 changes: 14 additions & 4 deletions Lib/json/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class JSONEncoder(object):
key_separator = ': '
def __init__(self, *, skipkeys=False, ensure_ascii=True,
check_circular=True, allow_nan=True, sort_keys=False,
indent=None, separators=None, default=None):
indent=None, separators=None, default=None, list_oneline = False):
"""Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt
Expand All @@ -129,6 +129,10 @@ def __init__(self, *, skipkeys=False, ensure_ascii=True,
sorted by key; this is useful for regression tests to ensure
that JSON serializations can be compared on a day-to-day basis.

If list_oneline is true, then lists/tuples will be output as arrays
on a single line. If false, a newline character will be placed after each
array element.

If indent is a non-negative integer, then JSON array
elements and object members will be pretty-printed with that
indent level. An indent level of 0 will only insert newlines.
Expand All @@ -151,6 +155,7 @@ def __init__(self, *, skipkeys=False, ensure_ascii=True,
self.allow_nan = allow_nan
self.sort_keys = sort_keys
self.indent = indent
self.list_oneline = list_oneline
if separators is not None:
self.item_separator, self.key_separator = separators
elif indent is not None:
Expand Down Expand Up @@ -254,11 +259,12 @@ def floatstr(o, allow_nan=self.allow_nan,
_iterencode = _make_iterencode(
markers, self.default, _encoder, self.indent, floatstr,
self.key_separator, self.item_separator, self.sort_keys,
self.skipkeys, _one_shot)
self.skipkeys, _one_shot, self.list_oneline)
return _iterencode(o, 0)

def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
_key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
list_oneline,
## HACK: hand-optimized bytecode; turn globals into locals
ValueError=ValueError,
dict=dict,
Expand Down Expand Up @@ -287,7 +293,10 @@ def _iterencode_list(lst, _current_indent_level):
buf = '['
if _indent is not None:
_current_indent_level += 1
newline_indent = '\n' + _indent * _current_indent_level
if not list_oneline:
newline_indent = '\n' + _indent * _current_indent_level
else:
newline_indent = ''
separator = _item_separator + newline_indent
buf += newline_indent
else:
Expand Down Expand Up @@ -326,7 +335,8 @@ def _iterencode_list(lst, _current_indent_level):
yield from chunks
if newline_indent is not None:
_current_indent_level -= 1
yield '\n' + _indent * _current_indent_level
if not list_oneline:
yield '\n' + _indent * _current_indent_level
yield ']'
if markers is not None:
del markers[markerid]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added new optional list_oneline argument to json.dump() and json.dumps()
which causes array output to be printed on a single line. Defaults to False
if not passed.