import json from app import db class ContainerColumn(db.TypeDecorator): impl = db.String def __init__(self, container_type, *args, **kwargs): super().__init__(*args, **kwargs) self.container_type = container_type def process_bind_param(self, value, dialect): if isinstance(value, self.container_type): return json.dumps(value) elif isinstance(value, str) and isinstance(json.loads(value), self.container_type): return value else: return TypeError() def process_result_value(self, value, dialect): return json.loads(value)