diff --git a/README.md b/README.md index 5f94596..0175e4b 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ import json test_instance = Test('foo') blob = Marshal.marshal(test_instance) -print(blob) +print(blob.decode()) >>> '{name: foo}' marshal = Marshal() @@ -101,7 +101,7 @@ class TestContainer: marshal = Marshal() container_instance = TestContainer({'foo', 'bar'}) blob = marshal.marshal(container_instance) -print(blob) +print(blob.decode()) >>> '{container: ["foo", "bar"]}' result = marshal.unmarshal(TestContainer,json.loads(blob)) @@ -190,5 +190,5 @@ print(result.message_obj) >>> 'Hello from the custom delegate!' ``` -The result from any delegate should be initialized resulting class instance +The result from any delegate should be the initialized resulting class instance diff --git a/pymarshaler/__init__.py b/pymarshaler/__init__.py index 3533191..8804a29 100644 --- a/pymarshaler/__init__.py +++ b/pymarshaler/__init__.py @@ -1,4 +1,4 @@ -__version__ = '0.3.0' +__version__ = '0.3.1' __all__ = ['Marshal', 'utils', 'arg_delegates', 'errors'] from pymarshaler.marshal import Marshal diff --git a/pymarshaler/arg_delegates.py b/pymarshaler/arg_delegates.py index 4436aed..910ae66 100644 --- a/pymarshaler/arg_delegates.py +++ b/pymarshaler/arg_delegates.py @@ -28,9 +28,9 @@ def resolve(self, data): class EnumArgBuilderDelegate(ArgBuilderDelegate): - def resolve(self, data: str): - for k, v in self.cls.__members__.items(): - if k == data.upper(): + def resolve(self, data): + for v in self.cls.__members__.values(): + if v.value == data: return v raise UnknownFieldError(f'Invalid value {data} for enum {self.cls.__name__}') diff --git a/pymarshaler/marshal.py b/pymarshaler/marshal.py index ff65838..fff6c0b 100644 --- a/pymarshaler/marshal.py +++ b/pymarshaler/marshal.py @@ -1,11 +1,9 @@ import datetime import inspect -import json import typing from enum import Enum -from json import JSONEncoder -import jsonpickle +import orjson from pymarshaler.arg_delegates import ArgBuilderDelegate, ListArgBuilderDelegate, \ SetArgBuilderDelegate, TupleArgBuilderDelegate, DictArgBuilderDelegate, BuiltinArgBuilderDelegate, \ @@ -81,16 +79,11 @@ def _safe_get(self, name): return self._default_arg_builder_delegates[name] -class _DictEncoder(JSONEncoder): - def default(self, o): - if isinstance(o, datetime.datetime): - return o.isoformat() - elif isinstance(o, Enum): - return o.name - try: - return o.__dict__ - except AttributeError: - return repr(o) +def _default(o): + try: + return o.__dict__ + except AttributeError: + return repr(o) class Marshal: @@ -106,11 +99,10 @@ def __init__(self, ignore_unknown_fields: bool = False, walk_unknown_fields: boo ) @staticmethod - def marshal(obj, indent=2) -> str: + def marshal(obj) -> bytes: """ Convert a class instance to a JSON formatted string :param obj: The object to convert - :param indent: How to format the JSON. Defaults to an indent of 2 :return: String JSON representation of the class instance Example: >>> class Test: @@ -121,7 +113,7 @@ def marshal(obj, indent=2) -> str: >>> print(data) '{name: foo}' """ - return json.dumps(obj, cls=_DictEncoder, indent=indent) + return orjson.dumps(obj, default=_default) def unmarshal_str(self, cls, data: str): """ @@ -143,7 +135,7 @@ def unmarshal_str(self, cls, data: str): >>> print(test_instance.name) 'foo' """ - return self.unmarshal(cls, json.loads(data)) + return self.unmarshal(cls, orjson.loads(data)) def unmarshal(self, cls, data: dict): """ diff --git a/requirements.txt b/requirements.txt index 2f522cc..f826a90 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ -jsonpickle -python-dateutil \ No newline at end of file +python-dateutil +pymarshaler +setuptools +orjson \ No newline at end of file diff --git a/setup.py b/setup.py index 0ddba32..eb730ba 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( name="pymarshaler", - version="0.3.0", + version='0.3.1', author="Hernan Romer", author_email="nanug33@gmail.com", description="Package to marshal and unmarshal python objects",