Handle non-str keys when storing json data (#73958)

This commit is contained in:
J. Nick Koston 2022-06-24 09:59:01 -05:00 committed by GitHub
parent b880a05e45
commit 1866a1e925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -45,6 +45,13 @@ def load_json(filename: str, default: list | dict | None = None) -> list | dict:
return {} if default is None else default
def _orjson_encoder(data: Any) -> str:
"""JSON encoder that uses orjson."""
return orjson.dumps(
data, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS
).decode("utf-8")
def save_json(
filename: str,
data: list | dict,
@ -62,8 +69,8 @@ def save_json(
if encoder:
json_data = json.dumps(data, indent=2, cls=encoder)
else:
dump = orjson.dumps
json_data = orjson.dumps(data, option=orjson.OPT_INDENT_2).decode("utf-8")
dump = _orjson_encoder
json_data = _orjson_encoder(data)
except TypeError as error:
msg = f"Failed to serialize to JSON: {filename}. Bad data at {format_unserializable_data(find_paths_unserializable_data(data, dump=dump))}"
_LOGGER.error(msg)

View File

@ -52,6 +52,14 @@ def test_save_and_load():
assert data == TEST_JSON_A
def test_save_and_load_int_keys():
"""Test saving and loading back stringifies the keys."""
fname = _path_for("test1")
save_json(fname, {1: "a", 2: "b"})
data = load_json(fname)
assert data == {"1": "a", "2": "b"}
def test_save_and_load_private():
"""Test we can load private files and that they are protected."""
fname = _path_for("test2")