This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients bob.ippolito, rhettinger
Date 2009-02-27.08:37:52
SpamBayes Score 3.6115555e-13
Marked as misclassified No
Message-id <>
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:
Date User Action Args
2009-02-27 08:37:56rhettingersetrecipients: + rhettinger, bob.ippolito
2009-02-27 08:37:56rhettingersetmessageid: <>
2009-02-27 08:37:54rhettingerlinkissue5381 messages
2009-02-27 08:37:53rhettingercreate