python-kasa/kasa/json.py

35 lines
849 B
Python
Raw Normal View History

"""JSON abstraction."""
2024-11-10 18:55:13 +00:00
from __future__ import annotations
from collections.abc import Callable
from typing import Any
2024-11-10 18:55:13 +00:00
try:
import orjson
2024-11-10 18:55:13 +00:00
def dumps(obj: Any, *, default: Callable | None = None) -> str:
"""Dump JSON."""
return orjson.dumps(obj).decode()
loads = orjson.loads
except ImportError:
import json
2024-11-10 18:55:13 +00:00
def dumps(obj: Any, *, default: Callable | None = None) -> str:
"""Dump JSON."""
# Separators specified for consistency with orjson
return json.dumps(obj, separators=(",", ":"))
loads = json.loads
try:
from mashumaro.mixins.orjson import DataClassORJSONMixin
DataClassJSONMixin = DataClassORJSONMixin
except ImportError:
from mashumaro.mixins.json import DataClassJSONMixin as JSONMixin
DataClassJSONMixin = JSONMixin # type: ignore[assignment, misc]