Fixed json typecaster with NULL input

This commit is contained in:
Daniele Varrazzo 2012-09-20 00:36:53 +01:00
parent 7386b8327c
commit 9b2ad7abb5
2 changed files with 12 additions and 0 deletions

View File

@ -197,6 +197,8 @@ def _create_json_typecasters(oid, array_oid, loads=None):
loads = json.loads
def typecast_json(s, cur):
if s is None:
return None
return loads(s)
JSON = new_type((oid, ), 'JSON', typecast_json)

View File

@ -1005,6 +1005,16 @@ class JsonTestCase(unittest.TestCase):
self.assert_(isinstance(data[0]['a'], Decimal))
self.assertEqual(data[0]['a'], Decimal('100.0'))
@skip_if_no_json_module
@skip_if_no_json_type
def test_null(self):
psycopg2.extras.register_json(self.conn)
curs = self.conn.cursor()
curs.execute("""select NULL::json""")
self.assertEqual(curs.fetchone()[0], None)
curs.execute("""select NULL::json[]""")
self.assertEqual(curs.fetchone()[0], None)
def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__)