Skip to content

Commit 25db959

Browse files
modified codes for xml serialization from xml back to dict
1 parent 70bd234 commit 25db959

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/modelspec/base_types.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,14 @@ def from_bson(cls, bson_str: str) -> "Base":
167167
@classmethod
168168
def from_xml(cls, xml_str: str) -> "Base":
169169
"""Instantiate a Base object from an XML string"""
170-
from modelspec.utils import element_to_dict
170+
from modelspec.utils import element_to_dict, handle_id, convert_values
171171

172172
root = ET.fromstring(xml_str)
173-
data_dict = {root.tag: element_to_dict(root)}
174-
return cls.from_dict(data_dict)
173+
data_dict = element_to_dict(root)
174+
removed_id = handle_id(data_dict)
175+
converted_to_actual_val = convert_values(removed_id)
176+
177+
return cls.from_dict(converted_to_actual_val)
175178

176179
def to_json_file(
177180
self, filename: Optional[str] = None, include_metadata: bool = True
@@ -273,13 +276,8 @@ def to_xml_file(
273276
if filename is None:
274277
filename = f"{self.id}.xml"
275278

276-
# root = ET.Element(root_name)
277-
278279
root = build_xml_element(self)
279280

280-
# Create an ElementTree object with the root element
281-
# tree = ET.ElementTree(root)
282-
283281
# Generate the XML string
284282
xml_str = ET.tostring(root, encoding="utf-8", method="xml").decode("utf-8")
285283

@@ -377,14 +375,16 @@ def from_xml_file(cls, filename: str) -> "Base":
377375
Returns:
378376
A modelspec Base for this XML.
379377
"""
380-
from modelspec.utils import element_to_dict
378+
from modelspec.utils import element_to_dict, handle_id, convert_values
381379

382380
with open(filename) as infile:
383381
tree = ET.parse(filename)
384382
root = tree.getroot()
385383

386-
data_dict = {root.tag: element_to_dict(root)}
387-
return cls.from_dict(data_dict)
384+
data_dict = element_to_dict(root)
385+
removed_id = handle_id(data_dict)
386+
converted_to_actual_val = convert_values(removed_id)
387+
return cls.from_dict(converted_to_actual_val)
388388

389389
def get_child(self, id: str, type_: str) -> Any:
390390
"""

src/modelspec/utils.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ def load_xml(filename: str):
7272
root = tree.getroot() # Get the root element
7373

7474
# Convert the ElementTree object to a dictionary
75-
data = {root.tag: element_to_dict(root)}
75+
data = element_to_dict(root)
76+
removed_id = handle_id(data)
77+
converted_to_actual_val = convert_values(removed_id)
7678

77-
return convert_values(data)
79+
return convert_values(converted_to_actual_val)
7880

7981

8082
def element_to_dict(element):
@@ -106,6 +108,20 @@ def element_to_dict(element):
106108
return result
107109

108110

111+
def handle_id(dictionary):
112+
if isinstance(dictionary, dict):
113+
if "id" in dictionary:
114+
nested_dict = {dictionary["id"]: dictionary.copy()}
115+
del nested_dict[dictionary["id"]]["id"]
116+
return {k: handle_id(v) for k, v in nested_dict.items()}
117+
else:
118+
return {k: handle_id(v) for k, v in dictionary.items()}
119+
elif isinstance(dictionary, list):
120+
return [handle_id(item) for item in dictionary]
121+
else:
122+
return dictionary
123+
124+
109125
def convert_values(value):
110126
"""
111127
This recursively converts values to their actual types.

0 commit comments

Comments
 (0)