mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue 5381: Add object_pairs_hook to the json module.
This commit is contained in:
parent
2124599eaa
commit
91852ca673
7 changed files with 150 additions and 23 deletions
|
@ -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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue