diff options
Diffstat (limited to 'lib/utils')
| -rw-r--r-- | lib/utils/geometry.py | 3 | ||||
| -rw-r--r-- | lib/utils/json.py | 15 |
2 files changed, 18 insertions, 0 deletions
diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py index 647636c8..bae32c32 100644 --- a/lib/utils/geometry.py +++ b/lib/utils/geometry.py @@ -63,6 +63,9 @@ class Point: self.x = x self.y = y + def __json__(self): + return vars(self) + def __add__(self, other): return Point(self.x + other.x, self.y + other.y) diff --git a/lib/utils/json.py b/lib/utils/json.py new file mode 100644 index 00000000..c670a46b --- /dev/null +++ b/lib/utils/json.py @@ -0,0 +1,15 @@ +from flask.json import JSONEncoder + + +class InkStitchJSONEncoder(JSONEncoder): + """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: + return JSONEncoder.default(self, obj) |
