Files

74 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from dataclasses import dataclass, asdict, fields
from dacite import from_dict
from typing import List
import json
@dataclass
class Punkt:
x: float
y: float
@dataclass
class Ziel:
#Sensor oder Aktor
id: str
pos: Punkt
@dataclass
class Quelle:
#Unterverteiler
id: str
pos: Punkt
@dataclass
class Pritsche:
id: str
cords: List[Punkt]
@dataclass
class Anlage:
name: str
sensoren: List[Ziel]
aktoren: List[Ziel]
unterverteiler: List[Quelle]
kabelpritschen: List[Pritsche]
def to_json(self, pretty: bool = True) -> str:
d = self.to_dict()
return json.dumps(d, indent=2 if pretty else None, default=str)
def to_dict(self) -> dict:
"""Konvertiert das Objekt in ein dict datetime wird als ISO-String dargestellt."""
result = asdict(self)
# hier um irgendwas ergänzen
# result["startdatum"] = self.startdatum.isoformat()
return result
if __name__ == '__main__':
json_string = '''{
"name": "H&M",
"sensoren": [
{"id": "S1", "pos": {"x": 3, "y": 5}}
],
"aktoren": [
{"id": "A1", "pos": {"x": 3, "y": 5}}
],
"unterverteiler": [
{"id": "U1", "pos": {"x": 3, "y": 5}}
],
"kabelpritschen": [
{"id": "p1", "cords": [{"x": 3, "y": 5},{"x": 3, "y": 5}]},
{"id": "p2", "cords": [{"x": 3, "y": 5},{"x": 3, "y": 5}]}
]
}
'''
data = json.loads(json_string)
anlage = from_dict(
data_class=Anlage,
data=data
)
print(anlage)