Closed pjz closed 4 years ago
Ok. I think canonical output, like https://github.com/matrix-org/python-canonicaljson, and orjson.OPT_CANONICAL_MODE
, might be worth implementing. I haven't looked at what else beyond sorting keys would be necessary.
I would also like support for sort_keys
, but I'm not sure that it should be called OPT_CANONICAL_MODE
, that suggests that it's doing more than sorting keys, and might be missed by some people. Maybe have both OPT_SORT_KEYS
and OPT_CANONICAL_MODE
?
Agreed - 'canonical mode' also implies things about spacing - if there's a chance that orjson would ever support something like stdlib json's indent
or separators
, then OPT_SORT_KEYS
seems a more futureproof name.
Ok, OPT_SORT_KEYS
. I would not want this to impact the performance of the normal case. Performance will be worse, and both that and the use cases would need to be explained.
Or let it be another dimension to compare against: compare orjson's OPT_SORT_KEYS to json.dumps(foo, sort_keys=1)
. I bet orjson still wins :)
+1 and, I just want to document another use case for sorted output – if you want to hash some JSON, you need sorted keys so that you can md5 it and get a stable result.
To be released in 2.3.0.
For people coming from Google searching how sort_keys works:
sort_keys=1
has been replaced with option=orjson.OPT_SORT_KEYS
a while back
as mentioned in the orjson README
PS: I arrived here because I'm using structlog
21.1.0
(and that is using sort_keys
as keyword) in combination with orjson :)
It would be nice to have dumps support a 'sort_keys' option that would sort the keys in output dictionaries. This allows me to verify that
dumps({"a":"b", "c":"d"}) == dumps({"a":"b", "c":"d"})
. Otherwise I have to deserialize them both and do individual key/value compares.