diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-12-17 12:32:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-17 12:32:48 (GMT) |
commit | 0fe61d0838218b18bcff4bf434beba3e8dbcc42b (patch) | |
tree | 54d1b62faa69456c80603aeba76953e0f4749a66 /Modules | |
parent | 2b38a9aa747785f6d540e95e6846d6510de6b306 (diff) | |
download | cpython-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')
-rw-r--r-- | Modules/_json.c | 7 |
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++) { |