Add netaddr.IPAddress support to to_primitive()
This enlightens jsonutils.to_primitive() about how to serialize netaddr.IPAddress objects. Since these types are serializable in their native form with just string coercion, just do that here. Fixes bug 1195097 Change-Id: I358b0731e4d3774e5b36aefb5384fdfb75fd31b6
This commit is contained in:
@@ -41,6 +41,7 @@ import json
|
||||
import types
|
||||
import xmlrpclib
|
||||
|
||||
import netaddr
|
||||
import six
|
||||
|
||||
from openstack.common import timeutils
|
||||
@@ -137,6 +138,8 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
||||
# Likely an instance of something. Watch for cycles.
|
||||
# Ignore class member vars.
|
||||
return recursive(value.__dict__, level=level + 1)
|
||||
elif isinstance(value, netaddr.IPAddress):
|
||||
return six.text_type(value)
|
||||
else:
|
||||
if any(test(value) for test in _nasty_type_tests):
|
||||
return six.text_type(value)
|
||||
|
||||
@@ -15,3 +15,4 @@ SQLAlchemy>=0.7.8,<=0.7.9
|
||||
http://tarballs.openstack.org/oslo.config/oslo.config-1.2.0a2.tar.gz#egg=oslo.config-1.2.0a2
|
||||
qpid-python
|
||||
six
|
||||
netaddr
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
import datetime
|
||||
import xmlrpclib
|
||||
|
||||
import netaddr
|
||||
from six import StringIO
|
||||
|
||||
from openstack.common import jsonutils
|
||||
@@ -170,3 +171,8 @@ class ToPrimitiveTestCase(utils.BaseTestCase):
|
||||
|
||||
ret = jsonutils.to_primitive(l4_obj, max_depth=4)
|
||||
self.assertEquals(ret, json_l4)
|
||||
|
||||
def test_ipaddr(self):
|
||||
thing = {'ip_addr': netaddr.IPAddress('1.2.3.4')}
|
||||
ret = jsonutils.to_primitive(thing)
|
||||
self.assertEquals({'ip_addr': '1.2.3.4'}, ret)
|
||||
|
||||
Reference in New Issue
Block a user