classification
Title: json.dumps should accept key function for ``sort_keys``
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: bob.ippolito Nosy List: bob.ippolito, catherinedevlin, cheryl.sabella, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2015-06-26 22:59 by catherinedevlin, last changed 2018-03-12 22:55 by cheryl.sabella.

Files
File name Uploaded Description Edit
json_callable_sort_24518.patch catherinedevlin, 2015-06-27 15:09 Patch review
Messages (7)
msg245874 - (view) Author: Catherine Devlin (catherinedevlin) * Date: 2015-06-26 22:59
Right now, json.dumps can be called with True or False, but it would be easy to also support accepting a key function, which then could be used to control the order of keys arbitrarily in the serialized JSON output.
msg245880 - (view) Author: Catherine Devlin (catherinedevlin) * Date: 2015-06-27 15:09
Implementation for the enhancement.  Includes tests.
msg245943 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-06-29 15:32
The idea and patch look reasonable.

Bob, what do you think?
msg245947 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2015-06-29 15:56
Seems like a good idea to me, I'll make sure this gets in simplejson as well.
msg245953 - (view) Author: Bob Ippolito (bob.ippolito) * (Python committer) Date: 2015-06-29 18:13
On further investigation, simplejson has implemented this functionality under a different name since 2.5.0 (2012-03-29).

"""
If item_sort_key is a callable (not the default), then the output of dictionaries will be sorted with it. The callable will be used like this: sorted(dct.items(), key=item_sort_key). This option takes precedence over sort_keys.

Changed in version 2.5.0: item_sort_key is new in 2.5.0.
"""
msg246194 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-07-03 17:36
Catherine, if you are going to continue contributing patches, which we hope you do, please sign the PSF Contribution Agreement.
https://www.python.org/psf/contrib/
https://www.python.org/psf/contrib/contrib_form/

It would be needed if this patch were to be used, but I presume Bob would import the patch from simplejson instead.
msg313710 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python triager) Date: 2018-03-12 22:55
As there was interest in this patch, @catherinedevlin would you be able to make a Github pull request for it?

Thanks!
History
Date User Action Args
2018-03-12 22:55:08cheryl.sabellasetversions: + Python 3.8, - Python 3.6
nosy: + cheryl.sabella

messages: + msg313710

stage: needs patch
2015-07-03 17:36:04terry.reedysetnosy: + terry.reedy
messages: + msg246194
2015-06-29 18:13:42bob.ippolitosetmessages: + msg245953
2015-06-29 15:56:05bob.ippolitosetmessages: + msg245947
2015-06-29 15:32:31rhettingersetversions: + Python 3.6, - Python 3.4
nosy: + bob.ippolito, rhettinger

messages: + msg245943

assignee: bob.ippolito
2015-06-27 15:09:59catherinedevlinsetfiles: + json_callable_sort_24518.patch
keywords: + patch
messages: + msg245880
2015-06-26 22:59:43catherinedevlincreate