"""
import simplejson
-import ConfigParser
import re
+
# Check whether the simplejson module supports indentation
_JSON_INDENT = 2
try:
except TypeError:
_JSON_INDENT = None
-_RE_EOLSP = re.compile('\s+$', re.MULTILINE)
+_RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE)
-def DumpJson(data):
+def DumpJson(data, indent=True):
"""Serialize a given object.
+ @param data: the data to serialize
+ @param indent: whether to indent output (depends on simplejson version)
+
+ @return: the string representation of data
+
"""
- 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'
- txt = _RE_EOLSP.sub("", txt)
return txt
def LoadJson(txt):
"""Unserialize data from a string.
+ @param txt: the json-encoded form
+
+ @return: the original data
+
"""
return simplejson.loads(txt)