不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2020-09-26 21:45:48  修改时间:2024-04-12 13:55:38  分类:Python基础  编辑

自定义模块文件:json_util.py,解决如下报错问题:

TypeError: Object of type 'datetime' is not JSON serializable

json_util.py 文件内容:

import decimal
import json
from datetime import time, datetime, date, timedelta
from framework.utils.log_util import logger
from framework.utils.string_util import string_is_empty


class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        try:
            if isinstance(obj, int):
                return int(obj)
            elif isinstance(obj, float) or isinstance(obj, decimal.Decimal):
                return float(obj)
            if isinstance(obj, datetime):
                return obj.strftime('%Y-%m-%d %H:%M:%S')
            elif isinstance(obj, date):
                return obj.strftime('%Y-%m-%d')
            if isinstance(obj, time) or isinstance(obj, timedelta):
                return obj.__str__()
            else:
                return json.JSONEncoder.default(self, obj)
        except Exception as e:
            logger.exception(e, stack_info=True)
            return obj.__str__()


def json_encode(val_obj):
    """
    对象转JSON字符串
    :param val_obj: 值对象
    :return:
    """
    return json.dumps(val_obj, cls=MyEncoder, ensure_ascii=False)


def json_decode(json_str, default=None):
    """
    JSON字符串转对象
    :param json_str: JSON字符串
    :param default: 默认值,比如:{} 或 []
    :return:
    """
    if default is None:
        default = {}
    if string_is_empty(json_str):
        return default
    return json.loads(json_str)

 

 

参考:

解决TypeError: Object of type ‘datetime‘ is not JSON serializable问题

解决:TypeError: Object of type xxx is not JSON serializable

Python-TypeError: Object of type 'Decimal' is not JSON serializable 报错