aboutsummaryrefslogtreecommitdiff
path: root/ihatemoney/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'ihatemoney/utils.py')
-rw-r--r--ihatemoney/utils.py27
1 files changed, 26 insertions, 1 deletions
diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py
index a25e3b9..5dd1e7b 100644
--- a/ihatemoney/utils.py
+++ b/ihatemoney/utils.py
@@ -3,7 +3,7 @@ import re
from io import BytesIO, StringIO
import jinja2
-from json import dumps
+from json import dumps, JSONEncoder
from flask import redirect
from werkzeug.routing import HTTPException, RoutingException
import six
@@ -185,3 +185,28 @@ def create_jinja_env(folder, strict_rendering=False):
if strict_rendering:
kwargs['undefined'] = jinja2.StrictUndefined
return jinja2.Environment(**kwargs)
+
+
+class IhmJSONEncoder(JSONEncoder):
+ """Subclass of the default encoder to support custom objects.
+ Taken from the deprecated flask-rest package."""
+ def default(self, o):
+ if hasattr(o, "_to_serialize"):
+ # build up the object
+ data = {}
+ for attr in o._to_serialize:
+ data[attr] = getattr(o, attr)
+ return data
+ elif hasattr(o, "isoformat"):
+ return o.isoformat()
+ else:
+ try:
+ from flask_babel import speaklater
+ if isinstance(o, speaklater.LazyString):
+ try:
+ return unicode(o) # For python 2.
+ except NameError:
+ return str(o) # For python 3.
+ except ImportError:
+ pass
+ return JSONEncoder.default(self, o)