summaryrefslogtreecommitdiff
path: root/lib/utils/json.py
blob: 22107c48977ad43c862a8eeb098ae2bf8456c8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

from flask.json.provider import DefaultJSONProvider
from ..exceptions import InkstitchException


class InkstitchJSONException(InkstitchException):
    pass


class InkStitchJSONProvider(DefaultJSONProvider):
    """JSON encoder class that runs __json__ on an object if available.

    The __json__ method should return a JSON-compatible representation of the
    object.
    """

    def default(self, obj):
        try:
            return obj.__json__()
        except AttributeError:
            raise InkstitchJSONException(
                f"Object of type {obj.__class__.__name__} cannot be serialized to JSON because it does not implement __json__()")