add 'j' format string conversion
to convert to a JSON formatted string
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user