mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 22:07:10 +00:00
Workaround to_json
template filter in parsing dict key (#105327)
* Work-a-round orjson for `to_json` fiter in case dict key is str subclass * Add option instead * Remove json.dumps work-a-round * Update homeassistant/helpers/template.py * Fix test --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
parent
af715a4b9a
commit
4bb0e13cda
@ -2125,6 +2125,10 @@ def to_json(
|
|||||||
|
|
||||||
option = (
|
option = (
|
||||||
ORJSON_PASSTHROUGH_OPTIONS
|
ORJSON_PASSTHROUGH_OPTIONS
|
||||||
|
# OPT_NON_STR_KEYS is added as a workaround to
|
||||||
|
# ensure subclasses of str are allowed as dict keys
|
||||||
|
# See: https://github.com/ijl/orjson/issues/445
|
||||||
|
| orjson.OPT_NON_STR_KEYS
|
||||||
| (orjson.OPT_INDENT_2 if pretty_print else 0)
|
| (orjson.OPT_INDENT_2 if pretty_print else 0)
|
||||||
| (orjson.OPT_SORT_KEYS if sort_keys else 0)
|
| (orjson.OPT_SORT_KEYS if sort_keys else 0)
|
||||||
)
|
)
|
||||||
|
@ -1233,6 +1233,22 @@ def test_to_json(hass: HomeAssistant) -> None:
|
|||||||
with pytest.raises(TemplateError):
|
with pytest.raises(TemplateError):
|
||||||
template.Template("{{ {'Foo': now()} | to_json }}", hass).async_render()
|
template.Template("{{ {'Foo': now()} | to_json }}", hass).async_render()
|
||||||
|
|
||||||
|
# Test special case where substring class cannot be rendered
|
||||||
|
# See: https://github.com/ijl/orjson/issues/445
|
||||||
|
class MyStr(str):
|
||||||
|
pass
|
||||||
|
|
||||||
|
expected_result = '{"mykey1":11.0,"mykey2":"myvalue2","mykey3":["opt3b","opt3a"]}'
|
||||||
|
test_dict = {
|
||||||
|
MyStr("mykey2"): "myvalue2",
|
||||||
|
MyStr("mykey1"): 11.0,
|
||||||
|
MyStr("mykey3"): ["opt3b", "opt3a"],
|
||||||
|
}
|
||||||
|
actual_result = template.Template(
|
||||||
|
"{{ test_dict | to_json(sort_keys=True) }}", hass
|
||||||
|
).async_render(parse_result=False, variables={"test_dict": test_dict})
|
||||||
|
assert actual_result == expected_result
|
||||||
|
|
||||||
|
|
||||||
def test_to_json_ensure_ascii(hass: HomeAssistant) -> None:
|
def test_to_json_ensure_ascii(hass: HomeAssistant) -> None:
|
||||||
"""Test the object to JSON string filter."""
|
"""Test the object to JSON string filter."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user