summaryrefslogtreecommitdiffstats
path: root/Lib/json/tests
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-03-19 19:19:03 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-03-19 19:19:03 (GMT)
commit91852ca673adcfa4e9ceacbb36f2cd45767efa58 (patch)
tree4b1d71bcc09823e096bc9124cb1424d12e08334c /Lib/json/tests
parent2124599eaa739f66db8871d68334706f4aa373a6 (diff)
downloadcpython-91852ca673adcfa4e9ceacbb36f2cd45767efa58.zip
cpython-91852ca673adcfa4e9ceacbb36f2cd45767efa58.tar.gz
cpython-91852ca673adcfa4e9ceacbb36f2cd45767efa58.tar.bz2
Issue 5381: Add object_pairs_hook to the json module.
Diffstat (limited to 'Lib/json/tests')
-rw-r--r--Lib/json/tests/test_decode.py16
-rw-r--r--Lib/json/tests/test_unicode.py16
2 files changed, 32 insertions, 0 deletions
diff --git a/Lib/json/tests/test_decode.py b/Lib/json/tests/test_decode.py
index 484cc94..0744b52 100644
--- a/Lib/json/tests/test_decode.py
+++ b/Lib/json/tests/test_decode.py
@@ -2,6 +2,7 @@ import decimal
from unittest import TestCase
import json
+from collections import OrderedDict
class TestDecode(TestCase):
def test_decimal(self):
@@ -20,3 +21,18 @@ class TestDecode(TestCase):
# exercise the uncommon cases. The array cases are already covered.
rval = json.loads('{ "key" : "value" , "k":"v" }')
self.assertEquals(rval, {"key":"value", "k":"v"})
+
+ def test_object_pairs_hook(self):
+ s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
+ p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
+ ("qrt", 5), ("pad", 6), ("hoy", 7)]
+ self.assertEqual(json.loads(s), eval(s))
+ self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p)
+ od = json.loads(s, object_pairs_hook = OrderedDict)
+ self.assertEqual(od, OrderedDict(p))
+ self.assertEqual(type(od), OrderedDict)
+ # the object_pairs_hook takes priority over the object_hook
+ self.assertEqual(json.loads(s,
+ object_pairs_hook = OrderedDict,
+ object_hook = lambda x: None),
+ OrderedDict(p))
diff --git a/Lib/json/tests/test_unicode.py b/Lib/json/tests/test_unicode.py
index 0b47cbb..13759f8 100644
--- a/Lib/json/tests/test_unicode.py
+++ b/Lib/json/tests/test_unicode.py
@@ -1,6 +1,7 @@
from unittest import TestCase
import json
+from collections import OrderedDict
class TestUnicode(TestCase):
def test_encoding1(self):
@@ -54,6 +55,21 @@ class TestUnicode(TestCase):
s = '"\\u{0:04x}"'.format(i)
self.assertEquals(json.loads(s), u)
+ def test_object_pairs_hook_with_unicode(self):
+ s = u'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
+ p = [(u"xkd", 1), (u"kcw", 2), (u"art", 3), (u"hxm", 4),
+ (u"qrt", 5), (u"pad", 6), (u"hoy", 7)]
+ self.assertEqual(json.loads(s), eval(s))
+ self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p)
+ od = json.loads(s, object_pairs_hook = OrderedDict)
+ self.assertEqual(od, OrderedDict(p))
+ self.assertEqual(type(od), OrderedDict)
+ # the object_pairs_hook takes priority over the object_hook
+ self.assertEqual(json.loads(s,
+ object_pairs_hook = OrderedDict,
+ object_hook = lambda x: None),
+ OrderedDict(p))
+
def test_default_encoding(self):
self.assertEquals(json.loads(u'{"a": "\xe9"}'.encode('utf-8')),
{'a': u'\xe9'})