mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Add workaround for orjson not handling subclasses of str (#105314)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
46d626280e
commit
af715a4b9a
@ -33,9 +33,17 @@ class SerializationError(HomeAssistantError):
|
|||||||
"""Error serializing the data to JSON."""
|
"""Error serializing the data to JSON."""
|
||||||
|
|
||||||
|
|
||||||
json_loads: Callable[[bytes | bytearray | memoryview | str], JsonValueType]
|
def json_loads(__obj: bytes | bytearray | memoryview | str) -> JsonValueType:
|
||||||
json_loads = orjson.loads
|
"""Parse JSON data.
|
||||||
"""Parse JSON data."""
|
|
||||||
|
This adds a workaround for orjson not handling subclasses of str,
|
||||||
|
https://github.com/ijl/orjson/issues/445.
|
||||||
|
"""
|
||||||
|
if type(__obj) in (bytes, bytearray, memoryview, str):
|
||||||
|
return orjson.loads(__obj) # type:ignore[no-any-return]
|
||||||
|
if isinstance(__obj, str):
|
||||||
|
return orjson.loads(str(__obj)) # type:ignore[no-any-return]
|
||||||
|
return orjson.loads(__obj) # type:ignore[no-any-return]
|
||||||
|
|
||||||
|
|
||||||
def json_loads_array(__obj: bytes | bytearray | memoryview | str) -> JsonArrayType:
|
def json_loads_array(__obj: bytes | bytearray | memoryview | str) -> JsonArrayType:
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
"""Test Home Assistant json utility functions."""
|
"""Test Home Assistant json utility functions."""
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import orjson
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.util.json import (
|
from homeassistant.util.json import (
|
||||||
|
json_loads,
|
||||||
json_loads_array,
|
json_loads_array,
|
||||||
json_loads_object,
|
json_loads_object,
|
||||||
load_json,
|
load_json,
|
||||||
@ -153,3 +155,20 @@ async def test_deprecated_save_json(
|
|||||||
save_json(fname, TEST_JSON_A)
|
save_json(fname, TEST_JSON_A)
|
||||||
assert "uses save_json from homeassistant.util.json" in caplog.text
|
assert "uses save_json from homeassistant.util.json" in caplog.text
|
||||||
assert "should be updated to use homeassistant.helpers.json module" in caplog.text
|
assert "should be updated to use homeassistant.helpers.json module" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
async def test_loading_derived_class():
|
||||||
|
"""Test loading data from classes derived from str."""
|
||||||
|
|
||||||
|
class MyStr(str):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MyBytes(bytes):
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert json_loads('"abc"') == "abc"
|
||||||
|
assert json_loads(MyStr('"abc"')) == "abc"
|
||||||
|
|
||||||
|
assert json_loads(b'"abc"') == "abc"
|
||||||
|
with pytest.raises(orjson.JSONDecodeError):
|
||||||
|
assert json_loads(MyBytes(b'"abc"')) == "abc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user