# PROJECT : kungfucms
# TIME    : 2020/6/8 10:32
# AUTHOR : <NAME>
# EMAIL : <EMAIL>
# PHONE : 13811754531
# WECHAT : 13811754531
# https://github.com/youngershen
import datetime
import decimal
import uuid
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.timezone import is_aware
from django.utils.duration import duration_iso_string
from django.utils.functional import Promise


class JSONEncoder(DjangoJSONEncoder):
    def default(self, o):
        # See "Date Time String Format" in the ECMA-262 specification.
        if isinstance(o, datetime.datetime):
            dt_str = o.strftime('%Y-%m-%d %H:%M:%S')
            return dt_str
        elif isinstance(o, datetime.date):
            return o.isoformat()
        elif isinstance(o, datetime.time):
            if is_aware(o):
                raise ValueError("JSON can't represent timezone-aware times.")
            r = o.isoformat()
            if o.microsecond:
                r = r[:12]
            return r
        elif isinstance(o, datetime.timedelta):
            return duration_iso_string(o)
        elif isinstance(o, (decimal.Decimal, uuid.UUID, Promise)):
            return str(o)
        else:
            return super().default(o)

# PROJECT : kungfucms
# TIME    : 2020/6/8 10:32
# AUTHOR : <NAME>
# EMAIL : <EMAIL>
# PHONE : 13811754531
# WECHAT : 13811754531
# https://github.com/youngershen
import datetime
import decimal
import uuid
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.timezone import is_aware
from django.utils.duration import duration_iso_string
from django.utils.functional import Promise


class JSONEncoder(DjangoJSONEncoder):
    def default(self, o):
        # See "Date Time String Format" in the ECMA-262 specification.
        if isinstance(o, datetime.datetime):
            dt_str = o.strftime('%Y-%m-%d %H:%M:%S')
            return dt_str
        elif isinstance(o, datetime.date):
            return o.isoformat()
        elif isinstance(o, datetime.time):
            if is_aware(o):
                raise ValueError("JSON can't represent timezone-aware times.")
            r = o.isoformat()
            if o.microsecond:
                r = r[:12]
            return r
        elif isinstance(o, datetime.timedelta):
            return duration_iso_string(o)
        elif isinstance(o, (decimal.Decimal, uuid.UUID, Promise)):
            return str(o)
        else:
            return super().default(o)

# PROJECT : kungfucms # TIME    : 2020/6/8 10:32 # AUTHOR : <NAME> # EMAIL : <EMAIL> # PHONE : 13811754531 # WECHAT : 13811754531 # https://github.com/youngershen # See "Date Time String Format" in the ECMA-262 specification.