mirror of
https://github.com/hanzo-ds/connect.git
synced 2026-10-30 20:31:24 +00:00
* migrate from pylint to ruff * ruff format/lint switch over * add formatting commit * update changelog
52 lines
1.1 KiB
Python
52 lines
1.1 KiB
Python
import json as py_json
|
|
import logging
|
|
from collections import OrderedDict
|
|
from typing import Any
|
|
|
|
try:
|
|
import orjson
|
|
|
|
any_to_json = orjson.dumps
|
|
except ImportError:
|
|
orjson = None
|
|
|
|
try:
|
|
import ujson
|
|
|
|
def _ujson_to_json(obj: Any) -> bytes:
|
|
return ujson.dumps(obj).encode()
|
|
except ImportError:
|
|
ujson = None
|
|
_ujson_to_json = None
|
|
|
|
|
|
def _pyjson_to_json(obj: Any) -> bytes:
|
|
return py_json.dumps(obj, separators=(",", ":")).encode()
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
_to_json = OrderedDict()
|
|
_to_json["orjson"] = orjson.dumps if orjson else None
|
|
_to_json["ujson"] = _ujson_to_json if ujson else None
|
|
_to_json["python"] = _pyjson_to_json
|
|
|
|
any_to_json = _pyjson_to_json
|
|
|
|
|
|
def set_json_library(impl: str = None):
|
|
global any_to_json
|
|
if impl:
|
|
func = _to_json.get(impl)
|
|
if func:
|
|
any_to_json = func
|
|
return
|
|
raise NotImplementedError(f"JSON library {impl} is not supported")
|
|
for library, func in _to_json.items():
|
|
if func:
|
|
logger.debug("Using %s library for writing JSON byte strings", library)
|
|
any_to_json = func
|
|
break
|
|
|
|
|
|
set_json_library()
|