Skip to content

Commit

Permalink
add 'j' format string conversion
Browse files Browse the repository at this point in the history
to convert to a JSON formatted string
  • Loading branch information
mikf committed Aug 27, 2021
1 parent 6651da2 commit 292fffc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ Conversion specifiers allow to *convert* the value to a different form or type.
<td><code>{foo!C}</code></td>
<td><code>Foo Bar</code></td>
</tr>
<tr>
<td align="center"><code>j</code></td>
<td>Serialize value to a JSON formatted string</td>
<td><code>{tags!j}</code></td>
<td><code>["sun", "tree", "water"]</code></td>
</tr>
<tr>
<td align="center"><code>t</code></td>
<td>Trim a string, i.e. remove leading and trailing whitespace characters</td>
Expand Down
2 changes: 2 additions & 0 deletions gallery_dl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ def test_conversions(self):
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")
Expand Down

0 comments on commit 292fffc

Please sign in to comment.