Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/zmq/utils/jsonapi.py: 67%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

12 statements  

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']