# Store Custom Class in ValueTree

**URL:** <https://forum.juce.com/t/store-custom-class-in-valuetree/44019>\
**Category:** General JUCE discussion\
**Created:** [January 25, 2021, 1:30pm UTC](https://forum.juce.com/t/store-custom-class-in-valuetree/44019 "2021-01-25T13:30:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolezim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolezim/32/8210_2.png) [@nicolezim](https://forum.juce.com/u/nicolezim)\
**Post date:** [January 25, 2021, 1:30pm UTC](https://forum.juce.com/t/store-custom-class-in-valuetree/44019/1 "2021-01-25T13:30:46Z")

</div>

Hi!  
I’ve started using Value Trees and I am able to store and retrieve basic types like strings or floats, as they are easily cast to `juce::var`. My question is: how is it possible to pass custom classes into a Value Tree?

I have a custom class like this, very simple:

```
class Scale
{
public:
    Scale(int tonic, NotePattern scaleTemplatePattern, juce::String scaleType);
    virtual ~Scale();

    juce::String getName() const;
    juce::String getType() const;
    int getTonic() const;
    NotePattern getNotePattern() const;

    bool doesIncludePitch(int pitch) const;
    bool doesIncludePattern(NotePattern& notePattern) const;
    
private:
    juce::String _name;
    juce::String _scaleType;
    int _tonic;
    NotePattern _notePattern;

};

```

And now I want to store an instance of this class as a Value Tree property.  
I’ve read about dynamic\_casts and ReferenceCountedObjects, but I’m still unsure of how to go about this.

I’d really appreciate any guidance or help on this. 🙂  
Thanks!

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [January 25, 2021, 2:01pm UTC](https://forum.juce.com/t/store-custom-class-in-valuetree/44019/2 "2021-01-25T14:01:39Z")

</div>

I would do this as a ValueTree child. `<Scale name="" type="" tonic="" notePattern="">`. I have a base class called ValueTreeWrapper, which I derive domain specific classes from, which have setters/getters/callbacks… This is the basic idea. The wrapper is meant to only be used locally, not shared, in that it only has single callbacks, not lists. This is super useful in that I don’t have to think about the ValueTree specifics in the everyday code. 🙂

```auto
class ScaleWrapper : public ValueTreeWrapper
{
public:
    void setName (String name);
    void setType (ScaleType type);

   String getName ();
   ScaleType getType ();

   std::function<void(String name) onNameChange;
   std::fucntion<void(ScaleType type) onTypeChange;

private:
}
```

---

<div class="post-metadata">

**Author:** ![jakemumu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jakemumu/32/13038_2.png) [@jakemumu](https://forum.juce.com/u/jakemumu)\
**Post date:** [January 25, 2021, 3:36pm UTC](https://forum.juce.com/t/store-custom-class-in-valuetree/44019/3 "2021-01-25T15:36:31Z")

</div>

You can only really store basic types in the value tree. Under the hood it converts to XML, so if it’s not a type XML will support it won’t convert nicely.

This sometimes means arrays etc need to get a bit funky in there… I’ve tried store some JSON strings but that leads to weird formatting in the ValueTree if you look at the XML. I’ve settled on generating comma delimited strings for arrays, but in essence I’ll usually add to my class:

```auto
/* */
String getValueTreeID();

/* */
ValueTree toValueTree() {}

/* */
void updateFromValueTree(ValueTree& inTree) {}

```

Then use this for create serialization functions, and add the value tree for the specific object / class as a child tree / node in the parent ValueTree

then you can do something like:

```auto
ValueTree& child = parent_tree.getChildForID(class->getTreeID);
class->updateFromValueTree(child);

```

---

<div class="post-metadata">

**Author:** ![nicolezim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolezim/32/8210_2.png) [@nicolezim](https://forum.juce.com/u/nicolezim)\
**Post date:** [January 25, 2021, 3:54pm UTC](https://forum.juce.com/t/store-custom-class-in-valuetree/44019/4 "2021-01-25T15:54:14Z")

</div>

Thank you @cpr2323 and @jakemumu for the ideas!  
I’ll try some things 🙂

---

<div class="post-metadata">

**Author:** ![nicolezim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolezim/32/8210_2.png) [@nicolezim](https://forum.juce.com/u/nicolezim)\
**Post date:** [January 25, 2021, 5:36pm UTC](https://forum.juce.com/t/store-custom-class-in-valuetree/44019/5 "2021-01-25T17:36:11Z")

</div>

I just watched this talk from ADC '17 that explains @cpr2323’s method of using a wrapper in more detail. Great stuff, really helpful for others wondering about the some problem 🙂

[![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/c/c5649877bbf425b76c2a5351b8be6a6c30fb7fa8.jpeg "David Rowland - Using JUCE value trees and modern C++ to build large scale applications (ADC'17)") ](https://www.youtube.com/watch?v=3IaMjH5lBEY)

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [January 25, 2021, 6:04pm UTC](https://forum.juce.com/t/store-custom-class-in-valuetree/44019/6 "2021-01-25T18:04:57Z")

</div>

This is the exact talk that gave me the ValueTree education required to not only start using them, but to really lean into them for data models.
