summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-09-19 15:49:00 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-09-19 15:49:00 +0100
commitd963b478e27557dab74028b4fd7caf5ebe5f9f44 (patch)
tree95c61f512700c181d4040b88b86bcbb60664f31a /lib
parent26d71b4cba31a62bc648bec353aab299adb391a6 (diff)
downloadpsycopg2-d963b478e27557dab74028b4fd7caf5ebe5f9f44.tar.gz
Added register_default_json() function
Register a typecaster for PostgreSQL 9.2 json.
Diffstat (limited to 'lib')
-rw-r--r--lib/_json.py21
-rw-r--r--lib/extensions.py9
-rw-r--r--lib/extras.py2
3 files changed, 23 insertions, 9 deletions
diff --git a/lib/_json.py b/lib/_json.py
index 09d854b..3d21148 100644
--- a/lib/_json.py
+++ b/lib/_json.py
@@ -43,6 +43,10 @@ else:
json = None
+# oids from PostgreSQL 9.2
+JSON_OID = 114
+JSONARRAY_OID = 199
+
class Json(object):
"""A wrapper to adapt a Python object to :sql:`json` data type.
@@ -130,7 +134,7 @@ def register_json(conn_or_curs=None, globally=False, loads=None,
if oid is None:
oid, array_oid = _get_json_oids(conn_or_curs)
- JSON, JSONARRAY = create_json_typecasters(oid, array_oid, loads)
+ JSON, JSONARRAY = _create_json_typecasters(oid, array_oid, loads)
register_type(JSON, not globally and conn_or_curs or None)
@@ -139,7 +143,20 @@ def register_json(conn_or_curs=None, globally=False, loads=None,
return JSON, JSONARRAY
-def create_json_typecasters(oid, array_oid, loads=None):
+def register_default_json(conn_or_curs=None, globally=False, loads=None):
+ """
+ Create and register :sql:`json` typecasters for PostgreSQL 9.2 and following.
+
+ Since PostgreSQL 9.2 :sql:`json` is a builtin type, hence its oid is known
+ and fixed. This function allows specifying a customized *loads* function
+ for the default :sql:`json` type without querying the database.
+ All the parameters have the same meaning of `register_json()`.
+ """
+ return register_json(conn_or_curs=conn_or_curs, globally=globally,
+ loads=loads, oid=JSON_OID, array_oid=JSONARRAY_OID)
+
+
+def _create_json_typecasters(oid, array_oid, loads=None):
"""Create typecasters for json data type."""
if loads is None:
if json is None:
diff --git a/lib/extensions.py b/lib/extensions.py
index 066373c..94dcfb7 100644
--- a/lib/extensions.py
+++ b/lib/extensions.py
@@ -151,17 +151,14 @@ class NoneAdapter(object):
# Create default json typecasters for PostgreSQL 9.2 oids
-from psycopg2._json import create_json_typecasters
+from psycopg2._json import register_default_json
try:
- JSON, JSONARRAY = create_json_typecasters(114, 199)
+ JSON, JSONARRAY = register_default_json()
except ImportError:
pass
-else:
- register_type(JSON)
- register_type(JSONARRAY)
-del create_json_typecasters
+del register_default_json
# Add the "cleaned" version of the encodings to the key.
diff --git a/lib/extras.py b/lib/extras.py
index 643214e..e14e1c4 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -970,7 +970,7 @@ def register_composite(name, conn_or_curs, globally=False):
# expose the json adaptation stuff into the module
-from psycopg2._json import json, Json, register_json
+from psycopg2._json import json, Json, register_json, register_default_json
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())