Skip to content
Snippets Groups Projects
Commit 17e82fc8 authored by Vanessa Stehr's avatar Vanessa Stehr
Browse files

Implement toJsonDict for list types (siList, siHybrid).

parent cf9d8c98
No related branches found
No related tags found
No related merge requests found
Pipeline #51997 passed
from __future__ import annotations # for type annotation recursion
from __future__ import annotations
from collections import defaultdict # for type annotation recursion
from AbstractQuantityTypeData import AbstractQuantityTypeData
from AbstractValueType import AbstractValueType
......@@ -10,6 +11,13 @@ class AbstractListType(AbstractQuantityTypeData):
super().__init__()
self.children = children
def toJsonDict(self):
result = defaultdict(list)
for child in self.children:
childJson = child.toJsonDict()
for key, value in childJson.items():
result[key].append(value)
return dict(result)
def flatten(self):
pass
......
......@@ -14,6 +14,9 @@ class SiHybrid(AbstractListType):
def getUnits(self):
pass
def toJsonDict(self):
return {'si:hybrid': super().toJsonDict()}
def __repr__(self) -> str:
params = {key: value for key, value in vars(self).items() if value is not None}
paramStr = ", ".join(f"{key}={repr(value)}" for key, value in params.items())
......
......@@ -16,6 +16,24 @@ class SiList(AbstractListType):
super().__init__(children)
self.label = label
self.dateTime = dateTime
def toJsonDict(self):
keyMapping = {
'label': 'si:label',
'dateTime': 'si:dateTime',
}
attributes = super().toJsonDict() # converts all the data in 'children'
for key, value in self.__dict__.items():
if value:
if key in keyMapping:
attributes[keyMapping[key]] = value
elif key == 'children':
pass
else:
attributes[key] = value
# TODO: This should not happen, maybe add a warning?
return {'si:list': attributes}
def __repr__(self) -> str:
params = {key: value for key, value in vars(self).items() if value is not None}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment