JSONize for QML any Collection, not just list/dict

This commit is contained in:
miruka 2021-04-14 13:52:27 -04:00
parent 30919eca7a
commit 719938d1db

View File

@ -21,8 +21,8 @@ from pathlib import Path
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from types import ModuleType from types import ModuleType
from typing import ( from typing import (
Any, AsyncIterator, Callable, Dict, Iterable, Mapping, Optional, Sequence, Any, AsyncIterator, Callable, Collection, Dict, Iterable, Mapping,
Tuple, Type, Union, Optional, Tuple, Type, Union,
) )
from uuid import UUID from uuid import UUID
@ -212,7 +212,7 @@ def serialize_value_for_qml(
- For `bool`, `int`, `float`, `bytes`, `str`, `datetime`, `date`, `time`: - For `bool`, `int`, `float`, `bytes`, `str`, `datetime`, `date`, `time`:
the unchanged value (PyOtherSide handles these) the unchanged value (PyOtherSide handles these)
- For `Iterable` objects (includes `list` and `dict`): - For `Collection` objects (includes `list` and `dict`):
a JSON dump if `json_list_dicts` is `True`, else the unchanged value a JSON dump if `json_list_dicts` is `True`, else the unchanged value
- If the value is an instancied object and has a `serialized` attribute or - If the value is an instancied object and has a `serialized` attribute or
@ -237,7 +237,9 @@ def serialize_value_for_qml(
if isinstance(value, (bool, int, float, bytes, str, datetime, date, time)): if isinstance(value, (bool, int, float, bytes, str, datetime, date, time)):
return value return value
if json_list_dicts and isinstance(value, (Sequence, Mapping)): if json_list_dicts and isinstance(value, Collection):
if isinstance(value, set):
value = list(value)
return json.dumps(value) return json.dumps(value)
if not inspect.isclass(value) and hasattr(value, "serialized"): if not inspect.isclass(value) and hasattr(value, "serialized"):