本文整理汇总了Python中oslo_serialization.jsonutils.to_primitive方法的典型用法代码示例。如果您正苦于以下问题:Python jsonutils.to_primitive方法的具体用法?Python jsonutils.to_primitive怎么用?Python jsonutils.to_primitive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oslo_serialization.jsonutils
的用法示例。
在下文中一共展示了jsonutils.to_primitive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_to_primitive_recursive
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_to_primitive_recursive(self):
obj = TestObject(id=1, nested=TestObject(id=2))
# Ensure only the id attribute is returned
primitive = obj.to_primitive()
expected = {
'designate_object.name': 'TestObject',
'designate_object.data': {
'id': 1,
'nested': {
'designate_object.name': 'TestObject',
'designate_object.data': {
'id': 2,
},
'designate_object.changes': ['id'],
'designate_object.namespace': 'designate',
'designate_object.version': '1.0',
}
},
'designate_object.changes': ['id', 'nested'],
'designate_object.namespace': 'designate',
'designate_object.version': '1.0',
}
self.assertEqual(expected, primitive)
示例2: test_deepcopy
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_deepcopy(self):
# Create the Original object
o_obj = TestObject()
o_obj.id = "My ID"
o_obj.name = "My Name"
# Clear the "changed" flag for one of the two fields we set
o_obj.obj_reset_changes(['name'])
# Deepcopy the object
c_obj = copy.deepcopy(o_obj)
# Ensure the copy was successful
self.assertEqual(o_obj.id, c_obj.id)
self.assertEqual(o_obj.name, c_obj.name)
self.assertEqual(o_obj.obj_attr_is_set('nested'),
c_obj.obj_attr_is_set('nested'))
self.assertEqual(o_obj.obj_get_changes(), c_obj.obj_get_changes())
self.assertEqual(o_obj.to_primitive(), c_obj.to_primitive())
示例3: test_iteritems_with_cycle
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_iteritems_with_cycle(self):
class IterItemsClass(object):
def __init__(self):
self.data = dict(a=1, b=2, c=3)
self.index = 0
def iteritems(self):
return self.data.items()
x = IterItemsClass()
x2 = IterItemsClass()
x.data['other'] = x2
x2.data['other'] = x
# If the cycle isn't caught, to_primitive() will eventually result in
# an exception due to excessive recursion depth.
jsonutils.to_primitive(x)
示例4: test_mapping
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_mapping(self):
# Make sure collections.Mapping is converted to a dict
# and not a list.
class MappingClass(collections.Mapping):
def __init__(self):
self.data = dict(a=1, b=2, c=3)
def __getitem__(self, val):
return self.data[val]
def __iter__(self):
return iter(self.data)
def __len__(self):
return len(self.data)
x = MappingClass()
p = jsonutils.to_primitive(x)
self.assertEqual({'a': 1, 'b': 2, 'c': 3}, p)
示例5: test_depth
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_depth(self):
class LevelsGenerator(object):
def __init__(self, levels):
self._levels = levels
def iteritems(self):
if self._levels == 0:
return iter([])
else:
return iter([(0, LevelsGenerator(self._levels - 1))])
l4_obj = LevelsGenerator(4)
json_l2 = {0: {0: None}}
json_l3 = {0: {0: {0: None}}}
json_l4 = {0: {0: {0: {0: None}}}}
ret = jsonutils.to_primitive(l4_obj, max_depth=2)
self.assertEqual(json_l2, ret)
ret = jsonutils.to_primitive(l4_obj, max_depth=3)
self.assertEqual(json_l3, ret)
ret = jsonutils.to_primitive(l4_obj, max_depth=4)
self.assertEqual(json_l4, ret)
示例6: create_share_group
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def create_share_group(self, context, share_group_id, request_spec=None,
filter_properties=None):
"""Casts an rpc to the scheduler to create a share group.
Example of 'request_spec' argument value::
{
'share_group_type_id': 'fake_share_group_type_id',
'share_group_id': 'some_fake_uuid',
'availability_zone_id': 'some_fake_az_uuid',
'share_types': [models.ShareType],
'resource_type': models.ShareGroup,
}
"""
request_spec_p = jsonutils.to_primitive(request_spec)
call_context = self.client.prepare(version='1.8')
return call_context.cast(context,
'create_share_group',
share_group_id=share_group_id,
request_spec=request_spec_p,
filter_properties=filter_properties)
示例7: migrate_share_to_host
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def migrate_share_to_host(
self, context, share_id, host, force_host_assisted_migration,
preserve_metadata, writable, nondisruptive, preserve_snapshots,
new_share_network_id, new_share_type_id, request_spec=None,
filter_properties=None):
call_context = self.client.prepare(version='1.7')
request_spec_p = jsonutils.to_primitive(request_spec)
return call_context.cast(
context, 'migrate_share_to_host',
share_id=share_id,
host=host,
force_host_assisted_migration=force_host_assisted_migration,
preserve_metadata=preserve_metadata,
writable=writable,
nondisruptive=nondisruptive,
preserve_snapshots=preserve_snapshots,
new_share_network_id=new_share_network_id,
new_share_type_id=new_share_type_id,
request_spec=request_spec_p,
filter_properties=filter_properties)
示例8: serialize_entity
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def serialize_entity(context, entity):
return json.to_primitive(entity, convert_instances=True)
示例9: serialize_entity
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def serialize_entity(context, entity):
return jsonutils.to_primitive(entity, convert_instances=True)
示例10: test_to_primitive
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_to_primitive(self):
obj = TestObject(id=1)
# Ensure only the id attribute is returned
primitive = obj.to_primitive()
expected = {
'designate_object.name': 'TestObject',
'designate_object.data': {
'id': 1,
},
'designate_object.changes': ['id'],
'designate_object.namespace': 'designate',
'designate_object.version': '1.0',
}
self.assertEqual(expected, primitive)
# Set the name attribute to a None value
obj.name = None
# Ensure both the id and name attributes are returned
primitive = obj.to_primitive()
expected = {
'designate_object.name': 'TestObject',
'designate_object.data': {
'id': 1,
'name': None,
},
'designate_object.changes': ['id', 'name'],
'designate_object.namespace': 'designate',
'designate_object.version': '1.0',
}
self.assertEqual(expected, primitive)
示例11: test_to_primitive_nested_obj
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_to_primitive_nested_obj(self):
# Create a few objects
obj_one = TestObject()
obj_two = TestObject()
obj_two.id = "Two"
obj_one.nested = obj_two
# Create a ListObject
obj = TestObjectList(objects=[obj_one, obj_two])
primitive = obj.to_primitive()
expected = {
'designate_object.name': 'TestObjectList',
'designate_object.changes': ['objects'],
'designate_object.data': {
'objects': [
{'designate_object.changes': ['nested'],
'designate_object.data': {'nested':
{
'designate_object.changes': [
'id'],
'designate_object.data': {
'id': 'Two'},
'designate_object.name': 'TestObject',
'designate_object.namespace': 'designate',
'designate_object.version': '1.0'}},
'designate_object.name': 'TestObject',
'designate_object.namespace': 'designate',
'designate_object.version': '1.0'},
{'designate_object.changes': ['id'],
'designate_object.data': {'id': 'Two'},
'designate_object.name': 'TestObject',
'designate_object.namespace': 'designate',
'designate_object.version': '1.0'}]},
'designate_object.namespace': 'designate',
'designate_object.version': '1.0'}
self.assertEqual(expected, primitive)
示例12: __init__
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def __init__(self, default=jsonutils.to_primitive, encoding='utf-8'):
self._default = default
self._encoding = encoding
示例13: test_dumps_default
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_dumps_default(self):
args = [ReprObject()]
convert = functools.partial(jsonutils.to_primitive, fallback=repr)
self.assertEqual('["repr"]', jsonutils.dumps(args, default=convert))
示例14: test_bytes
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_bytes(self):
self.assertEqual(jsonutils.to_primitive(b'abc'), 'abc')
示例15: test_list
# 需要导入模块: from oslo_serialization import jsonutils [as 别名]
# 或者: from oslo_serialization.jsonutils import to_primitive [as 别名]
def test_list(self):
self.assertEqual([1, 2, 3], jsonutils.to_primitive([1, 2, 3]))