diff options
| author | Alexis Metaireau <alexis@notmyidea.org> | 2018-07-16 22:58:48 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-07-16 22:58:48 +0200 |
| commit | 1d0880f3cb39463483a4241197d8eb0d817dffc6 (patch) | |
| tree | ffb843f74d1d1337a6e66d92b5deb21c0dd9c77e /ihatemoney/utils.py | |
| parent | c3f8ddd274a40b164b5fceeab44c1c26cf053b04 (diff) | |
| parent | f9cc4e56230ce04f58d457bfc8f468d56e53cb36 (diff) | |
| download | ihatemoney-mirror-1d0880f3cb39463483a4241197d8eb0d817dffc6.zip ihatemoney-mirror-1d0880f3cb39463483a4241197d8eb0d817dffc6.tar.gz ihatemoney-mirror-1d0880f3cb39463483a4241197d8eb0d817dffc6.tar.bz2 | |
Merge branch 'master' into almet/fix-supervisord-template
Diffstat (limited to 'ihatemoney/utils.py')
| -rw-r--r-- | ihatemoney/utils.py | 27 |
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) |
