import os
import sys
import glob
import json
import numpy as np
from datetime import datetime

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        elif isinstance(obj, datetime):
            return obj.__str__()
        else:
            return super(MyEncoder, self).default(obj)

def LoadJson(filename):
    if not os.path.exists(filename):
        print ('path %s does not exist'%filename)
    with open(filename,'r') as f:
        result = json.load(f)
    f.close()
    return result
    
def WriteJson(filename,dict_data):
    dirname = os.path.dirname(filename)
    if (dirname != '') and (not os.path.exists(dirname)):
        os.makedirs(dirname)
    f = open(filename, 'wt')
    json.dump(dict_data, f,sort_keys=True, indent=4,cls=MyEncoder)
    f.close()
