TypeError: Object of type bool_ is not JSON serializable
注意区分bool和bool_的区别!!!
- bool_类型实际上是numpy.bool_,不能写入到json
- bool只是普通的布尔类型,可以写入到json
TypeError: Object of type bool_ is not JSON serializable解决方法
重写NumpyEncoder(参考博客)增加了bool_的处理
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
numpy.uint16, numpy.uint32, numpy.uint64)):
return int(obj)
elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32,
numpy.float64)):
return float(obj)
elif isinstance(obj, (numpy.ndarray,)):
return obj.tolist()
elif isinstance(obj, (numpy.bool_,)):
return bool(obj)
return json.JSONEncoder.default(self, obj)
PKL转json完整代码
# coding = utf-8
import pickle
import json
import numpy
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
numpy.uint16, numpy.uint32, numpy.uint64)):
return int(obj)
elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32,
numpy.float64)):
return float(obj)
elif isinstance(obj, (numpy.ndarray,)):
return obj.tolist()
elif isinstance(obj, (numpy.bool_,)):
return bool(obj)
return json.JSONEncoder.default(self, obj)
def convert_dict_to_json(file_path):
with open(file_path, 'rb') as fpkl, open('%s.json' % file_path, 'a+') as fjson:
data = pickle.load(fpkl, encoding='latin1')
data_dict = dict(data)
json.dump(data, fjson, ensure_ascii=False, sort_keys=True, indent=4, cls=NumpyEncoder)
def main():
# if sys.argv[1] and os.path.isfile(sys.argv[1]):
file_path = '../models/model_best.model.pkl'
print("Processing %s ..." % file_path)
convert_dict_to_json(file_path)
if __name__ == '__main__':
main()