summaryrefslogtreecommitdiffstats
path: root/Modules/_json.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-12-17 12:32:48 (GMT)
committerGitHub <noreply@github.com>2022-12-17 12:32:48 (GMT)
commit0fe61d0838218b18bcff4bf434beba3e8dbcc42b (patch)
tree54d1b62faa69456c80603aeba76953e0f4749a66 /Modules/_json.c
parent2b38a9aa747785f6d540e95e6846d6510de6b306 (diff)
downloadcpython-0fe61d0838218b18bcff4bf434beba3e8dbcc42b.zip
cpython-0fe61d0838218b18bcff4bf434beba3e8dbcc42b.tar.gz
cpython-0fe61d0838218b18bcff4bf434beba3e8dbcc42b.tar.bz2
gh-100272: Fix JSON serialization of OrderedDict (GH-100273)
It now preserves the order of keys.
Diffstat (limited to 'Modules/_json.c')
-rw-r--r--Modules/_json.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/Modules/_json.c b/Modules/_json.c
index 429b4ee..6879ad3 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++) {