import json
import numpy as np
from datetime import date, datetime, timedelta
[docs]
class NumpyEncoder(json.JSONEncoder):
[docs]
def default(self, obj):
if isinstance(obj, np.bool_):
return bool(obj)
if isinstance(obj, (np.floating, np.complexfloating)):
return float(obj)
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, np.string_):
return str(obj)
if isinstance(obj, (datetime, date)):
return obj.isoformat()
if isinstance(obj, timedelta):
return str(obj)
return super(NumpyEncoder, self).default(obj)