|
2 | 2 |
|
3 | 3 | import functools |
4 | 4 | import json |
5 | | -from typing import Any, List, Union |
| 5 | +from typing import Any, List, Type, Union |
6 | 6 |
|
7 | 7 | from znjson.base import ConverterBase |
8 | 8 | from znjson.config import config |
9 | 9 |
|
10 | | -CONVERTER_TYPE = Union[ConverterBase, List[ConverterBase], None] |
| 10 | +CONVERTER_TYPE = Union[Type[ConverterBase], List[Type[ConverterBase]], None] |
11 | 11 |
|
12 | 12 |
|
13 | 13 | @functools.wraps(json.loads) |
14 | | -def loads(data: str, converter: CONVERTER_TYPE = None, **kwargs): |
| 14 | +def loads(data: str, converter: CONVERTER_TYPE = None, cls=None, **kwargs): |
15 | 15 | """Load a string with ZnJSON decoding""" |
| 16 | + if converter is not None and cls is not None: |
| 17 | + raise TypeError("Cannot specify both `converter` and `cls`") |
16 | 18 | if converter is None: |
17 | 19 | converter = config.ACTIVE_CONVERTER |
18 | | - return json.loads(data, cls=ZnDecoder.from_converters(converter), **kwargs) |
| 20 | + if cls is None: |
| 21 | + cls = ZnDecoder.from_converters(converter) |
| 22 | + return json.loads(data, cls=cls, **kwargs) |
19 | 23 |
|
20 | 24 |
|
21 | 25 | @functools.wraps(json.dumps) |
22 | | -def dumps(data: Any, converter: CONVERTER_TYPE = None, **kwargs) -> str: |
| 26 | +def dumps(data: Any, converter: CONVERTER_TYPE = None, cls=None, **kwargs) -> str: |
23 | 27 | """Dump data with ZnJSON encoding""" |
| 28 | + if converter is not None and cls is not None: |
| 29 | + raise TypeError("Cannot specify both `converter` and `cls`") |
24 | 30 | if converter is None: |
25 | 31 | converter = config.ACTIVE_CONVERTER |
26 | | - return json.dumps(data, cls=ZnEncoder.from_converters(converter), **kwargs) |
| 32 | + if cls is None: |
| 33 | + cls = ZnEncoder.from_converters(converter) |
| 34 | + return json.dumps(data, cls=cls, **kwargs) |
27 | 35 |
|
28 | 36 |
|
29 | 37 | class SelectConverters: |
|
0 commit comments