Message82825
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": 3, "four": 4, "five": 5}'
But the decoder doesn't fare so well. The existing object_hook for the
decoder passes in a dictionary instead of a list of pairs. So, all the
ordering information is lost:
>>> 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
pairs. If present, that hook should run instead of object_hook. A
rough proof-of-concept patch is attached.
FWIW, sample ordered dict code is at:
http://code.activestate.com/recipes/576669/ |
|
Date |
User |
Action |
Args |
2009-02-27 08:37:56 | rhettinger | set | recipients:
+ rhettinger, bob.ippolito |
2009-02-27 08:37:56 | rhettinger | set | messageid: <1235723876.26.0.796036500228.issue5381@psf.upfronthosting.co.za> |
2009-02-27 08:37:54 | rhettinger | link | issue5381 messages |
2009-02-27 08:37:53 | rhettinger | create | |
|