inkstitch/lib/utils/json.py

27 wiersze
804 B
Python
Czysty Zwykły widok Historia

2021-03-12 04:17:19 +00:00
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
2023-02-23 02:08:40 +00:00
from flask.json.provider import DefaultJSONProvider
from ..exceptions import InkstitchException
2020-04-28 16:34:05 +00:00
2023-02-23 02:08:40 +00:00
class InkstitchJSONException(InkstitchException):
pass
class InkStitchJSONProvider(DefaultJSONProvider):
2020-04-28 16:34:05 +00:00
"""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:
2023-02-23 02:08:40 +00:00
raise InkstitchJSONException(
f"Object of type {obj.__class__.__name__} cannot be serialized to JSON because it does not implement __json__()")