From 071448fb6a6898c8b56389a0e65549a899a24770 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Tue, 8 Jul 2008 09:38:16 +0000 Subject: [PATCH] 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 --- lib/serializer.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/serializer.py b/lib/serializer.py index c5a8b0d39..05bc33498 100644 --- a/lib/serializer.py +++ b/lib/serializer.py @@ -28,6 +28,7 @@ backend (currently json). import simplejson import re + # Check whether the simplejson module supports indentation _JSON_INDENT = 2 try: @@ -38,14 +39,18 @@ except TypeError: _RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE) -def DumpJson(data): +def DumpJson(data, indent=True): """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) else: txt = simplejson.dumps(data, indent=_JSON_INDENT) + txt = _RE_EOLSP.sub("", txt) if not txt.endswith('\n'): txt += '\n' -- GitLab