From 192a0869f731bbb75948066590621a9bb7db643d Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Mon, 25 Oct 2021 15:14:55 +1100 Subject: [PATCH] thrimshim: Fix bug in serializing columns Converting all datetime values to string no longer works because video_range isn't a datetime but a list of datetimes. We switch to a more robust approach of using the json "default" arg to specify how to serialize. --- thrimshim/thrimshim/main.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/thrimshim/thrimshim/main.py b/thrimshim/thrimshim/main.py index aaf27ad..36790ff 100644 --- a/thrimshim/thrimshim/main.py +++ b/thrimshim/thrimshim/main.py @@ -162,12 +162,6 @@ def get_row(ident): response = row._asdict() response['id'] = str(response['id']) - response = { - key: ( - value.isoformat() if isinstance(value, datetime.datetime) - else value - ) for key, value in response.items() - } if response["video_channel"] is None: response["video_channel"] = app.default_channel response["title_prefix"] = app.title_header @@ -190,7 +184,14 @@ def get_row(ident): response["video_description"] = response["video_description"][:-len(app.description_footer)] logging.info('Row {} fetched'.format(ident)) - return json.dumps(response) + + def convert(value): + if isinstance(value, datetime.datetime): + return value.isoformat() + if isinstance(value, datetime.timedelta): + return value.total_seconds() + raise TypeError(f"Can't convert object of type {value.__class__.__name__} to JSON: {value}") + return json.dumps(response, default=convert) @app.route('/thrimshim/', methods=['POST'])