Home Assistant Unofficial Reference 2024.12.1
homeassistant.util.json Namespace Reference

Classes

class  SerializationError
 

Functions

str format_unserializable_data (dict[str, Any] data)
 
JsonValueType json_loads (bytes|bytearray|memoryview|str obj)
 
JsonArrayType json_loads_array (bytes|bytearray|memoryview|str obj)
 
JsonObjectType json_loads_object (bytes|bytearray|memoryview|str obj)
 
JsonValueType load_json (str|PathLike[str] filename, JsonValueType default=_SENTINEL)
 
JsonArrayType load_json_array (str|PathLike[str] filename, JsonArrayType default=_SENTINEL)
 
JsonObjectType load_json_object (str|PathLike[str] filename, JsonObjectType default=_SENTINEL)
 

Variables

 _LOGGER = logging.getLogger(__name__)
 
 _SENTINEL = object()
 
tuple JSON_DECODE_EXCEPTIONS = (orjson.JSONDecodeError,)
 
tuple JSON_ENCODE_EXCEPTIONS = (TypeError, ValueError)
 
 JsonArrayType
 
 JsonObjectType
 
 JsonValueType
 

Detailed Description

JSON utility functions.

Function Documentation

◆ format_unserializable_data()

str homeassistant.util.json.format_unserializable_data ( dict[str, Any]  data)
Format output of find_paths in a friendly way.

Format is comma separated: <path>=<value>(<type>)

Definition at line 126 of file json.py.

◆ json_loads()

JsonValueType homeassistant.util.json.json_loads ( bytes | bytearray | memoryview | str  obj)
Parse JSON data.

This adds a workaround for orjson not handling subclasses of str,
https://github.com/ijl/orjson/issues/445.

Definition at line 33 of file json.py.

◆ json_loads_array()

JsonArrayType homeassistant.util.json.json_loads_array ( bytes | bytearray | memoryview | str  obj)
Parse JSON data and ensure result is a list.

Definition at line 45 of file json.py.

◆ json_loads_object()

JsonObjectType homeassistant.util.json.json_loads_object ( bytes | bytearray | memoryview | str  obj)
Parse JSON data and ensure result is a dictionary.

Definition at line 54 of file json.py.

◆ load_json()

JsonValueType homeassistant.util.json.load_json ( str | PathLike[str]  filename,
JsonValueType   default = _SENTINEL 
)
Load JSON data from a file.

Defaults to returning empty dict if file is not found.

Definition at line 63 of file json.py.

◆ load_json_array()

JsonArrayType homeassistant.util.json.load_json_array ( str | PathLike[str]  filename,
JsonArrayType   default = _SENTINEL 
)
Load JSON data from a file and return as list.

Defaults to returning empty list if file is not found.

Definition at line 86 of file json.py.

◆ load_json_object()

JsonObjectType homeassistant.util.json.load_json_object ( str | PathLike[str]  filename,
JsonObjectType   default = _SENTINEL 
)
Load JSON data from a file and return as dict.

Defaults to returning empty dict if file is not found.

Definition at line 106 of file json.py.

Variable Documentation

◆ _LOGGER

homeassistant.util.json._LOGGER = logging.getLogger(__name__)
private

Definition at line 14 of file json.py.

◆ _SENTINEL

homeassistant.util.json._SENTINEL = object()
private

Definition at line 13 of file json.py.

◆ JSON_DECODE_EXCEPTIONS

tuple homeassistant.util.json.JSON_DECODE_EXCEPTIONS = (orjson.JSONDecodeError,)

Definition at line 26 of file json.py.

◆ JSON_ENCODE_EXCEPTIONS

tuple homeassistant.util.json.JSON_ENCODE_EXCEPTIONS = (TypeError, ValueError)

Definition at line 25 of file json.py.

◆ JsonArrayType

homeassistant.util.json.JsonArrayType

Definition at line 20 of file json.py.

◆ JsonObjectType

homeassistant.util.json.JsonObjectType

Definition at line 22 of file json.py.

◆ JsonValueType

homeassistant.util.json.JsonValueType

Definition at line 16 of file json.py.