Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[json] document fact that key ordering is preserved #10020

Merged
merged 1 commit into from
Dec 20, 2018

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Dec 17, 2018

document (with a runnableExamples unittest) fact that key ordering is preserved

This property is useful in some cases (eg when dumping some value as json string, it's nice to preserve source ordering of keys)

note

the "purist" approach is that json key ordering is unspecified and shouldn't matter (eg see https://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv) however it's and often recurring pain point, and since we're already preserving key ordering, might as well document that it works within Nim's stdlib.

same as in python, where they realized it can be useful:
https://stackoverflow.com/questions/10844064/items-in-json-object-are-out-of-order-using-json-dumps

Since Python 3.6, the keyword argument order is preserved and the above can be rewritten using a nicer syntax:

import json
from collections import OrderedDict
json.dumps(OrderedDict(a1=0, a2=0, a0=0, a3=0, a4=0))
'{"a1": 0, "a2": 0, "a0": 0, "a3": 0, "a4": 0}'

@Araq Araq merged commit 6cbe1d4 into nim-lang:devel Dec 20, 2018
@timotheecour timotheecour deleted the pr_doc_json_key_ordering branch December 20, 2018 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants