Changed default flask json encoder so we could encode objects with custom fields, like field of type ObjectId

This commit is contained in:
VakarisZ 2020-09-18 09:28:31 +03:00
parent 96f3052dc2
commit 5a6a68fde0
2 changed files with 16 additions and 0 deletions

View File

@ -8,6 +8,7 @@ from werkzeug.exceptions import NotFound
import monkey_island.cc.environment.environment_singleton as env_singleton
from common.common_consts.api_url_consts import T1216_PBA_FILE_DOWNLOAD_PATH
from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.custom_json_encoder import CustomJSONEncoder
from monkey_island.cc.database import database, mongo
from monkey_island.cc.resources.attack.attack_config import AttackConfiguration
from monkey_island.cc.resources.attack.attack_report import AttackReport
@ -85,6 +86,8 @@ def init_app_config(app, mongo_url):
# configuration. See https://flask.palletsprojects.com/en/1.1.x/config/#JSON_SORT_KEYS.
app.config['JSON_SORT_KEYS'] = False
app.json_encoder = CustomJSONEncoder
def init_app_services(app):
init_jwt(app)

View File

@ -0,0 +1,13 @@
from bson import ObjectId
from flask.json import JSONEncoder
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
try:
if isinstance(obj, ObjectId):
return obj.__str__()
except TypeError:
pass
return JSONEncoder.default(self, obj)