Use default in dumps()
There's a different behaviour currently between dump() and dumps() as by default dumps() use the to_primitive function provided by this module, and dump() does not. This patch fixes that. Change-Id: I8a116e3777901bc713e9812c1a654a51b43e702a
This commit is contained in:
parent
3b98d5239a
commit
2c244b288e
|
@ -191,14 +191,16 @@ def dump(obj, fp, *args, **kwargs):
|
|||
|
||||
:param obj: object to be serialized
|
||||
:param fp: a ``.write()``-supporting file-like object
|
||||
:param default: function that returns a serializable version of an object
|
||||
:param args: extra arguments, please see documentation \
|
||||
of `json.dump <https://docs.python.org/2/library/json.html#basic-usage>`_
|
||||
:param kwargs: extra named parameters, please see documentation \
|
||||
of `json.dump <https://docs.python.org/2/library/json.html#basic-usage>`_
|
||||
"""
|
||||
default = kwargs.get('default', to_primitive)
|
||||
if is_simplejson:
|
||||
kwargs['namedtuple_as_object'] = False
|
||||
return json.dump(obj, fp, *args, **kwargs)
|
||||
return json.dump(obj, fp, default=default, *args, **kwargs)
|
||||
|
||||
|
||||
def loads(s, encoding='utf-8', **kwargs):
|
||||
|
|
|
@ -262,6 +262,17 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
|||
ret = jsonutils.to_primitive(thing)
|
||||
self.assertEqual({'ip_addr': '1.2.3.4'}, ret)
|
||||
|
||||
def test_dumps_ipaddr(self):
|
||||
thing = {'ip_addr': netaddr.IPAddress('1.2.3.4')}
|
||||
ret = jsonutils.dumps(thing)
|
||||
self.assertEqual('{"ip_addr": "1.2.3.4"}', ret)
|
||||
|
||||
def test_dump_ipaddr(self):
|
||||
thing = {'ip_addr': netaddr.IPAddress('1.2.3.4')}
|
||||
fp = six.StringIO()
|
||||
jsonutils.dump(thing, fp)
|
||||
self.assertEqual('{"ip_addr": "1.2.3.4"}', fp.getvalue())
|
||||
|
||||
def test_message_with_param(self):
|
||||
msg = self.trans_fixture.lazy('A message with param: %s')
|
||||
msg = msg % 'test_domain'
|
||||
|
|
Loading…
Reference in New Issue