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

Classes

class  ExtendedJSONEncoder
 
class  JSONEncoder
 

Functions

bytes _orjson_bytes_default_encoder (Any data)
 
str _orjson_default_encoder (Any data)
 
Any _strip_null (Any obj)
 
dict[str, Any] find_paths_unserializable_data (Any bad_data, *Callable[[Any], str] dump=json.dumps)
 
bytes json_bytes (Any obj)
 
bytes json_bytes_strip_null (Any data)
 
str json_dumps (Any data)
 
str json_dumps_sorted (Any data)
 
Any json_encoder_default (Any obj)
 
None save_json (str filename, list|dict data, bool private=False, *type[json.JSONEncoder]|None encoder=None, bool atomic_writes=False)
 

Variables

 __all__
 
 __dir__
 
 __getattr__
 
 _DEPRECATED_JSON_DECODE_EXCEPTIONS
 
 _DEPRECATED_JSON_ENCODE_EXCEPTIONS
 
 _LOGGER
 
 breaks_in_ha_version
 
 check_if_deprecated_constant
 
 default = json_encoder_default,
 
 dir_with_deprecated_constants
 
 dumps
 
 json_bytes
 
 json_bytes_sorted
 
 json_fragment
 
 json_loads
 
 module_globals
 
 module_globals_keys
 
 OPT_NON_STR_KEYS
 
 option = orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS,
 

Detailed Description

Helpers to help with encoding Home Assistant objects in JSON.

Function Documentation

◆ _orjson_bytes_default_encoder()

bytes homeassistant.helpers.json._orjson_bytes_default_encoder ( Any  data)
private
JSON encoder that uses orjson with hass defaults and returns bytes.

Definition at line 186 of file json.py.

◆ _orjson_default_encoder()

str homeassistant.helpers.json._orjson_default_encoder ( Any  data)
private
JSON encoder that uses orjson with hass defaults and returns a str.

Definition at line 181 of file json.py.

◆ _strip_null()

Any homeassistant.helpers.json._strip_null ( Any  obj)
private
Strip NUL from an object.

Definition at line 122 of file json.py.

◆ find_paths_unserializable_data()

dict[str, Any] homeassistant.helpers.json.find_paths_unserializable_data ( Any  bad_data,
*Callable[[Any], str]   dump = json.dumps 
)
Find the paths to unserializable data.

This method is slow! Only use for error handling.

Definition at line 231 of file json.py.

◆ json_bytes()

bytes homeassistant.helpers.json.json_bytes ( Any  obj)
Dump json bytes.

Definition at line 92 of file json.py.

◆ json_bytes_strip_null()

bytes homeassistant.helpers.json.json_bytes_strip_null ( Any  data)
Dump json bytes after terminating strings at the first NUL.

Definition at line 133 of file json.py.

◆ json_dumps()

str homeassistant.helpers.json.json_dumps ( Any  data)
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

Definition at line 149 of file json.py.

◆ json_dumps_sorted()

str homeassistant.helpers.json.json_dumps_sorted ( Any  data)
Dump json string with keys sorted.

Definition at line 173 of file json.py.

◆ json_encoder_default()

Any homeassistant.helpers.json.json_encoder_default ( Any  obj)
Convert Home Assistant objects.

Hand other objects to the original method.

Definition at line 70 of file json.py.

◆ save_json()

None homeassistant.helpers.json.save_json ( str  filename,
list | dict  data,
bool   private = False,
*type[json.JSONEncoder] | None   encoder = None,
bool   atomic_writes = False 
)
Save JSON data to a file.

Definition at line 195 of file json.py.

Variable Documentation

◆ __all__

homeassistant.helpers.json.__all__
private

Definition at line 46 of file json.py.

◆ __dir__

homeassistant.helpers.json.__dir__
private

Definition at line 43 of file json.py.

◆ __getattr__

homeassistant.helpers.json.__getattr__
private

Definition at line 42 of file json.py.

◆ _DEPRECATED_JSON_DECODE_EXCEPTIONS

homeassistant.helpers.json._DEPRECATED_JSON_DECODE_EXCEPTIONS
private

Definition at line 31 of file json.py.

◆ _DEPRECATED_JSON_ENCODE_EXCEPTIONS

homeassistant.helpers.json._DEPRECATED_JSON_ENCODE_EXCEPTIONS
private

Definition at line 34 of file json.py.

◆ _LOGGER

homeassistant.helpers.json._LOGGER
private

Definition at line 49 of file json.py.

◆ breaks_in_ha_version

homeassistant.helpers.json.breaks_in_ha_version

Definition at line 38 of file json.py.

◆ check_if_deprecated_constant

homeassistant.helpers.json.check_if_deprecated_constant

Definition at line 42 of file json.py.

◆ default

Any homeassistant.helpers.json.default = json_encoder_default,

Definition at line 97 of file json.py.

◆ dir_with_deprecated_constants

homeassistant.helpers.json.dir_with_deprecated_constants

Definition at line 44 of file json.py.

◆ dumps

homeassistant.helpers.json.dumps

Definition at line 97 of file json.py.

◆ json_bytes

homeassistant.helpers.json.json_bytes

Definition at line 96 of file json.py.

◆ json_bytes_sorted

homeassistant.helpers.json.json_bytes_sorted

Definition at line 165 of file json.py.

◆ json_fragment

homeassistant.helpers.json.json_fragment

Definition at line 146 of file json.py.

◆ json_loads

homeassistant.helpers.json.json_loads

Definition at line 37 of file json.py.

◆ module_globals

homeassistant.helpers.json.module_globals

Definition at line 42 of file json.py.

◆ module_globals_keys

homeassistant.helpers.json.module_globals_keys

Definition at line 44 of file json.py.

◆ OPT_NON_STR_KEYS

homeassistant.helpers.json.OPT_NON_STR_KEYS

Definition at line 97 of file json.py.

◆ option

homeassistant.helpers.json.option = orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS,

Definition at line 97 of file json.py.