Skip to content

Commit 0fe61d0

Browse files
gh-100272: Fix JSON serialization of OrderedDict (GH-100273)
It now preserves the order of keys.
1 parent 2b38a9a commit 0fe61d0

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

Lib/test/test_json/test_default.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collections
12
from test.test_json import PyTest, CTest
23

34

@@ -7,6 +8,16 @@ def test_default(self):
78
self.dumps(type, default=repr),
89
self.dumps(repr(type)))
910

11+
def test_ordereddict(self):
12+
od = collections.OrderedDict(a=1, b=2, c=3, d=4)
13+
od.move_to_end('b')
14+
self.assertEqual(
15+
self.dumps(od),
16+
'{"a": 1, "c": 3, "d": 4, "b": 2}')
17+
self.assertEqual(
18+
self.dumps(od, sort_keys=True),
19+
'{"a": 1, "b": 2, "c": 3, "d": 4}')
20+
1021

1122
class TestPyDefault(TestDefault, PyTest): pass
1223
class TestCDefault(TestDefault, CTest): pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix JSON serialization of OrderedDict. It now preserves the order of keys.

Modules/_json.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -1570,10 +1570,9 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer,
15701570
*/
15711571
}
15721572

1573-
if (s->sort_keys) {
1574-
1575-
items = PyDict_Items(dct);
1576-
if (items == NULL || PyList_Sort(items) < 0)
1573+
if (s->sort_keys || !PyDict_CheckExact(dct)) {
1574+
items = PyMapping_Items(dct);
1575+
if (items == NULL || (s->sort_keys && PyList_Sort(items) < 0))
15771576
goto bail;
15781577

15791578
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(items); i++) {

0 commit comments

Comments
 (0)