Skip to content
Snippets Groups Projects
Commit 071448fb authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

serializer.DumpJson: Control indentation by parameter

If the simplejson module supports indentation, it's always used. There
are cases where we might not want to use it or enable it only for
debugging purposes, such as in RPC.

Reviewed-by: iustinp
parent 6048c986
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ backend (currently json). ...@@ -28,6 +28,7 @@ backend (currently json).
import simplejson import simplejson
import re import re
# Check whether the simplejson module supports indentation # Check whether the simplejson module supports indentation
_JSON_INDENT = 2 _JSON_INDENT = 2
try: try:
...@@ -38,14 +39,18 @@ except TypeError: ...@@ -38,14 +39,18 @@ except TypeError:
_RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE) _RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE)
def DumpJson(data): def DumpJson(data, indent=True):
"""Serialize a given object. """Serialize a given object.
Args:
- indent: Whether to indent output (depends on simplejson version)
""" """
if _JSON_INDENT is None: if not indent or _JSON_INDENT is None:
txt = simplejson.dumps(data) txt = simplejson.dumps(data)
else: else:
txt = simplejson.dumps(data, indent=_JSON_INDENT) txt = simplejson.dumps(data, indent=_JSON_INDENT)
txt = _RE_EOLSP.sub("", txt) txt = _RE_EOLSP.sub("", txt)
if not txt.endswith('\n'): if not txt.endswith('\n'):
txt += '\n' txt += '\n'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment