mirror of
https://github.com/python/cpython.git
synced 2025-08-13 21:38:57 +00:00
gh-100272: Fix JSON serialization of OrderedDict (GH-100273)
It now preserves the order of keys.
This commit is contained in:
parent
2b38a9aa74
commit
0fe61d0838
3 changed files with 15 additions and 4 deletions
|
@ -1,3 +1,4 @@
|
|||
import collections
|
||||
from test.test_json import PyTest, CTest
|
||||
|
||||
|
||||
|
@ -7,6 +8,16 @@ class TestDefault:
|
|||
self.dumps(type, default=repr),
|
||||
self.dumps(repr(type)))
|
||||
|
||||
def test_ordereddict(self):
|
||||
od = collections.OrderedDict(a=1, b=2, c=3, d=4)
|
||||
od.move_to_end('b')
|
||||
self.assertEqual(
|
||||
self.dumps(od),
|
||||
'{"a": 1, "c": 3, "d": 4, "b": 2}')
|
||||
self.assertEqual(
|
||||
self.dumps(od, sort_keys=True),
|
||||
'{"a": 1, "b": 2, "c": 3, "d": 4}')
|
||||
|
||||
|
||||
class TestPyDefault(TestDefault, PyTest): pass
|
||||
class TestCDefault(TestDefault, CTest): pass
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue