core/homeassistant/helpers/json.py

39 lines
1.1 KiB
Python
Raw Normal View History

"""Helpers to help with encoding Home Assistant objects in JSON."""
from datetime import datetime, timedelta
import json
from typing import Any
class JSONEncoder(json.JSONEncoder):
"""JSONEncoder that supports Home Assistant objects."""
def default(self, o: Any) -> Any:
"""Convert Home Assistant objects.
Hand other objects to the original method.
"""
if isinstance(o, datetime):
return o.isoformat()
if isinstance(o, set):
return list(o)
2019-07-31 19:25:30 +00:00
if hasattr(o, "as_dict"):
return o.as_dict()
return json.JSONEncoder.default(self, o)
class ExtendedJSONEncoder(JSONEncoder):
"""JSONEncoder that supports Home Assistant objects and falls back to repr(o)."""
def default(self, o: Any) -> Any:
"""Convert certain objects.
Fall back to repr(o).
"""
if isinstance(o, timedelta):
return {"__type": str(type(o)), "total_seconds": o.total_seconds()}
try:
return super().default(o)
except TypeError:
return {"__type": str(type(o)), "repr": repr(o)}