Enable serialization of float subclasses with orjson (#74136)

This commit is contained in:
J. Nick Koston 2022-06-28 10:07:40 -05:00 committed by GitHub
parent a053a3a8a4
commit 2225d0e899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -35,6 +35,8 @@ def json_encoder_default(obj: Any) -> Any:
"""
if isinstance(obj, set):
return list(obj)
if isinstance(obj, float):
return float(obj)
if hasattr(obj, "as_dict"):
return obj.as_dict()
if isinstance(obj, Path):

View File

@ -8,6 +8,7 @@ from homeassistant import core
from homeassistant.helpers.json import (
ExtendedJSONEncoder,
JSONEncoder,
json_dumps,
json_dumps_sorted,
)
from homeassistant.util import dt as dt_util
@ -77,3 +78,12 @@ def test_json_dumps_sorted():
assert json_dumps_sorted(data) == json.dumps(
data, sort_keys=True, separators=(",", ":")
)
def test_json_dumps_float_subclass():
"""Test the json dumps a float subclass."""
class FloatSubclass(float):
"""A float subclass."""
assert json_dumps({"c": FloatSubclass(1.2)}) == '{"c":1.2}'