Skip to content

Commit

Permalink
Merge pull request #4 from hgromer/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hgromer authored May 21, 2021
2 parents 40844be + 84f3c5b commit f2d2e78
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import json

test_instance = Test('foo')
blob = Marshal.marshal(test_instance)
print(blob)
print(blob.decode())
>>> '{name: foo}'

marshal = Marshal()
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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

2 changes: 1 addition & 1 deletion pymarshaler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.3.0'
__version__ = '0.3.1'
__all__ = ['Marshal', 'utils', 'arg_delegates', 'errors']

from pymarshaler.marshal import Marshal
Expand Down
6 changes: 3 additions & 3 deletions pymarshaler/arg_delegates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}')

Expand Down
26 changes: 9 additions & 17 deletions pymarshaler/marshal.py
Original file line number Diff line number Diff line change
@@ -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, \
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand Down
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
jsonpickle
python-dateutil
python-dateutil
pymarshaler
setuptools
orjson
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="pymarshaler",
version="0.3.0",
version='0.3.1',
author="Hernan Romer",
author_email="[email protected]",
description="Package to marshal and unmarshal python objects",
Expand Down

0 comments on commit f2d2e78

Please sign in to comment.