Commit 02141fb1 authored by Michael Hanselmann's avatar Michael Hanselmann

Cache JSON encoders and sort keys

The sort_keys argument is supported since simplejson 1.3.
Signed-off-by: default avatarMichael Hanselmann <>
Reviewed-by: default avatarGuido Trotter <>
parent f154a7a3
......@@ -42,7 +42,7 @@ _JSON_INDENT = 2
_RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE)
def _GetJsonDumpers():
def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder):
"""Returns two JSON functions to serialize data.
@rtype: (callable, callable)
......@@ -50,22 +50,16 @@ def _GetJsonDumpers():
generate a more readable, indented form of JSON (if supported)
plain_dump = simplejson.dumps
plain_encoder = _encoder_class(sort_keys=True)
# Check whether the simplejson module supports indentation
simplejson.dumps(1, indent=_JSON_INDENT)
indent_encoder = _encoder_class(indent=_JSON_INDENT, sort_keys=True)
except TypeError:
# Indentation not supported
indent_dump = plain_dump
# Indentation supported
indent_dump = lambda data: simplejson.dumps(data, indent=_JSON_INDENT)
assert callable(plain_dump)
assert callable(indent_dump)
indent_encoder = plain_encoder
return (plain_dump, indent_dump)
return (plain_encoder.encode, indent_encoder.encode)
(_DumpJson, _DumpJsonIndent) = _GetJsonDumpers()
......@@ -52,15 +52,24 @@ class TestSerializer(testutils.GanetiTestCase):
def _TestSerializer(self, dump_fn, load_fn):
for data in self._TESTDATA:
self.assertEqualValues(load_fn(dump_fn(data)), data)
for indent in [True, False]:
for data in self._TESTDATA:
self.failUnless(dump_fn(data, indent=indent).endswith("\n"))
self.assertEqualValues(load_fn(dump_fn(data, indent=indent)), data)
def testGeneric(self):
return self._TestSerializer(serializer.Dump, serializer.Load)
self._TestSerializer(serializer.Dump, serializer.Load)
def testJson(self):
return self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
def testJsonIndent(self):
data = {
"k1": 1,
"k2": 3,
"k3": 4,
self.assert_(len(serializer.DumpJson(data, indent=True).splitlines()) > 3)
def testSignedMessage(self):
LoadSigned = serializer.LoadSigned
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment