ijl / orjson

Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy
Apache License 2.0
6.07k stars 211 forks source link

dumps support sort_keys #28

Closed pjz closed 4 years ago

pjz commented 5 years ago

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.

ijl commented 5 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.

2-5 commented 5 years ago

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?

pjz commented 5 years ago

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.

ijl commented 5 years ago

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.

pjz commented 5 years ago

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 :)

davidkhess commented 4 years ago

+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.

ijl commented 4 years ago

To be released in 2.3.0.

NostraDavid commented 3 years ago

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 :)