Skip to content

Commit eab53f8

Browse files
koyingemontnemery
andauthored
Allow overriding ensure_ascii in the "to_json" template filter (home-assistant#20218)
* to_json: add ensure_ascii * Update source/_docs/configuration/templating.markdown Co-authored-by: Erik Montnemery <erik@montnemery.com> * lint Co-authored-by: Erik Montnemery <erik@montnemery.com>
1 parent e32c562 commit eab53f8

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

source/_docs/configuration/templating.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ To fix it, enforce the ISO conversion via `isoformat()`:
357357

358358
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.
359359

360+
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.
361+
360362
The `from_json` filter operates similarly, but in the other direction, de-serializing a JSON string back into an object.
361363

362364
### To/From JSON examples
@@ -370,7 +372,7 @@ In this example, the special character '°' will be automatically escaped in ord
370372
```text
371373
{% set temp = {'temperature': 25, 'unit': '°C'} %}
372374
stringified object: {{ temp }}
373-
object|to_json: {{ temp|to_json }}
375+
object|to_json: {{ temp|to_json(ensure_ascii=False) }}
374376
```
375377

376378
{% endraw %}

0 commit comments

Comments
 (0)