Skip to content

Commit

Permalink
allow both cls and converter args (#30)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
PythonFZ and pre-commit-ci[bot] authored Oct 11, 2024
1 parent ac8d908 commit 9ee98e3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "znjson"
version = "0.2.5"
version = "0.2.6"
description = "A Python Package to Encode/Decode some common file formats to json"
authors = ["zincwarecode <[email protected]>"]
license = "Apache-2.0"
Expand Down
21 changes: 21 additions & 0 deletions tests/converter/test_numpy_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ def test_encode(numpy_array):
def test_encode_direct(numpy_array):
encoded_str = znjson.dumps(numpy_array, converter=znjson.converter.NumpyConverter)
assert encoded_str.startswith('{"_type": "np.ndarray_b64"')
encoded_str = znjson.dumps(
numpy_array,
cls=znjson.ZnEncoder.from_converters([znjson.converter.NumpyConverter]),
)
assert encoded_str.startswith('{"_type": "np.ndarray_b64"')

with pytest.raises(TypeError):
_ = znjson.dumps(
numpy_array, converter=znjson.converter.NumpyConverter, cls=znjson.ZnEncoder
)


def test_decode(numpy_array):
Expand All @@ -35,6 +45,17 @@ def test_decode_direct(numpy_array):
numpy_array,
znjson.loads(encoded_str, converter=znjson.converter.NumpyConverter),
)
np.testing.assert_array_equal(
numpy_array,
znjson.loads(
encoded_str,
cls=znjson.ZnDecoder.from_converters([znjson.converter.NumpyConverter]),
),
)
with pytest.raises(TypeError):
_ = znjson.loads(
encoded_str, converter=znjson.converter.NumpyConverter, cls=znjson.ZnDecoder
)


def test_decode_missing_converter(numpy_array):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_znjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert znjson.__version__ == "0.2.5"
assert znjson.__version__ == "0.2.6"
20 changes: 14 additions & 6 deletions znjson/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@

import functools
import json
from typing import Any, List, Union
from typing import Any, List, Type, Union

from znjson.base import ConverterBase
from znjson.config import config

CONVERTER_TYPE = Union[ConverterBase, List[ConverterBase], None]
CONVERTER_TYPE = Union[Type[ConverterBase], List[Type[ConverterBase]], None]


@functools.wraps(json.loads)
def loads(data: str, converter: CONVERTER_TYPE = None, **kwargs):
def loads(data: str, converter: CONVERTER_TYPE = None, cls=None, **kwargs):
"""Load a string with ZnJSON decoding"""
if converter is not None and cls is not None:
raise TypeError("Cannot specify both `converter` and `cls`")
if converter is None:
converter = config.ACTIVE_CONVERTER
return json.loads(data, cls=ZnDecoder.from_converters(converter), **kwargs)
if cls is None:
cls = ZnDecoder.from_converters(converter)
return json.loads(data, cls=cls, **kwargs)


@functools.wraps(json.dumps)
def dumps(data: Any, converter: CONVERTER_TYPE = None, **kwargs) -> str:
def dumps(data: Any, converter: CONVERTER_TYPE = None, cls=None, **kwargs) -> str:
"""Dump data with ZnJSON encoding"""
if converter is not None and cls is not None:
raise TypeError("Cannot specify both `converter` and `cls`")
if converter is None:
converter = config.ACTIVE_CONVERTER
return json.dumps(data, cls=ZnEncoder.from_converters(converter), **kwargs)
if cls is None:
cls = ZnEncoder.from_converters(converter)
return json.dumps(data, cls=cls, **kwargs)


class SelectConverters:
Expand Down

0 comments on commit 9ee98e3

Please sign in to comment.