~3v1n0/bileto/ppa-packages-link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""JSONEncoder Extension"""

from contextlib import suppress
from flask.json import JSONEncoder


class BiletoJSONEncoder(JSONEncoder):
    """Extend JSONEncoder to be more useful."""
    # Minify output
    item_separator = ','
    key_separator = ':'

    # Include all attributes by default
    hide = set()

    def default(self, obj):  # pylint: disable=method-hidden
        """Convert dates to ISO format, and db.Models to dicts."""
        with suppress(AttributeError):
            return obj.isoformat() + 'Z'  # UTC
        with suppress(AttributeError):
            obj.hidden = self.hide
        return dict(obj)