1"""JSON serialize to/from utf8 bytes
2
3.. versionchanged:: 22.2
4 Remove optional imports of different JSON implementations.
5 Now that we require recent Python, unconditionally use the standard library.
6 Custom JSON libraries can be used via custom serialization functions.
7"""
8
9# Copyright (C) PyZMQ Developers
10# Distributed under the terms of the Modified BSD License.
11from __future__ import annotations
12
13import json
14from typing import Any
15
16# backward-compatibility, unused
17jsonmod = json
18
19
20def dumps(o: Any, **kwargs) -> bytes:
21 """Serialize object to JSON bytes (utf-8).
22
23 Keyword arguments are passed along to :py:func:`json.dumps`.
24 """
25 return json.dumps(o, **kwargs).encode("utf8")
26
27
28def loads(s: bytes | str, **kwargs) -> dict | list | str | int | float:
29 """Load object from JSON bytes (utf-8).
30
31 Keyword arguments are passed along to :py:func:`json.loads`.
32 """
33 if isinstance(s, bytes):
34 s = s.decode("utf8")
35 return json.loads(s, **kwargs)
36
37
38__all__ = ['dumps', 'loads']