-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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 needs object_pairs_hook #49631
Comments
If PEP-372 goes through, Python is going to gain an ordered dict soon. The json module's encoder works well with it: >>> items = [('one', 1), ('two', 2), ('three',3), ('four',4), ('five',5)]
>>> json.dumps(OrderedDict(items))
'{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}' But the decoder doesn't fare so well. The existing object_hook for the >>> jtext = '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}'
>>> json.loads(jtext, object_hook=OrderedDict)
OrderedDict({u'four': 4, u'three': 3, u'five': 5, u'two': 2, u'one': 1}) A solution is to provide an alternate hook that emits a sequence of FWIW, sample ordered dict code is at: |
Why? According to RFC (emphasis mine): An object is an *unordered* collection of zero or more name/value |
Same reason as for config files and yaml files. Sometimes those files For example, jsonrpc method invocations are done with objects having --> {"method": "postMessage", "params": ["Hello all!"], "id": 99} If you're testing a program that filters json data (like a typical xml --> {{"title": "awk", "author":"aho", "isbn":"123456789X"}, Semantically, those entries can be scrambled; however, someone reading |
FWIW, here's the intended code for the filter in the last post: books = json.loads(infile, object_hook=OrderedDict)
for book in books:
del book['isbn']
json.dumps(books, outfile) |
Fair enough, but the patch isn't usable because the decoder was rewritten |
Thanks. I'll write-up a patch against |
Motivation: Yes. JSON says it's unordered. However Hashes in Ruby are ordered |
After enhancing namedtuple and ConfigParser, I found a simpler approach With a small tweak to OD's repr, it is fully substitutable for a dict See attached patch. |
Unfortunately this is a patch for the old json lib... the new one has a C |
When do you expect the new C version to go in? I'm looking forward to it. |
Whenever someone applies the patch for http://bugs.python.org/issue4136 -- |
Bob would you please take a look at the attached patch. |
This patch looks good to me, my only comment is that the patch mixes tabs |
Thanks for looking at this. |
I fixed two problems with this that didn't show up in the test suite, this r70702 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: