Add return type to json_loads (#85672)
* Add JSON type definitions * Sample use * Keep mutable for a follo-up PR (avoid dead code) * Use list/dict * Remove JsonObjectType * Remove reference to Union * Cleanup * Improve rest * Rename json_dict => json_data * Add docstring * Add type hint to json_loads * Add cast * Move type alias to json helpers * Cleanup * Create and use json_loads_object * Make error more explicit and add tests * Use JsonObjectType in conversation * Remove quotes
This commit is contained in:
parent
20b60d57f2
commit
a202588fd2
11 changed files with 70 additions and 28 deletions
|
@ -13,6 +13,7 @@ from homeassistant.helpers.json import (
|
|||
json_bytes_strip_null,
|
||||
json_dumps,
|
||||
json_dumps_sorted,
|
||||
json_loads_object,
|
||||
)
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.color import RGBColor
|
||||
|
@ -135,3 +136,20 @@ def test_json_bytes_strip_null() -> None:
|
|||
json_bytes_strip_null([[{"k1": {"k2": ["silly\0stuff"]}}]])
|
||||
== b'[[{"k1":{"k2":["silly"]}}]]'
|
||||
)
|
||||
|
||||
|
||||
def test_json_loads_object():
|
||||
"""Test json_loads_object validates result."""
|
||||
assert json_loads_object('{"c":1.2}') == {"c": 1.2}
|
||||
with pytest.raises(
|
||||
ValueError, match="Expected JSON to be parsed as a dict got <class 'list'>"
|
||||
):
|
||||
json_loads_object("[]")
|
||||
with pytest.raises(
|
||||
ValueError, match="Expected JSON to be parsed as a dict got <class 'bool'>"
|
||||
):
|
||||
json_loads_object("true")
|
||||
with pytest.raises(
|
||||
ValueError, match="Expected JSON to be parsed as a dict got <class 'NoneType'>"
|
||||
):
|
||||
json_loads_object("null")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue