mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Add support for JSON fragments (#107213)
This commit is contained in:
@@ -19,12 +19,15 @@ from homeassistant.helpers.json import (
|
||||
json_bytes_strip_null,
|
||||
json_dumps,
|
||||
json_dumps_sorted,
|
||||
json_fragment,
|
||||
save_json,
|
||||
)
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.color import RGBColor
|
||||
from homeassistant.util.json import SerializationError, load_json
|
||||
|
||||
from tests.common import json_round_trip
|
||||
|
||||
# Test data that can be saved as JSON
|
||||
TEST_JSON_A = {"a": 1, "B": "two"}
|
||||
TEST_JSON_B = {"a": "one", "B": 2}
|
||||
@@ -45,7 +48,8 @@ def test_json_encoder(hass: HomeAssistant, encoder: type[json.JSONEncoder]) -> N
|
||||
assert sorted(ha_json_enc.default(data)) == sorted(data)
|
||||
|
||||
# Test serializing an object which implements as_dict
|
||||
assert ha_json_enc.default(state) == state.as_dict()
|
||||
default = ha_json_enc.default(state)
|
||||
assert json_round_trip(default) == json_round_trip(state.as_dict())
|
||||
|
||||
|
||||
def test_json_encoder_raises(hass: HomeAssistant) -> None:
|
||||
@@ -133,6 +137,35 @@ def test_json_dumps_rgb_color_subclass() -> None:
|
||||
assert json_dumps(rgb) == "[4,2,1]"
|
||||
|
||||
|
||||
def test_json_fragments() -> None:
|
||||
"""Test the json dumps with a fragment."""
|
||||
|
||||
assert (
|
||||
json_dumps(
|
||||
[
|
||||
json_fragment('{"inner":"fragment2"}'),
|
||||
json_fragment('{"inner":"fragment2"}'),
|
||||
]
|
||||
)
|
||||
== '[{"inner":"fragment2"},{"inner":"fragment2"}]'
|
||||
)
|
||||
|
||||
class Fragment1:
|
||||
@property
|
||||
def json_fragment(self):
|
||||
return json_fragment('{"inner":"fragment1"}')
|
||||
|
||||
class Fragment2:
|
||||
@property
|
||||
def json_fragment(self):
|
||||
return json_fragment('{"inner":"fragment2"}')
|
||||
|
||||
assert (
|
||||
json_dumps([Fragment1(), Fragment2()])
|
||||
== '[{"inner":"fragment1"},{"inner":"fragment2"}]'
|
||||
)
|
||||
|
||||
|
||||
def test_json_bytes_strip_null() -> None:
|
||||
"""Test stripping nul from strings."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user