From 028ca641ef5cde87668d495f1837361d8e16162f Mon Sep 17 00:00:00 2001 From: izzy lyseggen Date: Thu, 24 Dec 2020 11:38:44 +0000 Subject: [PATCH] fix(serialisation): some quick fixes --- speckle/api/operations.py | 16 ++++++++++++++-- speckle/serialization/base_object_serializer.py | 14 ++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/speckle/api/operations.py b/speckle/api/operations.py index 09d6085..0da29b9 100644 --- a/speckle/api/operations.py +++ b/speckle/api/operations.py @@ -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) diff --git a/speckle/serialization/base_object_serializer.py b/speckle/serialization/base_object_serializer.py index ae01fa7..c32fb76 100644 --- a/speckle/serialization/base_object_serializer.py +++ b/speckle/serialization/base_object_serializer.py @@ -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: