From 67ca2a499520d0cc50bdfbb800eba94c8974a266 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Thu, 10 Sep 2026 03:09:31 +0900 Subject: [PATCH] Allow usage of own json serializer Introduce a few internal interfaces to dump/load json so that users can use their own serializer by overriding these specific interfaces. Signed-off-by: Takashi Kajinami --- sqlalchemy_utils/types/json.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sqlalchemy_utils/types/json.py b/sqlalchemy_utils/types/json.py index 39d98da43..50af9732a 100644 --- a/sqlalchemy_utils/types/json.py +++ b/sqlalchemy_utils/types/json.py @@ -56,6 +56,12 @@ class Product(Base): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + def _dump_json(self, value): + return json.dumps(value) + + def _load_json(self, value): + return json.loads(value) + def load_dialect_impl(self, dialect): if dialect.name == 'postgresql': # Use the native JSON type. @@ -70,12 +76,12 @@ def process_bind_param(self, value, dialect): if dialect.name == 'postgresql' and has_postgres_json: return value if value is not None: - value = json.dumps(value) + value = self._dump_json(value) return value def process_result_value(self, value, dialect): if dialect.name == 'postgresql': return value if value is not None: - value = json.loads(value) + value = self._load_json(value) return value