Skip to content

Commit 4fe1253

Browse files
committed
🎨 Update docstrings
Signed-off-by: ff137 <[email protected]>
1 parent 6a9c333 commit 4fe1253

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

nats/json_util.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@
1010

1111

1212
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
1927
"""
2028

2129
@staticmethod
@@ -24,7 +32,7 @@ def _handle_sort_keys(kwargs):
2432
Args:
2533
kwargs: The keyword arguments dictionary to modify
2634
Returns:
27-
Modified kwargs dictionary
35+
Modified kwargs dictionary with orjson-compatible options
2836
"""
2937
if kwargs.pop("sort_keys", False):
3038
option = kwargs.get("option", 0) | orjson.OPT_SORT_KEYS
@@ -33,15 +41,15 @@ def _handle_sort_keys(kwargs):
3341

3442
@staticmethod
3543
def dumps(obj, *args, **kwargs) -> str:
36-
"""Convert a Python object into a json string.
44+
"""Convert a Python object into a JSON string.
3745
Args:
3846
obj: The data to be converted
3947
*args: Extra arguments to pass to the dumps() function
4048
**kwargs: Extra keyword arguments to pass to the dumps() function.
4149
Special handling for 'sort_keys' which is translated to
4250
orjson.OPT_SORT_KEYS when using orjson.
4351
Returns:
44-
The json string representation of obj
52+
str: A JSON string representation of obj
4553
"""
4654
if orjson is None:
4755
return json.dumps(obj, *args, **kwargs)
@@ -51,15 +59,15 @@ def dumps(obj, *args, **kwargs) -> str:
5159

5260
@staticmethod
5361
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.
5563
Args:
5664
obj: The data to be converted
5765
*args: Extra arguments to pass to the dumps() function
5866
**kwargs: Extra keyword arguments to pass to the dumps() function.
5967
Special handling for 'sort_keys' which is translated to
6068
orjson.OPT_SORT_KEYS when using orjson.
6169
Returns:
62-
The json string representation of obj as bytes
70+
bytes: A JSON bytes string representation of obj
6371
"""
6472
if orjson is None:
6573
return json.dumps(obj, *args, **kwargs).encode("utf-8")

0 commit comments

Comments
 (0)