# Feature request: get all properties available from a var

**URL:** <https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001>\
**Category:** General JUCE discussion\
**Created:** [November 5, 2017, 9:41pm UTC](https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001 "2017-11-05T21:41:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [November 5, 2017, 9:41pm UTC](https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001/1 "2017-11-05T21:41:49Z")

</div>

Hi,

while using the JSON class and with it the var class for the first time I was missing an option to get all properties possible for a particiular var to parse a json file with unknown content.

Maybe I overlooked something, but otherwise a I think a function like

```
StringArray var::getAllProperties()

```

that returns an array with all possible Identifiers that could be passed to getProperty would be very helpful.

---

<div class="post-metadata">

**Author:** ![martinrobinson-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/martinrobinson-2/32/630_2.png) [@martinrobinson-2](https://forum.juce.com/u/martinrobinson-2)\
**Post date:** [November 5, 2017, 10:10pm UTC](https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001/2 "2017-11-05T22:10:35Z")

</div>

A free function to do this is pretty simple:

```
static Array<Identifier> getAllPropertyNames (const var& v)
{
    Array<Identifier> names;
    
    if (auto* const obj = v.getDynamicObject())
    {
        auto& properties = obj->getProperties();

        for (const auto& property : properties)
            names.add (property.name);
    }
    
    return names;
}

```

Usage:

```
auto* const p = new DynamicObject();

p->setProperty ("name", "Bob");
p->setProperty ("age", 40);
p->setProperty ("height", 1.7);

var v (p);

auto names = getAllPropertyNames (v);

for (const auto& name : names)
    Logger::writeToLog (name.toString());

```

Or, for your use-case you could just get the dynamic object, get its properties (a `NamedValueSet`) and just iterate over all the properties and their values.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 5, 2017, 10:10pm UTC](https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001/3 "2017-11-05T22:10:56Z")

</div>

Have a look at [DynamicObject](https://juce.com/doc/classDynamicObject).

```
if (auto* object = myVar.getDynamicObject()) {
    for (auto prop : object->getProperties())
        DBG ("Property: " << prop.name << " is: " << prop.value);
}
```

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [November 6, 2017, 8:39am UTC](https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001/4 "2017-11-06T08:39:21Z")

</div>

Ok cool, thank you. I wouldn‘t have found out that by myself, mostly because there is no description on var::getDynamicObject in the documentation, but it works that way.

But I‘d also like to understand whats happening here, how DynamicObject and var are related. When setting a breakpoint to step into getDynamicObject, I see a dynamic\_cast casting a var to a DynamicObject. Could someone explain why this works?

---

<div class="post-metadata">

**Author:** ![martinrobinson-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/martinrobinson-2/32/630_2.png) [@martinrobinson-2](https://forum.juce.com/u/martinrobinson-2)\
**Post date:** [November 6, 2017, 9:19am UTC](https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001/5 "2017-11-06T09:19:15Z")

</div>

`DynamicObject` is a kind of `ReferenceCountedObject` and a `var` can hold _any_ kind of `ReferenceCountedObject`. `DynamicObject` holds a set of name-value pairs (in a `NamedValueSet`) where the names are `Identifier` objects and the values are `var` objects. This is just a special case of `ReferenceCountedObject` that `var` knows about in order to be able to represent the JSON-like data. The `dynamic_cast` is required because `var` stores the objects as a `ReferenceCountedObject*` and it _may_ be storing some other kind of `ReferenceCountedObject` (but this would only be if you were using `var` for purposes other than serialising and deserialising JSON).

I don’t …

> [@JanosJuce](#):
>
> see a dynamic\_cast casting a var to a DynamicObject.

?

There are some `dynamic_cast`s but I think you might be misreading the code?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 6, 2017, 10:03am UTC](https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001/6 "2017-11-06T10:03:06Z")

</div>

The var implements the assignment operator for each type it can represent, e.g. this one: [var::operator=()](https://juce.com/doc/classvar#af25c16f53ee21ad9824c9d0933652304)

If the lvalue of the equation has already a type, the compiler picks the right override for you. If it is ambiguous, the compile will fail, and you have to do an explicit `dynamic_cast<type>` to force the compiler to pick that override. And if you use the `auto` keyword, the compiler cannot know, hence you need to use the explicit cast.

Like @martinrobinson-2 pointed out, getDynamicObject() is just a shorthand, that checks, if the wrapped object is a DynamicObject (by trying the dynamic\_cast). It can return a nullptr, because the var might not be an object at all, or any other descendant of ReferenceCountedObject.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [November 6, 2017, 10:41am UTC](https://forum.juce.com/t/feature-request-get-all-properties-available-from-a-var/25001/7 "2017-11-06T10:41:00Z")

</div>

> [@martinrobinson-2](#):
>
> I don’t …
> 
> JanosJuce:
> 
> ```
> see a dynamic_cast casting a var to a DynamicObject.
> 
> ```
> 
> ?
> 
> There are some dynamic\_casts but I think you might be misreading the code?

Yes I was misreading code 😉  
After reading your explanation and then reading the code again, I now think I got it!
