Source code for aiocache.serializers

import logging
import pickle

logger = logging.getLogger(__file__)

try:
    import ujson as json
except ImportError:
    logger.warning("ujson module not found, usin json")
    import json


[docs]class NullSerializer: """ This serializer does nothing. Its only recommended to be used by :class:`aiocache.SimpleMemoryCache` because for other backends it will produce incompatible data unless you work only with str types. DISCLAIMER: Be careful with mutable types and memory storage. The following behavior is considered normal (same as ``functools.lru_cache``):: cache = SimpleMemoryCache() my_list = [1] await cache.set("key", my_list) my_list.append(2) await cache.get("key") # Will return [1, 2] """ encoding = 'utf-8'
[docs] @classmethod def dumps(cls, value): """ Returns the same value """ return value
[docs] @classmethod def loads(cls, value): """ Returns the same value """ return value
[docs]class StringSerializer: """ Converts all input values to str. All return values are also str. Be careful because this means that if you store an ``int(1)``, you will get back '1'. The transformation is done by just casting to str in the ``dumps`` method. If you want to keep python types, use ``PickleSerializer``. ``JsonSerializer`` may also be useful to keep type of symple python types. """ encoding = 'utf-8' def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)
[docs] @classmethod def dumps(cls, value): """ Serialize the received value casting it to str. :param value: obj Anything support cast to str :returns: str """ return str(value)
[docs] @classmethod def loads(cls, value): """ Returns value back without transformations """ return value
[docs]class PickleSerializer(StringSerializer): """ Transform data to bytes using pickle.dumps and pickle.loads to retrieve it back. """ encoding = None
[docs] @classmethod def dumps(cls, value): """ Serialize the received value using ``pickle.dumps``. :param value: obj :returns: bytes """ return pickle.dumps(value)
[docs] @classmethod def loads(cls, value): """ Deserialize value using ``pickle.loads``. :param value: bytes :returns: obj """ if value is None: return None return pickle.loads(value)
[docs]class JsonSerializer(StringSerializer): """ Transform data to json string with json.dumps and json.loads to retrieve it back. Check https://docs.python.org/3/library/json.html#py-to-json-table for how types are converted. """
[docs] @classmethod def dumps(cls, value): """ Serialize the received value using ``json.dumps``. :param value: dict :returns: str """ return json.dumps(value)
[docs] @classmethod def loads(cls, value): """ Deserialize value using ``json.loads``. :param value: str :returns: output of ``json.loads``. """ if value is None: return None return json.loads(value)