I am using python to encode OrderedDict with a timestamp, and I'm having problems. The data I'm trying to encode is as follows:
OrderedDict([('a', datetime.datetime(2015, 6, 15, 15, 58, 54, 884000)), ('b', 'b'), ('c', 'c'), ('d', 'd')])
I expect this to be encoded and decoded by json to get exactly the same data.
To encode the timestamp directly, without changing the ISO or Unix time, I used the bson json_util interface as shown below. It is working correctly.
json.dumps(str, default=json_util.default) json.loads(jsonstr, object_hook=json_util.object_hook)
To get OrderedDict, I used object_pairs_hook, which also works:
json.loads(x, object_pairs_hook=OrderedDict)
However, when they are used together, the two things are messy with each other, and the result is not in the correct format (since the bson interface creates an additional dictionary for the timestamp).
json.loads(jsonstr, object_hook=json_util.object_hook, object_pairs_hook=OrderedDict)
This request receives the following:
OrderedDict([(u'a', OrderedDict([(u'$date', 1434383934884L)])), (u'b', u'b'), (u'c', u'c'), (u'd', u'd')])
The timestamp is not being processed correctly. Any suggestions on how to do this correctly? (Pickle may be the direction, but first I look for other solutions).