Python oslo_serialization.jsonutils.to_primitive() Examples

The following are 30 code examples of oslo_serialization.jsonutils.to_primitive(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module oslo_serialization.jsonutils , or try the search function .
Example #1
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 6 votes vote down vote up
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) 
Example #2
Source File: test_base.py    From designate with Apache License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: test_base.py    From designate with Apache License 2.0 6 votes vote down vote up
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()) 
Example #4
Source File: rpcapi.py    From manila with Apache License 2.0 6 votes vote down vote up
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) 
Example #5
Source File: rpcapi.py    From manila with Apache License 2.0 6 votes vote down vote up
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) 
Example #6
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 6 votes vote down vote up
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) 
Example #7
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 6 votes vote down vote up
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) 
Example #8
Source File: rpcapi.py    From manila with Apache License 2.0 5 votes vote down vote up
def create_share_replica(self, context, share_replica, host,
                             request_spec, filter_properties):
        new_host = utils.extract_host(host)
        call_context = self.client.prepare(server=new_host, version='1.8')
        request_spec_p = jsonutils.to_primitive(request_spec)
        call_context.cast(context,
                          'create_share_replica',
                          share_replica_id=share_replica['id'],
                          request_spec=request_spec_p,
                          filter_properties=filter_properties,
                          share_id=share_replica['share_id']) 
Example #9
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def test_message_with_param(self):
        msg = self.trans_fixture.lazy('A message with param: %s')
        msg = msg % 'test_domain'
        ret = jsonutils.to_primitive(msg)
        self.assertEqual(msg, ret) 
Example #10
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def test_message_with_named_param(self):
        msg = self.trans_fixture.lazy('A message with params: %(param)s')
        msg = msg % {'param': 'hello'}
        ret = jsonutils.to_primitive(msg)
        self.assertEqual(msg, ret) 
Example #11
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def test_fallback(self):
        obj = ReprObject()

        ret = jsonutils.to_primitive(obj)
        self.assertIs(obj, ret)

        ret = jsonutils.to_primitive(obj, fallback=repr)
        self.assertEqual('repr', ret) 
Example #12
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def test_fallback_list(self):
        obj = ReprObject()
        obj_list = [obj]

        ret = jsonutils.to_primitive(obj_list)
        self.assertEqual([obj], ret)

        ret = jsonutils.to_primitive(obj_list, fallback=repr)
        self.assertEqual(['repr'], ret) 
Example #13
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def test_fallback_nasty(self):
        obj = int
        ret = jsonutils.to_primitive(obj)
        self.assertEqual(str(obj), ret)

        def formatter(typeobj):
            return 'type:%s' % typeobj.__name__
        ret = jsonutils.to_primitive(obj, fallback=formatter)
        self.assertEqual("type:int", ret) 
Example #14
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def test_fallback_typeerror(self):
        class NotIterable(object):
            # __iter__ is not callable, cause a TypeError in to_primitive()
            __iter__ = None

        obj = NotIterable()

        ret = jsonutils.to_primitive(obj)
        self.assertEqual(str(obj), ret)

        ret = jsonutils.to_primitive(obj, fallback=lambda _: 'fallback')
        self.assertEqual('fallback', ret) 
Example #15
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def test_exception(self):
        self.assertIn(jsonutils.to_primitive(ValueError("an exception")),
                      ["ValueError('an exception',)",
                       "ValueError('an exception')"]) 
Example #16
Source File: wsgi.py    From searchlight with Apache License 2.0 5 votes vote down vote up
def _sanitizer(self, obj):
        """Sanitizer method that will be passed to jsonutils.dumps."""
        if hasattr(obj, "to_dict"):
            return obj.to_dict()
        if isinstance(obj, multidict.MultiDict):
            return obj.mixed()
        return jsonutils.to_primitive(obj) 
Example #17
Source File: rpc.py    From magnum with Apache License 2.0 5 votes vote down vote up
def serialize_entity(context, entity):
        return jsonutils.to_primitive(entity, convert_instances=True) 
Example #18
Source File: rpcapi.py    From manila with Apache License 2.0 5 votes vote down vote up
def create_share_instance(self, context, share_instance, host,
                              request_spec, filter_properties,
                              snapshot_id=None):
        new_host = utils.extract_host(host)
        call_context = self.client.prepare(server=new_host, version='1.4')
        request_spec_p = jsonutils.to_primitive(request_spec)
        call_context.cast(context,
                          'create_share_instance',
                          share_instance_id=share_instance['id'],
                          request_spec=request_spec_p,
                          filter_properties=filter_properties,
                          snapshot_id=snapshot_id) 
Example #19
Source File: utils.py    From coriolis with GNU Affero General Public License v3.0 5 votes vote down vote up
def to_dict(obj, max_depth=10):
    # jsonutils.dumps() has a max_depth of 3 by default
    def _to_primitive(value, convert_instances=False,
                      convert_datetime=True, level=0,
                      max_depth=max_depth):
        return jsonutils.to_primitive(
            value, convert_instances, convert_datetime, level, max_depth)
    return jsonutils.loads(jsonutils.dumps(obj, default=_to_primitive)) 
Example #20
Source File: rpc.py    From manila with Apache License 2.0 5 votes vote down vote up
def serialize_entity(context, entity):
        return jsonutils.to_primitive(entity, convert_instances=True) 
Example #21
Source File: test_rpcapi.py    From manila with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(ShareRpcAPITestCase, self).setUp()
        share = db_utils.create_share(
            availability_zone=CONF.storage_availability_zone,
            status=constants.STATUS_AVAILABLE
        )
        snapshot = db_utils.create_snapshot(share_id=share['id'])
        share_replica = db_utils.create_share_replica(
            id='fake_replica',
            share_id='fake_share_id',
            host='fake_host',
        )
        share_server = db_utils.create_share_server()
        share_group = {'id': 'fake_share_group_id', 'host': 'fake_host'}
        share_group_snapshot = {'id': 'fake_share_group_id'}
        host = 'fake_host'
        self.fake_share = jsonutils.to_primitive(share)
        # mock out the getattr on the share db model object since jsonutils
        # doesn't know about those extra attributes to pull in
        self.fake_share['instance'] = jsonutils.to_primitive(share.instance)
        self.fake_share_replica = jsonutils.to_primitive(share_replica)
        self.fake_snapshot = jsonutils.to_primitive(snapshot)
        self.fake_snapshot['share_instance'] = jsonutils.to_primitive(
            snapshot.instance)
        self.fake_share_server = jsonutils.to_primitive(share_server)
        self.fake_share_group = jsonutils.to_primitive(share_group)
        self.fake_share_group_snapshot = jsonutils.to_primitive(
            share_group_snapshot)
        self.fake_host = jsonutils.to_primitive(host)
        self.ctxt = context.RequestContext('fake_user', 'fake_project')
        self.rpcapi = share_rpcapi.ShareAPI() 
Example #22
Source File: test_rpcapi.py    From manila with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(DataRpcAPITestCase, self).setUp()
        share = db_utils.create_share(
            availability_zone=CONF.storage_availability_zone,
            status=constants.STATUS_AVAILABLE
        )
        self.fake_share = jsonutils.to_primitive(share) 
Example #23
Source File: rpcapi.py    From manila with Apache License 2.0 5 votes vote down vote up
def create_share_instance(self, context, request_spec=None,
                              filter_properties=None):
        request_spec_p = jsonutils.to_primitive(request_spec)
        call_context = self.client.prepare(version='1.2')
        return call_context.cast(context,
                                 'create_share_instance',
                                 request_spec=request_spec_p,
                                 filter_properties=filter_properties) 
Example #24
Source File: rpcapi.py    From manila with Apache License 2.0 5 votes vote down vote up
def create_share_replica(self, context, request_spec=None,
                             filter_properties=None):
        request_spec_p = jsonutils.to_primitive(request_spec)
        call_context = self.client.prepare(version='1.5')
        return call_context.cast(context,
                                 'create_share_replica',
                                 request_spec=request_spec_p,
                                 filter_properties=filter_properties) 
Example #25
Source File: formatters.py    From oslo.log with Apache License 2.0 5 votes vote down vote up
def _json_dumps_with_fallback(obj):
    # Bug #1593641: If an object cannot be serialized to JSON, convert
    # it using repr() to prevent serialization errors. Using repr() is
    # not ideal, but serialization errors are unexpected on logs,
    # especially when the code using logs is not aware that the
    # JSONFormatter will be used.
    convert = functools.partial(jsonutils.to_primitive, fallback=repr)
    return jsonutils.dumps(obj, default=convert) 
Example #26
Source File: rpc.py    From masakari with Apache License 2.0 5 votes vote down vote up
def serialize_entity(context, entity):
        return jsonutils.to_primitive(entity, convert_instances=True) 
Example #27
Source File: fake_notifier.py    From masakari with Apache License 2.0 5 votes vote down vote up
def _notify(self, priority, ctxt, event_type, payload):
        payload = self._serializer.serialize_entity(ctxt, payload)
        # NOTE(Dinesh_Bhor): simulate the kombu serializer
        # this permit to raise an exception if something have not
        # been serialized correctly
        jsonutils.to_primitive(payload)
        # NOTE(Dinesh_Bhor): Try to serialize the context, as the rpc would.
        #                An exception will be raised if something is wrong
        #                with the context.
        self._serializer.serialize_context(ctxt)
        msg = FakeMessage(self.publisher_id, priority, event_type,
                          payload, ctxt)
        NOTIFICATIONS.append(msg) 
Example #28
Source File: rpc.py    From zun with Apache License 2.0 5 votes vote down vote up
def serialize_entity(context, entity):
        return json.to_primitive(entity, convert_instances=True) 
Example #29
Source File: test_jsonutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def test_empty_dict(self):
        self.assertEqual({}, jsonutils.to_primitive({})) 
Example #30
Source File: rpc.py    From designate with Apache License 2.0 5 votes vote down vote up
def serialize_entity(context, entity):
        return jsonutils.to_primitive(entity, convert_instances=True)