File: json.py

package info (click to toggle)
python-aioshelly 13.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 632 kB
  • sloc: python: 5,395; makefile: 7; sh: 3
file content (33 lines) | stat: -rw-r--r-- 671 bytes parent folder | download
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
"""JSON helper."""

from typing import Any

import orjson

JSONDecodeError = orjson.JSONDecodeError
json_loads = orjson.loads


def json_encoder_default(obj: Any) -> Any:
    """Convert objects."""
    if isinstance(obj, set | tuple):
        return list(obj)
    raise TypeError


def json_dumps(data: Any) -> str:
    """Dump json string."""
    return orjson.dumps(
        data,
        option=orjson.OPT_NON_STR_KEYS,
        default=json_encoder_default,
    ).decode("utf-8")


def json_bytes(data: Any) -> bytes:
    """Dump json bytes."""
    return orjson.dumps(
        data,
        option=orjson.OPT_NON_STR_KEYS,
        default=json_encoder_default,
    )