2023-06-17 23:03:04 +00:00
|
|
|
"""JSON abstraction."""
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
2023-06-17 23:03:04 +00:00
|
|
|
try:
|
|
|
|
import orjson
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def dumps(obj: Any, *, default: Callable | None = None) -> str:
|
2023-06-17 23:03:04 +00:00
|
|
|
"""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:
|
2024-01-23 15:29:27 +00:00
|
|
|
"""Dump JSON."""
|
|
|
|
# Separators specified for consistency with orjson
|
|
|
|
return json.dumps(obj, separators=(",", ":"))
|
|
|
|
|
2023-06-17 23:03:04 +00:00
|
|
|
loads = json.loads
|