[issue21650] add json.tool option to avoid alphabetic sort of fields

R. David Murray report at bugs.python.org
Fri Jun 6 18:44:19 CEST 2014


R. David Murray added the comment:

Yes but the input is turned into a dict, and dicts do not preserve order.  Further, what is passed to the object_hook is already a dict, so the order is already lost before object_hook is called.

Since the parser (or at least the Python version of the parser) first turns the input into a list of pairs, and a Python OrderedDict can be instantiated from a list of pairs, it would theoretically be possible to extend the object_hook API to allow an OrderedDict to be used on decode.  Exactly how to do that so as to retain backward compatibility is a bit of a question.

So, it is *possible* to achieve your aim, but it isn't as simple as allowing sort= to be set False.

IMO being able to preserve the order of the input when desired (ie: use an OrderedDict object_hook) would be a nice feature to have.

----------
stage: patch review -> needs patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21650>
_______________________________________


More information about the Python-bugs-list mailing list