freepascal: JSON and recursive data structure

349 Views Asked by At

Can fpjson handle load AND save of the following structure from/to json:

PTreeStructure = ^TTreeStructure;
TTreeStructure = class
  name: string;
  value: Integer;
  items: array of PTreeStructure;
end;

Thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

fcl-json can store nested data (an object can have an array of objects, which can have an array of objects, see the supplied examples)

But that way you store the objects themselves, not the references, which is fine for a tree, but not for cycling structures.

A DAG can be represented by storing a flat array with all data, but store items with indexes or some form of unique ID per record into the array instead of pointers.

If your root isn't the first element, you need to additionally store the index/iD of the root somewhere