From a3418052e9fdc1fc05cda4b632957decb0fadc7c Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 24 Sep 2012 11:23:09 +0100 Subject: [PATCH] Don't create/register a json array typecaster if no oid provided --- lib/_json.py | 5 ++++- tests/test_types_extras.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/_json.py b/lib/_json.py index e117252e..f90bac5b 100644 --- a/lib/_json.py +++ b/lib/_json.py @@ -202,7 +202,10 @@ def _create_json_typecasters(oid, array_oid, loads=None): return loads(s) JSON = new_type((oid, ), 'JSON', typecast_json) - JSONARRAY = new_array_type((array_oid, ), "JSONARRAY", JSON) + if array_oid is not None: + JSONARRAY = new_array_type((array_oid, ), "JSONARRAY", JSON) + else: + JSONARRAY = None return JSON, JSONARRAY diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 6af3c4c5..6ca33e41 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -1015,6 +1015,18 @@ class JsonTestCase(unittest.TestCase): curs.execute("""select NULL::json[]""") self.assertEqual(curs.fetchone()[0], None) + @skip_if_no_json_module + def test_no_array_oid(self): + curs = self.conn.cursor() + t1, t2 = psycopg2.extras.register_json(curs, oid=25) + self.assertEqual(t1.values[0], 25) + self.assertEqual(t2, None) + + curs.execute("""select '{"a": 100.0, "b": null}'::text""") + data = curs.fetchone()[0] + self.assertEqual(data['a'], 100) + self.assertEqual(data['b'], None) + def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__)