def test04_write_json_with_mongo_objects():
schema = {
'db': {'coll': {'object': {'_id': {'type': 'oid', 'anonymized': ObjectId()},
'date': {'type': 'date', 'anonymized': datetime(2015, 1, 1)}}}}
}
output = os.path.join(TEST_DIR, 'output_data_dict.json')
output_maker = JsonOutput({})
output_maker.data = schema
with open(output, 'w') as out_fd:
output_maker.write_data(out_fd)
with open(output, 'r') as out_fd:
# custom json_options - DEFAULT_JSON_OPTIONS sets tzinfo when loading datetime (!= schema)
json_options = json_util.JSONOptions(tz_aware=False)
# object_hook allows to interpret $oid, $date, etc from json and convert them to objects
object_hook = partial(json_util.object_hook, json_options=json_options)
assert json.load(out_fd, object_hook=object_hook) == schema
os.remove(output)
评论列表
文章目录