mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Switch frontend to use json helper (#73874)
This commit is contained in:
parent
3d59088a62
commit
edb386c736
@ -3,7 +3,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
@ -22,6 +21,7 @@ from homeassistant.const import CONF_MODE, CONF_NAME, EVENT_THEMES_UPDATED
|
|||||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||||
from homeassistant.helpers import service
|
from homeassistant.helpers import service
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.json import json_dumps_sorted
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
from homeassistant.helpers.translation import async_get_translations
|
from homeassistant.helpers.translation import async_get_translations
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
@ -135,7 +135,7 @@ class Manifest:
|
|||||||
return self._serialized
|
return self._serialized
|
||||||
|
|
||||||
def _serialize(self) -> None:
|
def _serialize(self) -> None:
|
||||||
self._serialized = json.dumps(self.manifest, sort_keys=True)
|
self._serialized = json_dumps_sorted(self.manifest)
|
||||||
|
|
||||||
def update_key(self, key: str, val: str) -> None:
|
def update_key(self, key: str, val: str) -> None:
|
||||||
"""Add a keyval to the manifest.json."""
|
"""Add a keyval to the manifest.json."""
|
||||||
|
@ -87,6 +87,15 @@ def json_dumps(data: Any) -> str:
|
|||||||
).decode("utf-8")
|
).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def json_dumps_sorted(data: Any) -> str:
|
||||||
|
"""Dump json string with keys sorted."""
|
||||||
|
return orjson.dumps(
|
||||||
|
data,
|
||||||
|
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS,
|
||||||
|
default=json_encoder_default,
|
||||||
|
).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
json_loads = orjson.loads
|
json_loads = orjson.loads
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
"""Test Home Assistant remote methods and classes."""
|
"""Test Home Assistant remote methods and classes."""
|
||||||
import datetime
|
import datetime
|
||||||
|
import json
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import core
|
from homeassistant import core
|
||||||
from homeassistant.helpers.json import ExtendedJSONEncoder, JSONEncoder
|
from homeassistant.helpers.json import (
|
||||||
|
ExtendedJSONEncoder,
|
||||||
|
JSONEncoder,
|
||||||
|
json_dumps_sorted,
|
||||||
|
)
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
|
|
||||||
@ -64,3 +69,11 @@ def test_extended_json_encoder(hass):
|
|||||||
# Default method falls back to repr(o)
|
# Default method falls back to repr(o)
|
||||||
o = object()
|
o = object()
|
||||||
assert ha_json_enc.default(o) == {"__type": str(type(o)), "repr": repr(o)}
|
assert ha_json_enc.default(o) == {"__type": str(type(o)), "repr": repr(o)}
|
||||||
|
|
||||||
|
|
||||||
|
def test_json_dumps_sorted():
|
||||||
|
"""Test the json dumps sorted function."""
|
||||||
|
data = {"c": 3, "a": 1, "b": 2}
|
||||||
|
assert json_dumps_sorted(data) == json.dumps(
|
||||||
|
data, sort_keys=True, separators=(",", ":")
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user