I have a plugin with a class that contains as a field a list of objects of the class type itself (think of them as children) that I don’t know how to (de)serialize.
To clarify with an example, take the Boat.cs example from here:
Now, suppose the BoatShell class can describe also a boat carrier (a boat that carries other boats) and its fields are:
public class BoatShell
{
#region fields
private double m_width;
private double m_length;
private Point3d m_mast;
private Brep m_shape;
private Curve m_waterLine;
private List<BoatShell> m_children; // < list of "children" BoatShells
#endregion
...
Omitting the rest of the code for simplicity, but suppose there’s a related property, a constructor that accepts also a List<BoatShell> as a parameter, etc… How can one implement (de)serialization inside the Read and Write methods in BoatShellGoo for the m_children field?
Yikes, that sounds like a nightmare to get ones head around! Could you do something like this, by going through the list of children and accessing the members explicitly and writing them into flat lists, then do the reverse when reading them? It might be overly convoluted however especially if you are going to have many boats… maybe someone with better knowledge than me can help:
public class BoatShellGoo : GH_Goo<BoatShell>
{
public BoatShellGoo()
{
this.Value = new BoatShell();
}
// all the other goo stuff here
public override bool Write(GH_IO.Serialization.GH_IWriter writer)
{
if (Value != null)
{
for (int i=0; i<Value.m_children[i].Count; i++)
{
Value.m_children[i].m_mast.Write(writer.CreateChunk("mastData"+i));
Value.m_children[i].m_shape.Write(writer.CreateChunk("shapeData"+i));
etc...
}
}
return true;
}
Thank you John, I thought about going the thorough way myself, but the problem is that my “Boat” has a lot more fields in need of serialization than just a m_mast and m_shape… not an impossible problem, I guess it’s just a matter of elbow grease at this point. I really hoped for a more elegant solution, as in a way to recursively call read and write - which would have allowed the implementation of more nesting levels.
Anyways, you have already given me some ideas to try out, thank you very much!
maybe someone with better knowledge than me can help