From c55fc0746372dc498071b542ad51c9f98cac81a9 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 21 Jun 2021 22:30:50 +0200 Subject: [PATCH] add custom json encoder --- ITPlanning/api/__init__.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ITPlanning/api/__init__.py b/ITPlanning/api/__init__.py index 6e5a747..55f692b 100644 --- a/ITPlanning/api/__init__.py +++ b/ITPlanning/api/__init__.py @@ -1,7 +1,23 @@ from flask import Blueprint +from flask.json import JSONEncoder +from datetime import datetime + + +class CustomJsonEncoder(JSONEncoder): + def default(self, obj): + try: + if isinstance(obj, datetime): + return obj.strftime("%Y-%m-%dT%H%M") + iterable = iter(obj) + except TypeError: + pass + else: + return list(iterable) + return JSONEncoder.default(self, obj) + api = Blueprint("api", __name__) - +api.json_encoder = CustomJsonEncoder from .errors import * from .appointement import * from .planning import *