10
10
11
11
12
12
class JsonUtil :
13
- """A utility class for handling JSON-related operations.
14
- This class provides static methods for formatting JSON strings, and
15
- for converting between Python objects and JSON strings/files. It uses
16
- the `orjson` library where possible for its speed advantages, but reverts
17
- to the standard `json` library where `orjson` does not support the required
18
- functionality.
13
+ """A utility class for handling JSON serialization operations.
14
+ This class provides static methods for converting between Python objects and JSON
15
+ strings/bytes. It uses the `orjson` library when available for its performance
16
+ advantages, falling back to the standard `json` library when `orjson` is not
17
+ installed.
18
+
19
+ The class handles compatibility between the two libraries, particularly for options
20
+ like 'sort_keys' which have different implementations in each library. All methods
21
+ maintain consistent behavior regardless of which JSON library is being used.
22
+
23
+ Methods:
24
+ dumps(obj, *args, **kwargs) -> str: Converts object to JSON string
25
+ dump_bytes(obj, *args, **kwargs) -> bytes: Converts object to JSON bytes
26
+ loads(s, *args, **kwargs) -> Any: Parses JSON string into Python object
19
27
"""
20
28
21
29
@staticmethod
@@ -24,7 +32,7 @@ def _handle_sort_keys(kwargs):
24
32
Args:
25
33
kwargs: The keyword arguments dictionary to modify
26
34
Returns:
27
- Modified kwargs dictionary
35
+ Modified kwargs dictionary with orjson-compatible options
28
36
"""
29
37
if kwargs .pop ("sort_keys" , False ):
30
38
option = kwargs .get ("option" , 0 ) | orjson .OPT_SORT_KEYS
@@ -33,15 +41,15 @@ def _handle_sort_keys(kwargs):
33
41
34
42
@staticmethod
35
43
def dumps (obj , * args , ** kwargs ) -> str :
36
- """Convert a Python object into a json string.
44
+ """Convert a Python object into a JSON string.
37
45
Args:
38
46
obj: The data to be converted
39
47
*args: Extra arguments to pass to the dumps() function
40
48
**kwargs: Extra keyword arguments to pass to the dumps() function.
41
49
Special handling for 'sort_keys' which is translated to
42
50
orjson.OPT_SORT_KEYS when using orjson.
43
51
Returns:
44
- The json string representation of obj
52
+ str: A JSON string representation of obj
45
53
"""
46
54
if orjson is None :
47
55
return json .dumps (obj , * args , ** kwargs )
@@ -51,15 +59,15 @@ def dumps(obj, *args, **kwargs) -> str:
51
59
52
60
@staticmethod
53
61
def dump_bytes (obj , * args , ** kwargs ) -> bytes :
54
- """Convert a Python object into a bytes string.
62
+ """Convert a Python object into a JSON bytes string.
55
63
Args:
56
64
obj: The data to be converted
57
65
*args: Extra arguments to pass to the dumps() function
58
66
**kwargs: Extra keyword arguments to pass to the dumps() function.
59
67
Special handling for 'sort_keys' which is translated to
60
68
orjson.OPT_SORT_KEYS when using orjson.
61
69
Returns:
62
- The json string representation of obj as bytes
70
+ bytes: A JSON bytes string representation of obj
63
71
"""
64
72
if orjson is None :
65
73
return json .dumps (obj , * args , ** kwargs ).encode ("utf-8" )
0 commit comments