コード例 #1
0
ファイル: json.py プロジェクト: watsonpy/watson-common
def deserialize(obj, class_, attributes, strategies=None, snakecase=True,
                **kwargs):
    """Deserializes a dict into an object of type class_.

    Can be seen as the inverse of serialize().

    Args:
        obj (dict): The structure to deserialize
        class_ (class): The type that the object should be deserialized into
        attributes (tuple): A tuple of attributes that should be set
        strategies (dict): Key/value pairs of strategies to deal with objects
        snakecase (bool): snake_case the attribute names
        kwargs: Passed to the strategy when it's called
    """
    deserialized = class_()
    for attr in attributes:
        if snakecase:
            # camelcase the required attr
            attr = strings.camelcase(attr, uppercase=False)
        value = obj.get(attr)
        if value is None:
            continue
        if strategies and attr in strategies:
            value = strategies[attr](value, **kwargs)
        if snakecase:
            attr = strings.snakecase(attr)
        setattr(deserialized, attr, value)
    return deserialized
コード例 #2
0
ファイル: json.py プロジェクト: AndydeCleyre/watson-common
def deserialize(obj,
                class_,
                attributes,
                strategies=None,
                snakecase=True,
                **kwargs):
    """Deserializes a dict into an object of type class_.

    Can be seen as the inverse of serialize().

    Args:
        obj (dict): The structure to deserialize
        class_ (class): The type that the object should be deserialized into
        attributes (tuple): A tuple of attributes that should be set
        strategies (dict): Key/value pairs of strategies to deal with objects
        snakecase (bool): snake_case the attribute names
        kwargs: Passed to the strategy when it's called
    """
    deserialized = class_()
    for attr in attributes:
        if snakecase:
            # camelcase the required attr
            attr = strings.camelcase(attr, uppercase=False)
        value = obj.get(attr)
        if value is None:
            continue
        if strategies and attr in strategies:
            value = strategies[attr](value, **kwargs)
        if snakecase:
            attr = strings.snakecase(attr)
        setattr(deserialized, attr, value)
    return deserialized
コード例 #3
0
ファイル: json.py プロジェクト: watsonpy/watson-common
 def _camelize_attributes(self, o):
     if isinstance(o, dict):
         new_o = {}
         for attr in o:
             camelized_attr = strings.camelcase(attr, uppercase=False)
             new_o[camelized_attr] = self._camelize(o[attr])
         return new_o
     return o
コード例 #4
0
ファイル: json.py プロジェクト: watsonpy/watson-common
def serialize(obj, attributes, strategies=None, camelcase=True, **kwargs):
    """Serializes an object into a dict suitable to be dumped to json.

    Args:
        obj (mixed): The object to serialize
        attributes (tuple): A tuple of attributes that should be serialized
        strategies (dict): Key/value pairs of strategies to deal with objects
        camelcase (bool): camelCase the attribute names
        kwargs: Passed to the strategy when it's called

    Example:

    .. code-block:: python

        class AnotherClass(object):
            name = 'Else'

        class MyClass(object):
            name = 'Something'
            complex_classes = [AnotherClass()]

        class_ = MyClass()
        d = serialize(class_, ('name', 'complex'),
                       strategies={
                            'complex_class': lambda x:
                                                serialize(y, ('name',))
                                                for y in x})
        # {'name': 'Something', 'complexClass': {'name': 'Else'}}
    """
    serialized = {}
    for attr in attributes:
        value = getattr(obj, attr)
        if value is None:
            continue
        if strategies and attr in strategies:
            value = strategies[attr](value, **kwargs)
        if camelcase:
            attr = strings.camelcase(attr, uppercase=False)
        serialized[attr] = value
    return serialized
コード例 #5
0
ファイル: json.py プロジェクト: AndydeCleyre/watson-common
def serialize(obj, attributes, strategies=None, camelcase=True, **kwargs):
    """Serializes an object into a dict suitable to be dumped to json.

    Args:
        obj (mixed): The object to serialize
        attributes (tuple): A tuple of attributes that should be serialized
        strategies (dict): Key/value pairs of strategies to deal with objects
        camelcase (bool): camelCase the attribute names
        kwargs: Passed to the strategy when it's called

    Example:

    .. code-block:: python

        class AnotherClass(object):
            name = 'Else'

        class MyClass(object):
            name = 'Something'
            complex_classes = [AnotherClass()]

        class_ = MyClass()
        d = serialize(class_, ('name', 'complex'),
                       strategies={
                            'complex_class': lambda x:
                                                serialize(y, ('name',))
                                                for y in x})
        # {'name': 'Something', 'complexClass': {'name': 'Else'}}
    """
    serialized = {}
    for attr in attributes:
        value = getattr(obj, attr)
        if value is None:
            continue
        if strategies and attr in strategies:
            value = strategies[attr](value, **kwargs)
        if camelcase:
            attr = strings.camelcase(attr, uppercase=False)
        serialized[attr] = value
    return serialized
コード例 #6
0
 def test_camelize(self):
     assert strings.camelcase('something_else', uppercase=False) == 'somethingElse'
     assert strings.camelcase('something_else') == 'SomethingElse'
コード例 #7
0
 def test_camelize(self):
     assert strings.camelcase('something_else',
                              uppercase=False) == 'somethingElse'
     assert strings.camelcase('something_else') == 'SomethingElse'