This was causing the wheels to fail to build. We need
to workout why when we don't have release pressure
This reverts commit d9d22a9556
.
This commit is contained in:
parent
9cea936c22
commit
c365454afb
18 changed files with 67 additions and 127 deletions
|
@ -1,10 +1,7 @@
|
|||
"""Helpers to help with encoding Home Assistant objects in JSON."""
|
||||
import datetime
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any, Final
|
||||
|
||||
import orjson
|
||||
from typing import Any
|
||||
|
||||
|
||||
class JSONEncoder(json.JSONEncoder):
|
||||
|
@ -25,20 +22,6 @@ class JSONEncoder(json.JSONEncoder):
|
|||
return json.JSONEncoder.default(self, o)
|
||||
|
||||
|
||||
def json_encoder_default(obj: Any) -> Any:
|
||||
"""Convert Home Assistant objects.
|
||||
|
||||
Hand other objects to the original method.
|
||||
"""
|
||||
if isinstance(obj, set):
|
||||
return list(obj)
|
||||
if hasattr(obj, "as_dict"):
|
||||
return obj.as_dict()
|
||||
if isinstance(obj, Path):
|
||||
return obj.as_posix()
|
||||
raise TypeError
|
||||
|
||||
|
||||
class ExtendedJSONEncoder(JSONEncoder):
|
||||
"""JSONEncoder that supports Home Assistant objects and falls back to repr(o)."""
|
||||
|
||||
|
@ -57,31 +40,3 @@ class ExtendedJSONEncoder(JSONEncoder):
|
|||
return super().default(o)
|
||||
except TypeError:
|
||||
return {"__type": str(type(o)), "repr": repr(o)}
|
||||
|
||||
|
||||
def json_bytes(data: Any) -> bytes:
|
||||
"""Dump json bytes."""
|
||||
return orjson.dumps(
|
||||
data, option=orjson.OPT_NON_STR_KEYS, default=json_encoder_default
|
||||
)
|
||||
|
||||
|
||||
def json_dumps(data: Any) -> str:
|
||||
"""Dump json string.
|
||||
|
||||
orjson supports serializing dataclasses natively which
|
||||
eliminates the need to implement as_dict in many places
|
||||
when the data is already in a dataclass. This works
|
||||
well as long as all the data in the dataclass can also
|
||||
be serialized.
|
||||
|
||||
If it turns out to be a problem we can disable this
|
||||
with option |= orjson.OPT_PASSTHROUGH_DATACLASS and it
|
||||
will fallback to as_dict
|
||||
"""
|
||||
return orjson.dumps(
|
||||
data, option=orjson.OPT_NON_STR_KEYS, default=json_encoder_default
|
||||
).decode("utf-8")
|
||||
|
||||
|
||||
JSON_DUMP: Final = json_dumps
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue