1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2023 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Common json type for different dialects.
 
  10 from typing import Any
 
  12 import sqlalchemy as sa
 
  13 from sqlalchemy.dialects.postgresql import JSONB
 
  14 from sqlalchemy.dialects.sqlite import JSON as sqlite_json
 
  16 from nominatim.typing import SaDialect
 
  20 class Json(sa.types.TypeDecorator[Any]):
 
  21     """ Dialect-independent type for JSON.
 
  26     def load_dialect_impl(self, dialect: SaDialect) -> sa.types.TypeEngine[Any]:
 
  27         if dialect.name == 'postgresql':
 
  28             return JSONB(none_as_null=True) # type: ignore[no-untyped-call]
 
  30         return sqlite_json(none_as_null=True)