Files
azul/util/json_type.py
T
Athena Funderburg 4b463a3432 init
2026-05-25 07:05:17 +00:00

25 lines
680 B
Python

from typing import Any
import json
from sqlalchemy import types
from sqlalchemy.dialects import postgresql
class JSONType(types.TypeDecorator): # type: ignore
impl = types.TEXT
def load_dialect_impl(self, dialect: Any) -> Any:
if dialect.name == 'postgresql':
t = postgresql.JSON()
else:
t = types.TEXT()
return dialect.type_descriptor(t)
def process_bind_param(self, value: Any, dialect: Any) -> Any:
if value is None or dialect.name == 'postgresql':
return value
return json.dumps(value)
def process_result_value(self, value: Any, dialect: Any) -> Any:
if value is None or dialect.name == 'postgresql':
return value
return json.loads(value)