From 292fffc83ce0aad45e226090030d34d4cabcf703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 28 Aug 2021 01:19:36 +0200 Subject: [PATCH] add 'j' format string conversion to convert to a JSON formatted string --- docs/formatting.md | 6 ++++++ gallery_dl/util.py | 2 ++ test/test_util.py | 1 + 3 files changed, 9 insertions(+) diff --git a/docs/formatting.md b/docs/formatting.md index dbcdb6ee..624ab5d7 100644 --- a/docs/formatting.md +++ b/docs/formatting.md @@ -62,6 +62,12 @@ Conversion specifiers allow to *convert* the value to a different form or type. {foo!C} Foo Bar + + j + Serialize value to a JSON formatted string + {tags!j} + ["sun", "tree", "water"] + t Trim a string, i.e. remove leading and trailing whitespace characters diff --git a/gallery_dl/util.py b/gallery_dl/util.py index b1ed15ab..935bf99f 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -547,6 +547,7 @@ class Formatter(): - "u": calls str.upper - "c": calls str.capitalize - "C": calls string.capwords + - "j". calls json.dumps - "t": calls str.strip - "d": calls text.parse_timestamp - "U": calls urllib.parse.unquote @@ -581,6 +582,7 @@ class Formatter(): "u": str.upper, "c": str.capitalize, "C": string.capwords, + "j": json.dumps, "t": str.strip, "T": to_timestamp, "d": text.parse_timestamp, diff --git a/test/test_util.py b/test/test_util.py index 7a31ebbd..1aa66d1c 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -297,6 +297,7 @@ class TestFormatter(unittest.TestCase): self._run_test("{t!d}", datetime.datetime(2010, 1, 1)) self._run_test("{t!d:%Y-%m-%d}", "2010-01-01") self._run_test("{dt!T}", "1262304000") + self._run_test("{l!j}", '["a", "b", "c"]') with self.assertRaises(KeyError): self._run_test("{a!q}", "hello world")