from datetime import date
from flask import Flask as _Flask
from flask.json import JSONEncoder as _JSONEncoder
from app.libs.error_code import ServerError


class JSONEncoder(_JSONEncoder):

    def default(self, o):
        if hasattr(o, 'keys') and hasattr(o, "__getitem__"):
            return dict(o)
        elif isinstance(o, date):
            return o.strftime("%Y-%m-%d")
        else:
            raise ServerError()


class Flask(_Flask):
    json_encoder = JSONEncoder
