[issue5381] json need object_pairs_hook

2009-02-27 Thread Raymond Hettinger
New submission from Raymond Hettinger : If PEP372 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":

[issue5381] json need object_pairs_hook

2009-02-27 Thread Bob Ippolito
Bob Ippolito added the comment: Why? According to RFC (emphasis mine): An object is an *unordered* collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array. -- resolution: -> invalid _

[issue5381] json need object_pairs_hook

2009-02-27 Thread Raymond Hettinger
Raymond Hettinger added the comment: Same reason as for config files and yaml files. Sometimes those files represent human edited input and if a machine re-edits, filters, or copies, it is nice to keep the original order (though it may make no semantic difference to the computer). For exampl

[issue5381] json need object_pairs_hook

2009-02-27 Thread Raymond Hettinger
Raymond Hettinger added the comment: 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) ___ Python tracker

[issue5381] json need object_pairs_hook

2009-02-27 Thread Bob Ippolito
Bob Ippolito added the comment: Fair enough, but the patch isn't usable because the decoder was rewritten in a later version of simplejson. There's another issue with patch to backport those back into Python http://bugs.python.org/issue4136 or you could just use the simplejson source here htt

[issue5381] json need object_pairs_hook

2009-02-27 Thread Raymond Hettinger
Raymond Hettinger added the comment: Thanks. I'll write-up a patch against http://code.google.com/p/simplejson/ and assign it back to you for review. -- assignee: bob.ippolito -> rhettinger ___ Python tracker

[issue5381] json need object_pairs_hook

2009-02-27 Thread Armin Ronacher
Armin Ronacher added the comment: Motivation: Yes. JSON says it's unordered. However Hashes in Ruby are ordered since 1.9 and they were since the very beginning in JavaScript and PHP. -- nosy: +aronacher ___ Python tracker

[issue5381] json need object_pairs_hook

2009-03-04 Thread Raymond Hettinger
Raymond Hettinger added the comment: After enhancing namedtuple and ConfigParser, I found a simpler approach that doesn't involve extending the API. The simple way is to use ordered dictionaries directly. With a small tweak to OD's repr, it is fully substitutable for a dict without changing

[issue5381] json need object_pairs_hook

2009-03-04 Thread Bob Ippolito
Bob Ippolito added the comment: Unfortunately this is a patch for the old json lib... the new one has a C API and an entirely different method of parsing documents (for performance reasons). ___ Python tracker ___

[issue5381] json need object_pairs_hook

2009-03-04 Thread Raymond Hettinger
Raymond Hettinger added the comment: When do you expect the new C version to go in? I'm looking forward to it. ___ Python tracker ___ ___ Pyth

[issue5381] json need object_pairs_hook

2009-03-04 Thread Bob Ippolito
Bob Ippolito added the comment: Whenever someone applies the patch for http://bugs.python.org/issue4136 -- I don't know when that will happen. ___ Python tracker ___