Skip to content

Commit

Permalink
Reduce duplicate code in json_loads (home-assistant#106859)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 2, 2024
1 parent 391123b commit a4f0c84
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions homeassistant/util/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ def json_loads(__obj: bytes | bytearray | memoryview | str) -> JsonValueType:
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):
# Avoid isinstance overhead for the common case
if type(__obj) not in (bytes, bytearray, memoryview, str) and isinstance(
__obj, str
):
return orjson.loads(str(__obj)) # type:ignore[no-any-return]
return orjson.loads(__obj) # type:ignore[no-any-return]

Expand Down

0 comments on commit a4f0c84

Please sign in to comment.