Value & ValueTree example usage


#1

Hi,

I wonder if you have a (simple) code to show how to use the value & tree you’ve added.
I’m looking for a hierarchical container à la DOM tree that could easily be serialized to xml.
From the documentation, I wonder it could be used, but I don’t understand how it serialize things (and what is the property, type and name of a tree?)
How does it serialize objects in variant ?


#2

I’ve not got any example code, but you could take a look in the Drawable classes, which use it for serialisation.


#3

So if I sum this up, Value’s properties are serialized as attribute. The value type is the node name.
So this code:

ValueTree val(String("root"));
val.setProperty(String("prop"), "abc", 0);
val.setProperty(String("prop2"), 3, 0);

ValueTree main(String("main"));
main.addChild(val, -1, 0);
XmlElement * el = main.createXml();
el->writeToFile(BasePath.getChildFile(String("Example.xml")), String::empty);
delete el; 
/* Will produce an xml containing:
<?xml version="1.0" encoding="UTF-8"?>
<main>
  <root prop="abc" prop2="3"/>
</main>
*/

BTW, while testing this, there is a bug in the code if you enter ValueTree main(“Sponge bob”), it produce an invalid XML ()


#4

Hmm - I should put some assertions in there to catch non-xml-friendly names.


#5

What about a ValueTree::setType method ?
Currently, the only way to change the value type is to use operator = with a temporary, which is quite a waste of resources.


#6

The way I intended the type to be used is like an XML tag - it describes the purpose of the valuetree, not its current state. If you find you need to change the type, then maybe you should be using a property instead?