[issue29540] Add compact=True flag to json.dump/dumps

2017-02-12 Thread Alex Gordon

New submission from Alex Gordon:

Broadly speaking, there are three main output styles for json.dump/dumps:

1. Default: json.dumps(obj)
2. Compact: json.dumps(obj, separators=(',', ':'))
3. Pretty-printing: json.dumps(obj, sort_keys=True, indent=4)

The 'compact' style is the densest, suitable if the JSON is to be sent over the 
network, archived on disk, or otherwise consumed by a machine. The 
pretty-printed style is for human consumption: configuration files, debugging, 
etc.

Even though the compact style is often desirable, the API for producing it is 
unforgiving. It's easy to accidentally write code like the following, which 
silently produces invalid nonsense:

json.dumps(obj, separators=(':', ','))

I propose the addition of a new flag `compact=True`, that simply sets 
`separators=(',', ':')`. e.g.

>>> obj = {"foo": 1, "bar": 2}
>>> json.dumps(obj, compact=True)
'{"foo":1,"bar":2}'

The defaults for `separators=` are good, so eventually `compact=` would 
relegate `separators=` to obscurity. Setting both `compact=True` and 
`separators=` at the same time should be an error.

--
components: Library (Lib)
messages: 287663
nosy: Alex Gordon
priority: normal
severity: normal
status: open
title: Add compact=True flag to json.dump/dumps
type: enhancement
versions: Python 3.7

___
Python tracker 
<http://bugs.python.org/issue29540>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29540] Add compact=True flag to json.dump/dumps

2017-02-14 Thread Alex Gordon

Alex Gordon added the comment:

The point is that, as a principle of good API design, the json module should 
not generate malformed JSON unless the user very explicitly asks for their JSON 
to be corrupted.

Python stands alone in having a JSON serializer that can produce strings such 
as {"k",[1:2:3]} if the user holds it wrong.

Outside of Python, the 'compact' encoding is just the normal way that JSON is 
encoded. It's the default almost everywhere else. I'm not suggesting Python 
should default to it also, but I am suggesting that it should be safe and easy 
to remove the extraneous whitespace.

Historically there were two values you might want to pass to separators. Aside 
from (',', ':'), the other was (',', ': ') when indent was not None, to 
suppress the trailing space at the end of each line. This is no longer 
necessary after 3.4.

After 3.4, separators= currently acts as a very complicated boolean flag, 
because the only value that makes sense to pass to it is (',', ':'). With a 
compact flag, users could ignore separators entirely and so the API would be 
made simpler and safer.

--

___
Python tracker 
<http://bugs.python.org/issue29540>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com