# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

from datetime import datetime
from decimal import Decimal
from enum import Enum
from uuid import UUID

import simplejson as json


class ExtendedEncoder(json.JSONEncoder):
    def __init__(self, use_serialize_method=False, **kwargs):
        self.use_serialize_method = use_serialize_method
        super().__init__(**kwargs)

    def default(self, o):
        if isinstance(o, Decimal):
            return str(o)
        if isinstance(o, Enum):
            return o.value
        if isinstance(o, datetime):
            return o.isoformat()
        if isinstance(o, UUID):
            return str(o)
        if self.use_serialize_method and hasattr(o, 'serializable'):
            return o.serializable()
        return super(ExtendedEncoder, self).default(o)


def dumps(obj, **kwargs):
    return json.dumps(obj, **{**dict(
        cls=ExtendedEncoder,
        use_decimal=False,
        iterable_as_array=True,
        use_serialize_method=True
    ), **kwargs})


def loads(fp, **kwargs):
    return json.loads(fp, **kwargs)
