From 3262ea64b84bf19e00c5e3fdf9d56b1d28e6326b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 15 Dec 2022 18:28:41 +0200 Subject: [PATCH 1/2] gh-100272: Fix JSON serialization of OrderedDict It now preserves the order of keys. --- Lib/test/test_json/test_default.py | 11 +++++++++++ .../2022-12-15-18-28-13.gh-issue-100272.D1O9Ey.rst | 1 + Modules/_json.c | 7 +++---- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-12-15-18-28-13.gh-issue-100272.D1O9Ey.rst diff --git a/Lib/test/test_json/test_default.py b/Lib/test/test_json/test_default.py index 9b8325e9c3816b..2ce6182853185f 100644 --- a/Lib/test/test_json/test_default.py +++ b/Lib/test/test_json/test_default.py @@ -1,3 +1,4 @@ +import collections from test.test_json import PyTest, CTest @@ -7,6 +8,16 @@ def test_default(self): self.dumps(type, default=repr), self.dumps(repr(type))) + def test_ordereddict(self): + od = collections.OrderedDict(a=1, b=2) + od.move_to_end('a') + self.assertEqual( + self.dumps(od), + '{"b": 2, "a": 1}') + self.assertEqual( + self.dumps(od, sort_keys=True), + '{"a": 1, "b": 2}') + class TestPyDefault(TestDefault, PyTest): pass class TestCDefault(TestDefault, CTest): pass diff --git a/Misc/NEWS.d/next/Library/2022-12-15-18-28-13.gh-issue-100272.D1O9Ey.rst b/Misc/NEWS.d/next/Library/2022-12-15-18-28-13.gh-issue-100272.D1O9Ey.rst new file mode 100644 index 00000000000000..2fb08926179211 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-12-15-18-28-13.gh-issue-100272.D1O9Ey.rst @@ -0,0 +1 @@ +Fix JSON serialization of OrderedDict. It now preserves the order of keys. diff --git a/Modules/_json.c b/Modules/_json.c index 429b4ee0fa8d8d..6879ad3d0722b6 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1570,10 +1570,9 @@ encoder_listencode_dict(PyEncoderObject *s, _PyUnicodeWriter *writer, */ } - if (s->sort_keys) { - - items = PyDict_Items(dct); - if (items == NULL || PyList_Sort(items) < 0) + if (s->sort_keys || !PyDict_CheckExact(dct)) { + items = PyMapping_Items(dct); + if (items == NULL || (s->sort_keys && PyList_Sort(items) < 0)) goto bail; for (Py_ssize_t i = 0; i < PyList_GET_SIZE(items); i++) { From 8e5b181bb307912429c63423ed7d1318048122ae Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 17 Dec 2022 12:06:20 +0200 Subject: [PATCH 2/2] Add more items in the test. --- Lib/test/test_json/test_default.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_json/test_default.py b/Lib/test/test_json/test_default.py index 2ce6182853185f..3ce16684a08272 100644 --- a/Lib/test/test_json/test_default.py +++ b/Lib/test/test_json/test_default.py @@ -9,14 +9,14 @@ def test_default(self): self.dumps(repr(type))) def test_ordereddict(self): - od = collections.OrderedDict(a=1, b=2) - od.move_to_end('a') + od = collections.OrderedDict(a=1, b=2, c=3, d=4) + od.move_to_end('b') self.assertEqual( self.dumps(od), - '{"b": 2, "a": 1}') + '{"a": 1, "c": 3, "d": 4, "b": 2}') self.assertEqual( self.dumps(od, sort_keys=True), - '{"a": 1, "b": 2}') + '{"a": 1, "b": 2, "c": 3, "d": 4}') class TestPyDefault(TestDefault, PyTest): pass