Skip to content
Snippets Groups Projects
Verified Commit 6a39b734 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

WIP: Plain json serialization for high level api.

parent 25a6b054
No related branches found
No related tags found
2 merge requests!189ENH: add convenience functions,!185High level API serialization
Pipeline #50554 passed with warnings
...@@ -471,7 +471,6 @@ class CaosDBPythonEntity(object): ...@@ -471,7 +471,6 @@ class CaosDBPythonEntity(object):
if isinstance(att, list): if isinstance(att, list):
return att return att
else:
return [att] return [att]
def add_parent(self, parent: Union[ def add_parent(self, parent: Union[
...@@ -679,53 +678,68 @@ class CaosDBPythonEntity(object): ...@@ -679,53 +678,68 @@ class CaosDBPythonEntity(object):
return entity return entity
def serialize(self, without_metadata: bool = False, visited: dict = None): def serialize(self, without_metadata: bool = None, plain_json: bool = False,
""" visited: dict = None):
Serialize necessary information into a dict. """Serialize necessary information into a dict.
without_metadata: bool Parameters
----------
without_metadata: bool, optional
If True don't set the metadata field in order to increase If True don't set the metadata field in order to increase
readability. Not recommended if deserialization is needed. 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: if visited is None:
visited = dict() visited = {}
if self in visited: if self in visited:
return visited[self] return visited[self]
metadata: Dict[str, Any] = dict() metadata: Dict[str, Any] = {}
properties = dict() properties = {}
parents = list() parents = []
# The full information to be returned: # The full information to be returned:
fulldict = dict() fulldict = {}
visited[self] = fulldict visited[self] = fulldict
# Add CaosDB role:
fulldict["role"] = standard_type_for_high_level_type(self, True)
for parent in self._parents: for parent in self._parents:
if isinstance(parent, CaosDBPythonEntity): 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): elif isinstance(parent, CaosDBPythonUnresolvedParent):
parents.append({"name": parent.name, "id": parent.id, parents.append({"name": parent.name, "id": parent.id,
"unresolved": True}) "unresolved": True})
else: else:
raise RuntimeError("Incompatible class used as parent.") raise RuntimeError("Incompatible class used as parent.")
if not plain_json:
# Add LinkAhead role:
fulldict["role"] = standard_type_for_high_level_type(self, True)
for baseprop in ("name", "id", "description", "version"): for baseprop in ("name", "id", "description", "version"):
val = self.__getattribute__(baseprop) val = self.__getattribute__(baseprop)
if val is not None: if val is not None:
fulldict[baseprop] = val fulldict[baseprop] = val
if type(self) == CaosDBPythonFile: if isinstance(self, CaosDBPythonFile):
fulldict["file"] = self.file fulldict["file"] = self.file
fulldict["path"] = self.path fulldict["path"] = self.path
for p in self.get_properties(): for p in self.get_properties():
m = self.get_property_metadata(p) m = self.get_property_metadata(p)
metadata[p] = dict() metadata[p] = {}
for f in fields(m): for f in fields(m):
val = m.__getattribute__(f.name) val = m.__getattribute__(f.name)
if val is not None: if val is not None:
...@@ -735,28 +749,35 @@ class CaosDBPythonEntity(object): ...@@ -735,28 +749,35 @@ class CaosDBPythonEntity(object):
if isinstance(val, CaosDBPythonUnresolvedReference): if isinstance(val, CaosDBPythonUnresolvedReference):
properties[p] = {"id": val.id, "unresolved": True} properties[p] = {"id": val.id, "unresolved": True}
elif isinstance(val, CaosDBPythonEntity): 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): elif isinstance(val, list):
serializedelements = [] serializedelements = []
for element in val: for element in val:
if isinstance(element, CaosDBPythonUnresolvedReference): if isinstance(element, CaosDBPythonUnresolvedReference):
elm = dict() elm = {}
elm["id"] = element.id elm["id"] = element.id
elm["unresolved"] = True elm["unresolved"] = True
serializedelements.append(elm) serializedelements.append(elm)
elif isinstance(element, CaosDBPythonEntity): elif isinstance(element, CaosDBPythonEntity):
serializedelements.append( serializedelements.append(
element.serialize(without_metadata, element.serialize(without_metadata=without_metadata,
visited)) plain_json=plain_json,
visited=visited))
else: else:
serializedelements.append(element) serializedelements.append(element)
properties[p] = serializedelements properties[p] = serializedelements
else: else:
properties[p] = val properties[p] = val
if plain_json:
fulldict["id"] = getattr(self, "id")
fulldict["name"] = getattr(self, "name")
fulldict.update(properties)
else:
fulldict["properties"] = properties fulldict["properties"] = properties
fulldict["parents"] = parents fulldict["parents"] = parents
if not without_metadata: if not without_metadata:
fulldict["metadata"] = metadata fulldict["metadata"] = metadata
return fulldict return fulldict
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment