mirror of
https://github.com/hanzo-ds/connect.git
synced 2026-10-30 20:31:24 +00:00
Imports read `datastore_connect`, distribution `datastore-connect`, so nothing has to import another product's name to talk to Hanzo Datastore. Cut from upstream 0.8.17 — the version this stack already ran — rather than from upstream HEAD, so it is a PURE RENAME with no behaviour change. Forking from HEAD would have dragged versions along with the name (connect alone jumped 0.8.17 -> 1.6.0, a major), which is a different and much riskier change wearing a rename's clothes. X-ClickHouse-* headers are untouched: they are wire protocol the server sends and expects, and a different token shape from the module name.
51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
import logging
|
|
import json as py_json
|
|
from collections import OrderedDict
|
|
from typing import Any
|
|
|
|
try:
|
|
import orjson
|
|
any_to_json = orjson.dumps # pylint: disable=no-member
|
|
except ImportError:
|
|
orjson = None
|
|
|
|
try:
|
|
import ujson
|
|
|
|
def _ujson_to_json(obj: Any) -> bytes:
|
|
return ujson.dumps(obj).encode() # pylint: disable=c-extension-no-member
|
|
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 # pylint: disable=no-member
|
|
_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 # pylint: disable=global-statement
|
|
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()
|