import datetime
import json
def json_debug_handler(obj):
print("object received:")
print type(obj)
print("\n\n")
if isinstance(obj, datetime.datetime):
return obj.isoformat()
elif isinstance(obj,mDict):
return {'orig':obj , 'attrs': vars(obj)}
elif isinstance(obj,mList):
return {'orig':obj, 'attrs': vars(obj)}
else:
return None
class mDict(dict):
pass
class mList(list):
pass
def test_debug_json():
games = mList(['mario','contra','tetris'])
games.src = 'console'
scores = mDict({'dp':10,'pk':45})
scores.processed = "qunprocessed"
test_json = { 'games' : games , 'scores' : scores , 'date': datetime.datetime.now() }
print(json.dumps(test_json,default=json_debug_handler))
if __name__ == '__main__':
test_debug_json()
aW1wb3J0IGRhdGV0aW1lCmltcG9ydCBqc29uCgpkZWYganNvbl9kZWJ1Z19oYW5kbGVyKG9iaik6CiAgICBwcmludCgib2JqZWN0IHJlY2VpdmVkOiIpCiAgICBwcmludCB0eXBlKG9iaikKICAgIHByaW50KCJcblxuIikKICAgIGlmICBpc2luc3RhbmNlKG9iaiwgZGF0ZXRpbWUuZGF0ZXRpbWUpOgogICAgICAgIHJldHVybiBvYmouaXNvZm9ybWF0KCkKICAgIGVsaWYgaXNpbnN0YW5jZShvYmosbURpY3QpOgogICAgICAgIHJldHVybiB7J29yaWcnOm9iaiAsICdhdHRycyc6IHZhcnMob2JqKX0KICAgIGVsaWYgaXNpbnN0YW5jZShvYmosbUxpc3QpOgogICAgICAgIHJldHVybiB7J29yaWcnOm9iaiwgJ2F0dHJzJzogdmFycyhvYmopfQogICAgZWxzZToKICAgICAgICByZXR1cm4gTm9uZQoKCmNsYXNzIG1EaWN0KGRpY3QpOgogICAgcGFzcwoKCmNsYXNzIG1MaXN0KGxpc3QpOgogICAgcGFzcwoKCmRlZiB0ZXN0X2RlYnVnX2pzb24oKToKICAgIGdhbWVzID0gbUxpc3QoWydtYXJpbycsJ2NvbnRyYScsJ3RldHJpcyddKQogICAgZ2FtZXMuc3JjID0gJ2NvbnNvbGUnCiAgICBzY29yZXMgPSBtRGljdCh7J2RwJzox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