add 'j' format string conversion

to convert to a JSON formatted string
pull/1853/head
Mike Fährmann 3 years ago
parent 6651da27e9
commit 292fffc83c
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -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!C}</code></td>
<td><code>Foo Bar</code></td> <td><code>Foo Bar</code></td>
</tr> </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> <tr>
<td align="center"><code>t</code></td> <td align="center"><code>t</code></td>
<td>Trim a string, i.e. remove leading and trailing whitespace characters</td> <td>Trim a string, i.e. remove leading and trailing whitespace characters</td>

@ -547,6 +547,7 @@ class Formatter():
- "u": calls str.upper - "u": calls str.upper
- "c": calls str.capitalize - "c": calls str.capitalize
- "C": calls string.capwords - "C": calls string.capwords
- "j". calls json.dumps
- "t": calls str.strip - "t": calls str.strip
- "d": calls text.parse_timestamp - "d": calls text.parse_timestamp
- "U": calls urllib.parse.unquote - "U": calls urllib.parse.unquote
@ -581,6 +582,7 @@ class Formatter():
"u": str.upper, "u": str.upper,
"c": str.capitalize, "c": str.capitalize,
"C": string.capwords, "C": string.capwords,
"j": json.dumps,
"t": str.strip, "t": str.strip,
"T": to_timestamp, "T": to_timestamp,
"d": text.parse_timestamp, "d": text.parse_timestamp,

@ -297,6 +297,7 @@ class TestFormatter(unittest.TestCase):
self._run_test("{t!d}", datetime.datetime(2010, 1, 1)) self._run_test("{t!d}", datetime.datetime(2010, 1, 1))
self._run_test("{t!d:%Y-%m-%d}", "2010-01-01") self._run_test("{t!d:%Y-%m-%d}", "2010-01-01")
self._run_test("{dt!T}", "1262304000") self._run_test("{dt!T}", "1262304000")
self._run_test("{l!j}", '["a", "b", "c"]')
with self.assertRaises(KeyError): with self.assertRaises(KeyError):
self._run_test("{a!q}", "hello world") self._run_test("{a!q}", "hello world")

Loading…
Cancel
Save