From eab53f8c2cc7a9cd070d3fd02ff92726b8d5b441 Mon Sep 17 00:00:00 2001 From: Chris Browet Date: Mon, 8 Nov 2021 17:27:45 +0100 Subject: [PATCH] Allow overriding ensure_ascii in the "to_json" template filter (#20218) * to_json: add ensure_ascii * Update source/_docs/configuration/templating.markdown Co-authored-by: Erik Montnemery * lint Co-authored-by: Erik Montnemery --- source/_docs/configuration/templating.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/_docs/configuration/templating.markdown b/source/_docs/configuration/templating.markdown index a75db47168b..37d0e034e50 100644 --- a/source/_docs/configuration/templating.markdown +++ b/source/_docs/configuration/templating.markdown @@ -357,6 +357,8 @@ To fix it, enforce the ISO conversion via `isoformat()`: The `to_json` filter serializes an object to a JSON string. In some cases, it may be necessary to format a JSON string for use with a webhook, as a parameter for command-line utilities or any number of other applications. This can be complicated in a template, especially when dealing with escaping special characters. Using the `to_json` filter, this is handled automatically. +Similarly to the Python equivalent, the filter accepts an `ensure_ascii` parameter, defaulting to `True`. If `ensure_ascii` is `True`, the output is guaranteed to have all incoming non-ASCII characters escaped. If `ensure_ascii` is false, these characters will be output as-is. + The `from_json` filter operates similarly, but in the other direction, de-serializing a JSON string back into an object. ### To/From JSON examples @@ -370,7 +372,7 @@ In this example, the special character '°' will be automatically escaped in ord ```text {% set temp = {'temperature': 25, 'unit': '°C'} %} stringified object: {{ temp }} -object|to_json: {{ temp|to_json }} +object|to_json: {{ temp|to_json(ensure_ascii=False) }} ``` {% endraw %}