fork(1) download
  1. import datetime
  2. import json
  3.  
  4. def json_debug_handler(obj):
  5. print("object received:")
  6. print type(obj)
  7. print("\n\n")
  8. if isinstance(obj, datetime.datetime):
  9. return obj.isoformat()
  10. elif isinstance(obj,mDict):
  11. return {'orig':obj , 'attrs': vars(obj)}
  12. elif isinstance(obj,mList):
  13. return {'orig':obj, 'attrs': vars(obj)}
  14. else:
  15. return None
  16.  
  17.  
  18. class mDict(dict):
  19. pass
  20.  
  21.  
  22. class mList(list):
  23. pass
  24.  
  25.  
  26. def test_debug_json():
  27. games = mList(['mario','contra','tetris'])
  28. games.src = 'console'
  29. scores = mDict({'dp':10,'pk':45})
  30. scores.processed = "qunprocessed"
  31. test_json = { 'games' : games , 'scores' : scores , 'date': datetime.datetime.now() }
  32. print(json.dumps(test_json,default=json_debug_handler))
  33.  
  34. if __name__ == '__main__':
  35. test_debug_json()
  36.  
Success #stdin #stdout 0.02s 8280KB
stdin
Standard input is empty
stdout
object received:
<type 'datetime.datetime'>



{"date": "2013-05-10T00:36:50.746067", "games": ["mario", "contra", "tetris"], "scores": {"pk": 45, "dp": 10}}