From 89dbc2fc14c1021d70f923bc90f55063ad6aeaae Mon Sep 17 00:00:00 2001 From: Leonardo Taccari Date: Thu, 9 May 2019 00:01:54 +0200 Subject: [PATCH] [postprocessor:metadata] handle datetime objects when encoding JSON Introduce a GalleryDLJSONEncoder to encode datetime.datetime as ISO 8601 strings. --- gallery_dl/postprocessor/metadata.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gallery_dl/postprocessor/metadata.py b/gallery_dl/postprocessor/metadata.py index 3aae9431d6..63c7e331b2 100644 --- a/gallery_dl/postprocessor/metadata.py +++ b/gallery_dl/postprocessor/metadata.py @@ -10,6 +10,7 @@ from .common import PostProcessor from .. import util +import datetime import json @@ -61,12 +62,20 @@ def _write_tags(self, file, pathfmt): file.write("\n") def _write_json(self, file, pathfmt): + class GalleryDLJSONEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, datetime.datetime): + return obj.isoformat() + # Let the base class default method raise the TypeError + return json.JSONEncoder.default(self, obj) + json.dump( pathfmt.keywords, file, sort_keys=True, indent=self.indent, ensure_ascii=self.ascii, + cls=GalleryDLJSONEncoder, )