diff --git a/lib/_json.py b/lib/_json.py index 047551e5..eef1436e 100644 --- a/lib/_json.py +++ b/lib/_json.py @@ -47,6 +47,10 @@ else: JSON_OID = 114 JSONARRAY_OID = 199 +# oids from PostgreSQL 9.4 +JSONB_OID = 3802 +JSONBARRAY_OID = 3807 + class Json(object): """ An `~psycopg2.extensions.ISQLQuote` wrapper to adapt a Python object to @@ -151,6 +155,18 @@ def register_default_json(conn_or_curs=None, globally=False, loads=None): return register_json(conn_or_curs=conn_or_curs, globally=globally, loads=loads, oid=JSON_OID, array_oid=JSONARRAY_OID) +def register_default_jsonb(conn_or_curs=None, globally=False, loads=None): + """ + Create and register :sql:`jsonb` typecasters for PostgreSQL 9.4 and following. + + As in `register_default_json()`, the function allows to register a + customized *loads* function for the :sql:`jsonb` type at its known oid for + PostgreSQL 9.4 and following versions. All the parameters have the same + meaning of `register_json()`. + """ + return register_json(conn_or_curs=conn_or_curs, globally=globally, + loads=loads, oid=JSONB_OID, array_oid=JSONBARRAY_OID, name='jsonb') + def _create_json_typecasters(oid, array_oid, loads=None, name='JSON'): """Create typecasters for json data type.""" if loads is None: diff --git a/lib/extensions.py b/lib/extensions.py index f210da4f..71a92b93 100644 --- a/lib/extensions.py +++ b/lib/extensions.py @@ -152,20 +152,22 @@ class NoneAdapter(object): # Create default json typecasters for PostgreSQL 9.2 oids -from psycopg2._json import register_default_json +from psycopg2._json import register_default_json, register_default_jsonb try: JSON, JSONARRAY = register_default_json() + JSONB, JSONBARRAY = register_default_jsonb() except ImportError: pass -del register_default_json +del register_default_json, register_default_jsonb # Create default Range typecasters from psycopg2. _range import Range del Range + # Add the "cleaned" version of the encodings to the key. # When the encoding is set its name is cleaned up from - and _ and turned # uppercase, so an encoding not respecting these rules wouldn't be found in the diff --git a/lib/extras.py b/lib/extras.py index b21e223d..a873c4ef 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -965,7 +965,8 @@ def register_composite(name, conn_or_curs, globally=False, factory=None): # expose the json adaptation stuff into the module -from psycopg2._json import json, Json, register_json, register_default_json +from psycopg2._json import json, Json, register_json +from psycopg2._json import register_default_json, register_default_jsonb # Expose range-related objects