from connexion.apps.flask_app import FlaskJSONEncoder

# from connexion.decorators import produces
from six import iteritems
from jass.models.base_model_ import Model
from pandas import isnull


class JSONEncoder(FlaskJSONEncoder):
    include_nulls = False

    def default(self, o):
        if isinstance(o, Model):
            dikt = {}
            for attr, _ in iteritems(o.swagger_types):
                value = getattr(o, attr)
                if not (isinstance(value, list)) and (value is None or isnull(value)):
                    if not self.include_nulls:
                        continue
                    else:
                        return None
                attr = o.attribute_map[attr]
                dikt[attr] = value
            return dikt
        return produces.JSONEncoder.default(self, o)
