fix(serialisation): some quick fixes

This commit is contained in:
izzy lyseggen
2020-12-24 11:38:44 +00:00
parent 11bc10d072
commit 028ca641ef
2 changed files with 22 additions and 8 deletions
+14 -2
View File
@@ -66,7 +66,7 @@ def receive(
# try local transport first. if the parent is there, we assume all the children are there and continue wth deserialisation using the local transport
obj_string = local_transport.get_object(obj_id)
if obj_string:
base = serializer.read_json(id=obj_id, obj_string=obj_string)
base = serializer.read_json(obj_string=obj_string)
return base
if not remote_transport:
@@ -78,4 +78,16 @@ def receive(
id=obj_id, target_transport=local_transport
)
return serializer.read_json(id=obj_id, obj_string=obj_string)
return serializer.read_json(obj_string=obj_string)
def serialize(base: Base) -> str:
serializer = BaseObjectSerializer()
return serializer.write_json(base)[1]
def deserialize(obj_string: str) -> Base:
serializer = BaseObjectSerializer()
return serializer.read_json(obj_string=obj_string)
@@ -59,6 +59,10 @@ class BaseObjectSerializer:
if not value or prop.startswith(("__", "_")):
continue
# don't prepopulate id as this will mess up hashing
if prop == "id":
continue
chunkable = True if prop in base._chunkable else False
detach = True if prop.startswith("@") or chunkable else False
@@ -70,15 +74,14 @@ class BaseObjectSerializer:
# 2. handle Base objects
elif isinstance(value, Base):
child_obj = self.traverse_value(value, detach=detach)
if detach:
if detach and self.write_transports:
ref_hash = child_obj["id"]
object_builder[prop] = self.detach_helper(ref_hash=ref_hash)
else:
object_builder[prop] = child_obj
# 3. handle chunkable props
elif chunkable:
self.detach_lineage.append(detach)
elif chunkable and self.write_transports:
chunks = []
max_size = base._chunkable[prop]
chunk = DataChunk()
@@ -115,7 +118,7 @@ class BaseObjectSerializer:
}
# write detached or root objects to transports
if detached:
if detached and self.write_transports:
for t in self.write_transports:
t.save_object(id=hash, serialized_object=json.dumps(object_builder))
@@ -191,11 +194,10 @@ class BaseObjectSerializer:
self.family_tree = {}
self.closure_table = {}
def read_json(self, id: str, obj_string: str) -> Base:
def read_json(self, obj_string: str) -> Base:
"""Recomposes a Base object from the string representation of the object
Arguments:
id {str} -- the hash of the object
obj_string {str} -- the string representation of the object
Returns: