mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-22 08:56:34 +03:00
Add register_default_jsonb() and register the type
This commit is contained in:
parent
6bca443e37
commit
9d547469b8
16
lib/_json.py
16
lib/_json.py
|
@ -47,6 +47,10 @@ else:
|
||||||
JSON_OID = 114
|
JSON_OID = 114
|
||||||
JSONARRAY_OID = 199
|
JSONARRAY_OID = 199
|
||||||
|
|
||||||
|
# oids from PostgreSQL 9.4
|
||||||
|
JSONB_OID = 3802
|
||||||
|
JSONBARRAY_OID = 3807
|
||||||
|
|
||||||
class Json(object):
|
class Json(object):
|
||||||
"""
|
"""
|
||||||
An `~psycopg2.extensions.ISQLQuote` wrapper to adapt a Python object to
|
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,
|
return register_json(conn_or_curs=conn_or_curs, globally=globally,
|
||||||
loads=loads, oid=JSON_OID, array_oid=JSONARRAY_OID)
|
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'):
|
def _create_json_typecasters(oid, array_oid, loads=None, name='JSON'):
|
||||||
"""Create typecasters for json data type."""
|
"""Create typecasters for json data type."""
|
||||||
if loads is None:
|
if loads is None:
|
||||||
|
|
|
@ -152,20 +152,22 @@ class NoneAdapter(object):
|
||||||
|
|
||||||
|
|
||||||
# Create default json typecasters for PostgreSQL 9.2 oids
|
# 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:
|
try:
|
||||||
JSON, JSONARRAY = register_default_json()
|
JSON, JSONARRAY = register_default_json()
|
||||||
|
JSONB, JSONBARRAY = register_default_jsonb()
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
del register_default_json
|
del register_default_json, register_default_jsonb
|
||||||
|
|
||||||
|
|
||||||
# Create default Range typecasters
|
# Create default Range typecasters
|
||||||
from psycopg2. _range import Range
|
from psycopg2. _range import Range
|
||||||
del Range
|
del Range
|
||||||
|
|
||||||
|
|
||||||
# Add the "cleaned" version of the encodings to the key.
|
# Add the "cleaned" version of the encodings to the key.
|
||||||
# When the encoding is set its name is cleaned up from - and _ and turned
|
# 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
|
# uppercase, so an encoding not respecting these rules wouldn't be found in the
|
||||||
|
|
|
@ -965,7 +965,8 @@ def register_composite(name, conn_or_curs, globally=False, factory=None):
|
||||||
|
|
||||||
|
|
||||||
# expose the json adaptation stuff into the module
|
# 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
|
# Expose range-related objects
|
||||||
|
|
Loading…
Reference in New Issue
Block a user