File: json.py

package info (click to toggle)
python-aioshelly 11.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: python: 2,583; makefile: 8; sh: 3
file content (24 lines) | stat: -rw-r--r-- 483 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
"""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")