1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add pretty printing, key sorting, and better performance to to_json in Jinja (#91253)

Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
David Poll
2023-04-12 17:32:13 -05:00
committed by GitHub
parent 9b2e9b8746
commit ea12d7a86f
2 changed files with 70 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Iterable
from datetime import datetime, timedelta
import json
import logging
import math
import random
@@ -10,6 +11,7 @@ from typing import Any
from unittest.mock import patch
from freezegun import freeze_time
import orjson
import pytest
import voluptuous as vol
@@ -1047,14 +1049,31 @@ def test_to_json(hass: HomeAssistant) -> None:
).async_render()
assert actual_result == expected_result
expected_result = orjson.dumps({"Foo": "Bar"}, option=orjson.OPT_INDENT_2).decode()
actual_result = template.Template(
"{{ {'Foo': 'Bar'} | to_json(pretty_print=True) }}", hass
).async_render(parse_result=False)
assert actual_result == expected_result
def test_to_json_string(hass: HomeAssistant) -> None:
expected_result = orjson.dumps(
{"Z": 26, "A": 1, "M": 13}, option=orjson.OPT_SORT_KEYS
).decode()
actual_result = template.Template(
"{{ {'Z': 26, 'A': 1, 'M': 13} | to_json(sort_keys=True) }}", hass
).async_render(parse_result=False)
assert actual_result == expected_result
with pytest.raises(TemplateError):
template.Template("{{ {'Foo': now()} | to_json }}", hass).async_render()
def test_to_json_ensure_ascii(hass: HomeAssistant) -> None:
"""Test the object to JSON string filter."""
# Note that we're not testing the actual json.loads and json.dumps methods,
# only the filters, so we don't need to be exhaustive with our sample JSON.
actual_value_ascii = template.Template(
"{{ 'Bar ҝ éèà' | to_json }}", hass
"{{ 'Bar ҝ éèà' | to_json(ensure_ascii=True) }}", hass
).async_render()
assert actual_value_ascii == '"Bar \\u049d \\u00e9\\u00e8\\u00e0"'
actual_value = template.Template(
@@ -1062,6 +1081,19 @@ def test_to_json_string(hass: HomeAssistant) -> None:
).async_render()
assert actual_value == '"Bar ҝ éèà"'
expected_result = json.dumps({"Foo": "Bar"}, indent=2)
actual_result = template.Template(
"{{ {'Foo': 'Bar'} | to_json(pretty_print=True, ensure_ascii=True) }}", hass
).async_render(parse_result=False)
assert actual_result == expected_result
expected_result = json.dumps({"Z": 26, "A": 1, "M": 13}, sort_keys=True)
actual_result = template.Template(
"{{ {'Z': 26, 'A': 1, 'M': 13} | to_json(sort_keys=True, ensure_ascii=True) }}",
hass,
).async_render(parse_result=False)
assert actual_result == expected_result
def test_from_json(hass: HomeAssistant) -> None:
"""Test the JSON string to object filter."""