Source code for sqlalchemy_mate.types.json_serializable
# -*- coding: utf-8 -*-"""Json serializable type."""importtypingtry:fromsuperjsonimportjsonexceptImportError:# pragma: no coverimportjsonexcept:# pragma: no coverimportjsonimportsqlalchemyassa
[docs]classJSONSerializableType(sa.types.TypeDecorator):""" This column store json serialized python object in form of This column should be a json serializable python type such as combination of list, dict, string, int, float, bool. Usage: import jsonpickle # a custom python class class ComputerDetails: def __init__(self, ...): ... def to_json(self) -> str: return jsonpickle.encode(self) @classmethod def from_json(cls, json_str: str) -> 'Computer': return cls(**jsonpickle.decode(json_str)) Base = declarative_base() class Computer(Base): id = Column(Integer, primary_key) details = Column(JSONSerializableType(factory_class=Computer) ... computer = Computer( id=1, details=ComputerDetails(...), ) with Session(engine) as session: session.add(computer) session.commit() computer = session.get(Computer, 1) print(computer.details) """impl=sa.UnicodeTextcache_ok=True_FACTORY_CLASS="factory_class"def__init__(self,*args,**kwargs):ifself._FACTORY_CLASSnotinkwargs:raiseValueError(("'JSONSerializableType' take only ONE argument {}, ""it is the generic type that has ``to_json(self): -> str``, ""and ``from_json(cls, value: str):`` class method.").format(self._FACTORY_CLASS))self.factory_class=kwargs.pop(self._FACTORY_CLASS)super(JSONSerializableType,self).__init__(*args,**kwargs)