Added str() for the Json adapter

Fixes ticket #191.
This commit is contained in:
Daniele Varrazzo 2014-02-22 20:41:11 +00:00
parent 66f85b5832
commit 6192a4fb17
3 changed files with 28 additions and 0 deletions

9
NEWS
View File

@ -1,6 +1,15 @@
Current release
---------------
What's new in psycopg 2.6
-------------------------
Bug fixes:
- Json apapter's `!str()` returns the adapted content instead of the `!repr()`
(:ticket:`#191`).
What's new in psycopg 2.5.3
^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -92,6 +92,14 @@ class Json(object):
s = self.dumps(self.adapted)
return QuotedString(s).getquoted()
if sys.version_info < (3,):
def __str__(self):
return self.getquoted()
else:
def __str__(self):
# getquoted is binary in Py3
return self.getquoted().decode('ascii', errors='replace')
def register_json(conn_or_curs=None, globally=False, loads=None,
oid=None, array_oid=None):

View File

@ -1055,6 +1055,17 @@ class JsonTestCase(ConnectingTestCase):
self.assertEqual(data['a'], 100)
self.assertEqual(data['b'], None)
@skip_if_no_json_module
def test_str(self):
snowman = u"\u2603"
obj = {'a': [1, 2, snowman]}
j = psycopg2.extensions.adapt(psycopg2.extras.Json(obj))
s = str(j)
self.assert_(isinstance(s, str))
# no pesky b's
self.assert_(s.startswith("'"))
self.assert_(s.endswith("'"))
class RangeTestCase(unittest.TestCase):
def test_noparam(self):