forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_json.py
More file actions
35 lines (25 loc) · 921 Bytes
/
Copy pathpatch_json.py
File metadata and controls
35 lines (25 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Patch JSON related functions."""
import functools
from typing import Any
from unittest import mock
import orjson
from homeassistant.helpers import json as json_helper
real_json_encoder_default = json_helper.json_encoder_default
mock_objects = []
def json_encoder_default(obj: Any) -> Any:
"""Convert Home Assistant objects.
Hand other objects to the original method.
"""
if isinstance(obj, mock.Base):
mock_objects.append(obj)
raise TypeError(f"Attempting to serialize mock object {obj}")
return real_json_encoder_default(obj)
json_helper.json_encoder_default = json_encoder_default
json_helper.json_bytes = functools.partial(
orjson.dumps, option=orjson.OPT_NON_STR_KEYS, default=json_encoder_default
)
json_helper.json_bytes_sorted = functools.partial(
orjson.dumps,
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS,
default=json_encoder_default,
)