Added missing jsonutils.dump() function

The function is missing from the module, meaning that some module
consumers can't use jsonutils as a complete replacement for stdlib json
module.

Change-Id: Ia678d251a3984c959ae6c9d7ad70d33b197da4ff
Closes-Bug: 1338571
This commit is contained in:
Ihar Hrachyshka 2014-07-07 15:02:10 +02:00
parent 56336df6e1
commit 8668f9c1a4
2 changed files with 13 additions and 0 deletions

View File

@ -168,6 +168,10 @@ def dumps(value, default=to_primitive, **kwargs):
return json.dumps(value, default=default, **kwargs)
def dump(obj, fp, *args, **kwargs):
return json.dump(obj, fp, *args, **kwargs)
def loads(s, encoding='utf-8', **kwargs):
return json.loads(strutils.safe_decode(s, encoding), **kwargs)

View File

@ -44,6 +44,15 @@ class JSONUtilsTestMixin(object):
def test_dumps(self):
self.assertEqual('{"a": "b"}', jsonutils.dumps({'a': 'b'}))
def test_dump(self):
expected = '{"a": "b"}'
json_dict = {'a': 'b'}
fp = six.StringIO()
jsonutils.dump(json_dict, fp)
self.assertEqual(expected, fp.getvalue())
def test_loads(self):
self.assertEqual({'a': 'b'}, jsonutils.loads('{"a": "b"}'))