Skip to content

Commit 9ee98e3

Browse files
allow both cls and converter args (#30)
* allow both cls and converter args * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * format * bump version test --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ac8d908 commit 9ee98e3

4 files changed

Lines changed: 37 additions & 8 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "znjson"
3-
version = "0.2.5"
3+
version = "0.2.6"
44
description = "A Python Package to Encode/Decode some common file formats to json"
55
authors = ["zincwarecode <zincwarecode@gmail.com>"]
66
license = "Apache-2.0"

tests/converter/test_numpy_converter.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ def test_encode(numpy_array):
2020
def test_encode_direct(numpy_array):
2121
encoded_str = znjson.dumps(numpy_array, converter=znjson.converter.NumpyConverter)
2222
assert encoded_str.startswith('{"_type": "np.ndarray_b64"')
23+
encoded_str = znjson.dumps(
24+
numpy_array,
25+
cls=znjson.ZnEncoder.from_converters([znjson.converter.NumpyConverter]),
26+
)
27+
assert encoded_str.startswith('{"_type": "np.ndarray_b64"')
28+
29+
with pytest.raises(TypeError):
30+
_ = znjson.dumps(
31+
numpy_array, converter=znjson.converter.NumpyConverter, cls=znjson.ZnEncoder
32+
)
2333

2434

2535
def test_decode(numpy_array):
@@ -35,6 +45,17 @@ def test_decode_direct(numpy_array):
3545
numpy_array,
3646
znjson.loads(encoded_str, converter=znjson.converter.NumpyConverter),
3747
)
48+
np.testing.assert_array_equal(
49+
numpy_array,
50+
znjson.loads(
51+
encoded_str,
52+
cls=znjson.ZnDecoder.from_converters([znjson.converter.NumpyConverter]),
53+
),
54+
)
55+
with pytest.raises(TypeError):
56+
_ = znjson.loads(
57+
encoded_str, converter=znjson.converter.NumpyConverter, cls=znjson.ZnDecoder
58+
)
3859

3960

4061
def test_decode_missing_converter(numpy_array):

tests/test_znjson.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33

44
def test_version():
5-
assert znjson.__version__ == "0.2.5"
5+
assert znjson.__version__ == "0.2.6"

znjson/main.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@
22

33
import functools
44
import json
5-
from typing import Any, List, Union
5+
from typing import Any, List, Type, Union
66

77
from znjson.base import ConverterBase
88
from znjson.config import config
99

10-
CONVERTER_TYPE = Union[ConverterBase, List[ConverterBase], None]
10+
CONVERTER_TYPE = Union[Type[ConverterBase], List[Type[ConverterBase]], None]
1111

1212

1313
@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):
1515
"""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`")
1618
if converter is None:
1719
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)
1923

2024

2125
@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:
2327
"""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`")
2430
if converter is None:
2531
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)
2735

2836

2937
class SelectConverters:

0 commit comments

Comments
 (0)