Don't create/register a json array typecaster if no oid provided

This commit is contained in:
Daniele Varrazzo 2012-09-24 11:23:09 +01:00
parent 5645e7adef
commit a3418052e9
2 changed files with 16 additions and 1 deletions

View File

@ -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

View File

@ -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__)