aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/clickhouse-connect/clickhouse_connect/json_impl.py
blob: 686ddf395559602736642752ffa74ccf8621daa3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()