diff options
Diffstat (limited to 'api')
| -rwxr-xr-x | api/main.py | 2 | ||||
| -rw-r--r-- | api/opendc/util/json.py | 12 |
2 files changed, 14 insertions, 0 deletions
diff --git a/api/main.py b/api/main.py index 7544333a..46782638 100755 --- a/api/main.py +++ b/api/main.py @@ -15,6 +15,7 @@ from oauth2client import client, crypt from opendc.models.user import User from opendc.util import rest, path_parser, database from opendc.util.exceptions import AuthorizationTokenError, RequestInitializationError +from opendc.util.json import JSONEncoder load_dotenv() @@ -31,6 +32,7 @@ if not TEST_MODE: # Set up the core app FLASK_CORE_APP = Flask(__name__) FLASK_CORE_APP.config['SECRET_KEY'] = os.environ['OPENDC_FLASK_SECRET'] +FLASK_CORE_APP.json_encoder = JSONEncoder # Set up CORS support for local setups if 'localhost' in os.environ['OPENDC_SERVER_BASE_URL']: diff --git a/api/opendc/util/json.py b/api/opendc/util/json.py new file mode 100644 index 00000000..2ef4f965 --- /dev/null +++ b/api/opendc/util/json.py @@ -0,0 +1,12 @@ +import flask +from bson.objectid import ObjectId + + +class JSONEncoder(flask.json.JSONEncoder): + """ + A customized JSON encoder to handle unsupported types. + """ + def default(self, o): + if isinstance(o, ObjectId): + return str(o) + return flask.json.JSONEncoder.default(self, o) |
