-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from pedohorse/attribute-serialization-error-c…
…atching attribute serialization error catching
- Loading branch information
Showing
27 changed files
with
422 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import asyncio | ||
import json | ||
|
||
from .common_serialization import AttribSerializer, AttribDeserializer | ||
|
||
|
||
async def serialize_attributes(attributes: dict) -> str: | ||
return await asyncio.get_event_loop().run_in_executor(None, serialize_attributes_core, attributes) | ||
|
||
|
||
async def deserialize_attributes(attributes_serialized: str) -> dict: | ||
return await asyncio.get_event_loop().run_in_executor(None, deserialize_attributes_core, attributes_serialized) | ||
|
||
|
||
def serialize_attributes_core(attributes: dict) -> str: | ||
return json.dumps(attributes, cls=AttribSerializer) | ||
|
||
|
||
def deserialize_attributes_core(attributes_serialized: str) -> dict: | ||
return json.loads(attributes_serialized, cls=AttribDeserializer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
|
||
|
||
class AttribSerializer(json.JSONEncoder): | ||
def _reform(self, obj): | ||
if type(obj) is set: | ||
return { | ||
'__special_object_type__': 'set', | ||
'items': self._reform(list(obj)) | ||
} | ||
elif type(obj) is tuple: | ||
return { | ||
'__special_object_type__': 'tuple', | ||
'items': self._reform(list(obj)) | ||
} | ||
elif type(obj) is dict: # int keys case | ||
if any(isinstance(x, (int, float, tuple)) for x in obj.keys()): | ||
return { | ||
'__special_object_type__': 'kvp', | ||
'items': self._reform([[k, v] for k, v in obj.items()]) | ||
} | ||
return {k: self._reform(v) for k, v in obj.items()} | ||
elif isinstance(obj, list): | ||
return [self._reform(x) for x in obj] | ||
elif isinstance(obj, (int, float, str, bool)) or obj is None: | ||
return obj | ||
raise NotImplementedError(f'serialization not implemented for type "{type(obj)}"') | ||
|
||
def encode(self, o): | ||
return super().encode(self._reform(o)) | ||
|
||
def default(self, obj): | ||
return super().default(obj) | ||
|
||
|
||
class AttribDeserializer(json.JSONDecoder): | ||
def _dedata(self, obj): | ||
special_type = obj.get('__special_object_type__') | ||
if special_type == 'set': | ||
return set(obj.get('items')) | ||
elif special_type == 'tuple': | ||
return tuple(obj.get('items')) | ||
elif special_type == 'kvp': | ||
return {k: v for k, v in obj.get('items')} | ||
return obj | ||
|
||
def __init__(self): | ||
super().__init__(object_hook=self._dedata) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.