Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- import json
- from json import JSONEncoder
- import datetime
- class PythonObjectEncoder(JSONEncoder):
- def default(self, obj):
- if isinstance(obj, (datetime.datetime, datetime.date)):
- return obj.isoformat()
- elif isinstance(
- obj, (list, dict, str, unicode, int, float, bool, type, type(None))
- ):
- return JSONEncoder.default(self, obj)
- else:
- logging.warning(
- "Unable to find matching encoder for type %s. Using pickle",
- str(type(obj)),
- )
- return {"_python_object": pickle.dumps(obj)}
- json.dumps(data, cls=PythonObjectEncoder, sort_keys=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement