diff --git a/src/linkahead/high_level_api.py b/src/linkahead/high_level_api.py index 18d219c732672d16d0ab43e562cfe73d682614fe..5b1682f36d88970eb84fe8ebfc6525ae8ed97e7c 100644 --- a/src/linkahead/high_level_api.py +++ b/src/linkahead/high_level_api.py @@ -471,8 +471,7 @@ class CaosDBPythonEntity(object): if isinstance(att, list): return att - else: - return [att] + return [att] def add_parent(self, parent: Union[ CaosDBPythonUnresolvedParent, "CaosDBPythonRecordType", str]): @@ -679,53 +678,68 @@ class CaosDBPythonEntity(object): return entity - def serialize(self, without_metadata: bool = False, visited: dict = None): - """ - Serialize necessary information into a dict. - - without_metadata: bool - If True don't set the metadata field in order to increase - readability. Not recommended if deserialization is needed. + def serialize(self, without_metadata: bool = None, plain_json: bool = False, + visited: dict = None): + """Serialize necessary information into a dict. + +Parameters +---------- +without_metadata: bool, optional + If True don't set the metadata field in order to increase + readability. Not recommended if deserialization is needed. +plain_json: bool, optional + If True, serialize to a plain dict without any additional information besides the property values, + name and id. This should conform to the format as specified by the json schema generated by the + advanced user tools. This implies ``without_metadata = True``. """ + if plain_json: + if without_metadata is None: + without_metadata = True + if not without_metadata: + raise ValueError("`plain_json` implies `without_metadata`.") + if without_metadata is None: + without_metadata = False if visited is None: - visited = dict() + visited = {} if self in visited: return visited[self] - metadata: Dict[str, Any] = dict() - properties = dict() - parents = list() + metadata: Dict[str, Any] = {} + properties = {} + parents = [] # The full information to be returned: - fulldict = dict() + fulldict = {} visited[self] = fulldict - # Add CaosDB role: - fulldict["role"] = standard_type_for_high_level_type(self, True) - for parent in self._parents: if isinstance(parent, CaosDBPythonEntity): - parents.append(parent.serialize(without_metadata, visited)) + parents.append(parent.serialize(without_metadata=without_metadata, + plain_json=plain_json, + visited=visited)) elif isinstance(parent, CaosDBPythonUnresolvedParent): parents.append({"name": parent.name, "id": parent.id, "unresolved": True}) else: raise RuntimeError("Incompatible class used as parent.") - for baseprop in ("name", "id", "description", "version"): - val = self.__getattribute__(baseprop) - if val is not None: - fulldict[baseprop] = val + if not plain_json: + # Add LinkAhead role: + fulldict["role"] = standard_type_for_high_level_type(self, True) + for baseprop in ("name", "id", "description", "version"): + val = self.__getattribute__(baseprop) + if val is not None: + fulldict[baseprop] = val - if type(self) == CaosDBPythonFile: - fulldict["file"] = self.file - fulldict["path"] = self.path + if isinstance(self, CaosDBPythonFile): + fulldict["file"] = self.file + fulldict["path"] = self.path for p in self.get_properties(): m = self.get_property_metadata(p) - metadata[p] = dict() + metadata[p] = {} for f in fields(m): val = m.__getattribute__(f.name) if val is not None: @@ -735,30 +749,37 @@ class CaosDBPythonEntity(object): if isinstance(val, CaosDBPythonUnresolvedReference): properties[p] = {"id": val.id, "unresolved": True} elif isinstance(val, CaosDBPythonEntity): - properties[p] = val.serialize(without_metadata, visited) + properties[p] = val.serialize(without_metadata=without_metadata, + plain_json=plain_json, + visited=visited) elif isinstance(val, list): serializedelements = [] for element in val: if isinstance(element, CaosDBPythonUnresolvedReference): - elm = dict() + elm = {} elm["id"] = element.id elm["unresolved"] = True serializedelements.append(elm) elif isinstance(element, CaosDBPythonEntity): serializedelements.append( - element.serialize(without_metadata, - visited)) + element.serialize(without_metadata=without_metadata, + plain_json=plain_json, + visited=visited)) else: serializedelements.append(element) properties[p] = serializedelements else: properties[p] = val - fulldict["properties"] = properties - fulldict["parents"] = parents - - if not without_metadata: - fulldict["metadata"] = metadata + if plain_json: + fulldict["id"] = getattr(self, "id") + fulldict["name"] = getattr(self, "name") + fulldict.update(properties) + else: + fulldict["properties"] = properties + fulldict["parents"] = parents + if not without_metadata: + fulldict["metadata"] = metadata return fulldict def __str__(self):