Revision 02141fb1 lib/serializer.py
b/lib/serializer.py | ||
---|---|---|
42 | 42 |
_RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE) |
43 | 43 |
|
44 | 44 |
|
45 |
def _GetJsonDumpers(): |
|
45 |
def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder):
|
|
46 | 46 |
"""Returns two JSON functions to serialize data. |
47 | 47 |
|
48 | 48 |
@rtype: (callable, callable) |
... | ... | |
50 | 50 |
generate a more readable, indented form of JSON (if supported) |
51 | 51 |
|
52 | 52 |
""" |
53 |
plain_dump = simplejson.dumps
|
|
53 |
plain_encoder = _encoder_class(sort_keys=True)
|
|
54 | 54 |
|
55 | 55 |
# Check whether the simplejson module supports indentation |
56 | 56 |
try: |
57 |
simplejson.dumps(1, indent=_JSON_INDENT)
|
|
57 |
indent_encoder = _encoder_class(indent=_JSON_INDENT, sort_keys=True)
|
|
58 | 58 |
except TypeError: |
59 | 59 |
# Indentation not supported |
60 |
indent_dump = plain_dump |
|
61 |
else: |
|
62 |
# Indentation supported |
|
63 |
indent_dump = lambda data: simplejson.dumps(data, indent=_JSON_INDENT) |
|
64 |
|
|
65 |
assert callable(plain_dump) |
|
66 |
assert callable(indent_dump) |
|
60 |
indent_encoder = plain_encoder |
|
67 | 61 |
|
68 |
return (plain_dump, indent_dump)
|
|
62 |
return (plain_encoder.encode, indent_encoder.encode)
|
|
69 | 63 |
|
70 | 64 |
|
71 | 65 |
(_DumpJson, _DumpJsonIndent) = _GetJsonDumpers() |
Also available in: Unified diff