2023-06-17 23:03:04 +00:00
|
|
|
"""JSON abstraction."""
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-18 18:46:36 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from typing import Any
|
2024-11-10 18:55:13 +00:00
|
|
|
|
2023-06-17 23:03:04 +00:00
|
|
|
try:
|
|
|
|
import orjson
|
|
|
|
|
2024-12-10 22:42:14 +00:00
|
|
|
def dumps(
|
|
|
|
obj: Any, *, default: Callable | None = None, indent: bool = False
|
|
|
|
) -> str:
|
2023-06-17 23:03:04 +00:00
|
|
|
"""Dump JSON."""
|
2024-12-10 22:42:14 +00:00
|
|
|
return orjson.dumps(
|
|
|
|
obj, option=orjson.OPT_INDENT_2 if indent else None
|
|
|
|
).decode()
|
2023-06-17 23:03:04 +00:00
|
|
|
|
|
|
|
loads = orjson.loads
|
|
|
|
except ImportError:
|
|
|
|
import json
|
|
|
|
|
2024-12-10 22:42:14 +00:00
|
|
|
def dumps(
|
|
|
|
obj: Any, *, default: Callable | None = None, indent: bool = False
|
|
|
|
) -> str:
|
2024-01-23 15:29:27 +00:00
|
|
|
"""Dump JSON."""
|
|
|
|
# Separators specified for consistency with orjson
|
2024-12-10 22:42:14 +00:00
|
|
|
return json.dumps(obj, separators=(",", ":"), indent=2 if indent else None)
|
2024-01-23 15:29:27 +00:00
|
|
|
|
2023-06-17 23:03:04 +00:00
|
|
|
loads = json.loads
|
2024-11-12 21:00:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
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]
|