How to serialize a GH_Structure<IGH_Goo>?

I have not tested this at all…

protected Dictionary<string, GH_Structure<IGH_Goo>> _storage = new Dictionary<string, GH_Structure<IGH_Goo>>();
private void WriteTest(GH_IWriter writer)
{
  int n = 0;
  foreach (var pair in _storage)
  {
    // Create a chunk with a specific name and an increasing index.
    var chunk = writer.CreateChunk("StoredData", n++);
    // Store the dictionary name as a string on the chunk.
    chunk.SetString("Key", pair.Key);

    // Store the dictionary value in a sub-chunk.
    var treeChunk = chunk.CreateChunk("Tree");
    pair.Value.Write(treeChunk);
  }
}
private void ReadTest(GH_IReader reader)
{
  _storage.Clear();

  // Try and read as many "StoredData" chunks as possible.
  // This approach demands that all these chunks are stored under increasing indices.
  for (int i = 0; i < int.MaxValue; i++)
  {
    var chunk = reader.FindChunk("StoredData", i);
    // Stop looking once we've run out.
    if (chunk is null) break;

    var key = chunk.GetString("Key");
    var treeChunk = chunk.FindChunk("Tree");

    var tree = new GH_Structure<IGH_Goo>();
    tree.Read(treeChunk);

    _storage.Add(key, tree);
  }
}
2 Likes