Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import pickle
  2. import json
  3. from json import JSONEncoder
  4. import datetime
  5.  
  6. class PythonObjectEncoder(JSONEncoder):
  7.  
  8. def default(self, obj):
  9. if isinstance(obj, (datetime.datetime, datetime.date)):
  10. return obj.isoformat()
  11. elif isinstance(
  12. obj, (list, dict, str, unicode, int, float, bool, type, type(None))
  13. ):
  14. return JSONEncoder.default(self, obj)
  15. else:
  16. logging.warning(
  17. "Unable to find matching encoder for type %s. Using pickle",
  18. str(type(obj)),
  19. )
  20. return {"_python_object": pickle.dumps(obj)}
  21.  
  22. json.dumps(data, cls=PythonObjectEncoder, sort_keys=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement