aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/clickhouse-connect/clickhouse_connect/json_impl.py
diff options
context:
space:
mode:
authorvitalyisaev <vitalyisaev@ydb.tech>2023-11-14 09:58:56 +0300
committervitalyisaev <vitalyisaev@ydb.tech>2023-11-14 10:20:20 +0300
commitc2b2dfd9827a400a8495e172a56343462e3ceb82 (patch)
treecd4e4f597d01bede4c82dffeb2d780d0a9046bd0 /contrib/python/clickhouse-connect/clickhouse_connect/json_impl.py
parentd4ae8f119e67808cb0cf776ba6e0cf95296f2df7 (diff)
downloadydb-c2b2dfd9827a400a8495e172a56343462e3ceb82.tar.gz
YQ Connector: move tests from yql to ydb (OSS)
Перенос папки с тестами на Коннектор из папки yql в папку ydb (синхронизируется с github).
Diffstat (limited to 'contrib/python/clickhouse-connect/clickhouse_connect/json_impl.py')
-rw-r--r--contrib/python/clickhouse-connect/clickhouse_connect/json_impl.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/contrib/python/clickhouse-connect/clickhouse_connect/json_impl.py b/contrib/python/clickhouse-connect/clickhouse_connect/json_impl.py
new file mode 100644
index 00000000000..686ddf39555
--- /dev/null
+++ b/contrib/python/clickhouse-connect/clickhouse_connect/json_impl.py
@@ -0,0 +1,50 @@
+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()