1414 FunctionalHeaderEntry ,
1515 HeaderEntry ,
1616 InlineDependency ,
17+ ObjectDeserializationDeclaration ,
1718 ObjectSerializationDeclaration ,
1819 Struct ,
1920 VirtualDestructor ,
@@ -136,6 +137,17 @@ def __post_init__(self):
136137 self .trace ()
137138
138139
140+ @dataclass
141+ class StructDeserialization (FunctionDefinition ):
142+ _header_entry : Struct
143+
144+ def __post_init__ (self ):
145+ super (FunctionDefinition , self ).__post_init__ ()
146+ self ._func = f"void to_json(nlohmann::json& j, const { self ._name } & x)"
147+
148+ self .trace ()
149+
150+
139151@dataclass
140152class MemberFunctionDefinition (FunctionDefinition ):
141153 _header_entry : FunctionalHeaderEntry
@@ -193,6 +205,30 @@ def __post_init__(self):
193205 self .trace ()
194206
195207
208+ @dataclass
209+ class OwnedElementDeserialization (ElementSerialization ):
210+ def __post_init__ (self ):
211+ super ().__post_init__ ()
212+ self ._funclines = [f'json_set<{ self ._type } >(j, "{ self ._name } ", x.{ self ._name } , x.{ self ._name } _is_set);' ]
213+ self .trace ()
214+
215+
216+ @dataclass
217+ class OwnedElementDownload (ElementSerialization ):
218+ def __post_init__ (self ):
219+ super ().__post_init__ ()
220+ self ._funclines = []
221+ assert len (self ._header_entry .selector ) == 1 # only one switchable data element per entry
222+ data_element = next (iter (self ._header_entry .selector ))
223+ for enum in self ._header_entry .selector [data_element ]:
224+ self ._funclines += [
225+ f"if (x.{ data_element } == { enum } ) {{" ,
226+ f'\t json_set<{ self ._header_entry .selector [data_element ][enum ]} >(j, "{ self ._name } ", *dynamic_cast<const { self ._header_entry .selector [data_element ][enum ]} *>(x.{ self ._name } .get()), x.{ self ._name } _is_set);' , # noqa: E501
227+ "}" ,
228+ ]
229+ self .trace ()
230+
231+
196232@dataclass
197233class ClassFactoryCreation (ElementSerialization ):
198234 def __post_init__ (self ):
@@ -268,14 +304,14 @@ def _translate(self, container_class_name, header_tree):
268304
269305 self ._get_items_to_serialize (header_tree .root )
270306
271- def _get_items_to_serialize (self , header_tree : HeaderEntry ):
307+ def _get_items_to_serialize (self , header_tree : HeaderEntry ): # noqa: PLR0912 too-many-branches
272308 for h_entry in header_tree .child_entries :
273309 cpp_entry : Optional [ImplementationEntry ] = None
274310 logger .debug (f"Header entry being processed: { h_entry .name } under { h_entry .parent .name } " )
275311 if isinstance (h_entry , Struct ) and len ([c for c in h_entry .child_entries if isinstance (c , DataElement )]):
276312 cpp_entry = StructSerialization (
277313 h_entry , self ._namespace
278- ) # Create the "from_json" function definition (header) , only if it won't be empty
314+ ) # Create the "from_json/"to_json"" function definition, only if they won't be empty
279315
280316 for data_element_entry in [c for c in h_entry .child_entries if isinstance (c , DataElement )]:
281317 # In function body, create each "get_to" for individual data elements
@@ -284,6 +320,14 @@ def _get_items_to_serialize(self, header_tree: HeaderEntry):
284320 else :
285321 c = OwnedElementSerialization (data_element_entry , cpp_entry )
286322
323+ cpp_entry = StructDeserialization (h_entry , self ._namespace )
324+
325+ for data_element_entry in [c for c in h_entry .child_entries if isinstance (c , DataElement )]:
326+ if "unique_ptr" in data_element_entry .type :
327+ c = OwnedElementDownload (data_element_entry , cpp_entry )
328+ else :
329+ c = OwnedElementDeserialization (data_element_entry , cpp_entry )
330+
287331 elif isinstance (h_entry , DataElementStaticMetainfo ):
288332 logger .debug (f"{ h_entry .name } under { type (h_entry .parent )} under { self ._namespace ._name } " )
289333 cpp_entry = DataElementStaticInitialization (h_entry , self ._namespace )
@@ -295,6 +339,7 @@ def _get_items_to_serialize(self, header_tree: HeaderEntry):
295339 elif (
296340 isinstance (h_entry , FunctionalHeaderEntry )
297341 and not isinstance (h_entry , ObjectSerializationDeclaration )
342+ and not isinstance (h_entry , ObjectDeserializationDeclaration )
298343 and not isinstance (h_entry , VirtualDestructor )
299344 ):
300345 cpp_entry = MemberFunctionDefinition (
0 commit comments