Reduce duplicate code in json_loads (#106859)

pull/106861/head
J. Nick Koston 2024-01-01 20:25:23 -10:00 committed by GitHub
parent 391123beb0
commit a4f0c84457
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 3 deletions

View File

@ -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]