class JSON(sqltypes.TypeEngine):
- """Represent the Postgresql HSTORE type.
+ """Represent the Postgresql JSON type.
The :class:`.JSON` type stores arbitrary JSON format data, e.g.::
data_table.c.data.get_path('{key_1, key_2, ..., key_n}']
- Please be aware that when used with the SQL Alchemy ORM, you will need to
+ Please be aware that when used with the SQLAlchemy ORM, you will need to
replace the JSON object present on an attribute with a new object in order
for any changes to be properly persisted.
import re
import logging
+import psycopg2.extensions as ext
+
from ... import util, exc
import decimal
from ... import processors
ENUM, ARRAY, _DECIMAL_TYPES, _FLOAT_TYPES,\
_INT_TYPES
from .hstore import HSTORE
+from .pgjson import JSON
logger = logging.getLogger('sqlalchemy.dialects.postgresql')
else:
return super(_PGHStore, self).result_processor(dialect, coltype)
+
+class _PGJSON(JSON):
+ # I've omitted the bind processor here because the method of serializing
+ # involves registering specific types to auto-serialize, and the adapter
+ # just a thin wrapper over json.dumps.
+ def result_processor(self, dialect, coltype):
+ if dialect._has_native_json:
+ return None
+ else:
+ return super(_PGJSON, self).result_processor(dialect, coltype)
+
# When we're handed literal SQL, ensure it's a SELECT-query. Since
# 8.3, combining cursors and "FOR UPDATE" has been fine.
SERVER_SIDE_CURSOR_RE = re.compile(
psycopg2_version = (0, 0)
_has_native_hstore = False
+ _has_native_json = False
colspecs = util.update_copy(
PGDialect.colspecs,
sqltypes.Enum: _PGEnum, # needs force_unicode
ARRAY: _PGArray, # needs force_unicode
HSTORE: _PGHStore,
+ JSON: _PGJSON
}
)
self._has_native_hstore = self.use_native_hstore and \
self._hstore_oids(connection.connection) \
is not None
+ self._has_native_json = self.psycopg2_version >= (2, 5)
@classmethod
def dbapi(cls):