Formatted JSON output

A module that provides a custom JSON encoder for JSON-like data structures, that is more compact than the default encoder but still readable.

class CompactEncoder(*args: Any, **kwargs: Any)[source]

Bases: JSONEncoder

JSONEncoder can be used as cls argument to json.dump and json.dumps. It creates formatted JSON strings, with indentation that are more compact than the dafault formatting from json module. The main difference is that the lists of primitives are not split into multiple lines.

encode(o: Any) str[source]

Return a JSON string representation of a Python data structure.

Example

>>> CompactEncoder().encode({"foo": ["bar", "baz"]})
'{\n\t"foo": ["bar", "baz"]\n}'
iterencode(o: Any, *args: Any) Iterator[str][source]

Encode the given object and yield each string representation line by line.

Example

>>> item = {"foo": ["bar", "baz"]}
>>> ''.join(list(CompactEncoder().iterencode(item))) == \
... CompactEncoder().encode(item)
True