# JUCE, JSON and leakage

**URL:** <https://forum.juce.com/t/juce-json-and-leakage/50961>\
**Category:** General JUCE discussion\
**Created:** [April 10, 2022, 1:29pm UTC](https://forum.juce.com/t/juce-json-and-leakage/50961 "2022-04-10T13:29:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardfoss](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@richardfoss](https://forum.juce.com/u/richardfoss)\
**Post date:** [April 10, 2022, 1:29pm UTC](https://forum.juce.com/t/juce-json-and-leakage/50961/1 "2022-04-10T13:29:32Z")

</div>

Hi, I’ve recently returned to C++ after a long holiday in Javascript. I am attempting to create a JSON representation of sound sources and their mix values to be applied to each of a number of speakers, where the speakers are in different zones.

I have a few questions about the JSON construction, and would appreciate some guidelines:

1. I create the mix values

```auto
    Array<var> speakerMixVarLevels;

    // Create a JUCE array of var values for each mix level - can be added as a property
    for (int i = 0; i < zonePtr->noSpeakers; i++) {
        speakerMixVarLevels.add(speakerMixLevels[i]);
    }

    //DynamicObject::Ptr mixLevelsObject = new DynamicObject();

    // The final return object containing the speaker mix values
    DynamicObject* mixLevelsObject = new DynamicObject();
   
    mixLevelsObject->setProperty("Zone", zonePtr->Name);
    mixLevelsObject->setProperty("Channel", channel);
    mixLevelsObject->setProperty("MixValues", speakerMixVarLevels);

    //Make the object a var type for inclusion in the mix matrix JSON
    var mixLevelsVar(mixLevelsObject);

```

I found that I was not able to use DynamicObject::Ptr if I wanted to make the DynamicObject to be type var. Why is this?

1. I would like to pass back var objects to functions that do further JSON aggregation:

```auto
return mixLevelsVar;

and also pass var objects and var arrays (Array<var>) to functions:

```

var SoundSource::renderSoundSource(var soundObject) {

```auto
Does this pose a problem for reference counting? Should one rather pass strings between functions, and parse the strings? I have been getting leakage errors, hence my question.

Thanks!
```

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [April 20, 2022, 2:55pm UTC](https://forum.juce.com/t/juce-json-and-leakage/50961/2 "2022-04-20T14:55:33Z")

</div>

> [@richardfoss](#):
>
> I found that I was not able to use DynamicObject::Ptr if I wanted to make the DynamicObject to be type var. Why is this?

Try

```
var mixLevelsVar (mixLevelsObject.get());

```

> [@richardfoss](#):
>
> `Does this pose a problem for reference counting?`

It shouldn’t. The objects will be copied.

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [April 20, 2022, 3:22pm UTC](https://forum.juce.com/t/juce-json-and-leakage/50961/3 "2022-04-20T15:22:24Z")

</div>

If you want to create and work with JSON in C++ I recommend the [nlohmann json library](https://github.com/nlohmann/json) over `juce::var` for ease of use.

---

<div class="post-metadata">

**Author:** ![jrlanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jrlanglois/32/21372_2.png) [@jrlanglois](https://forum.juce.com/u/jrlanglois)\
**Post date:** [April 20, 2022, 6:13pm UTC](https://forum.juce.com/t/juce-json-and-leakage/50961/4 "2022-04-20T18:13:51Z")

</div>

I find JUCE’s `var` stuff less cumbersome than something like RapidJSON, but this library looks great. There are some cool ideas in there that JUCE could have like the user-defined literal, and more importantly some kind of `initializer_list` parser to create things inline. This way, at least creation is more intuitive. I’m not sure about suggestions in terms of accessing/mutating…

---

<div class="post-metadata">

**Author:** ![richardfoss](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@richardfoss](https://forum.juce.com/u/richardfoss)\
**Post date:** [April 20, 2022, 7:18pm UTC](https://forum.juce.com/t/juce-json-and-leakage/50961/5 "2022-04-20T19:18:27Z")

</div>

> var mixLevelsVar (mixLevelsObject.get());

Thank you very much, I will try this shortly.

Also, thanks for the confirmation about var parameter passing/returns and their lack of effect on reference counting - very helpful!

---

<div class="post-metadata">

**Author:** ![richardfoss](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@richardfoss](https://forum.juce.com/u/richardfoss)\
**Post date:** [May 1, 2022, 4:44pm UTC](https://forum.juce.com/t/juce-json-and-leakage/50961/6 "2022-05-01T16:44:26Z")

</div>

Apologies, slow with feedback - the approach you suggested plugged my leaks! Here is some code in case anyone has a similar problem. Its immersive sound code, where I determine speaker mix values for sound sources:

```auto
		// A JUCE array for each speaker mix level of the source
		for (int j = 0; j < noSpeakers; j++) {
			speakerMixVarLevels.add(mixValues[j]);
		}

		//An object which contains zone, channel and speaker mix levels for the source
		DynamicObject::Ptr mixLevelsObject = new DynamicObject();

		mixLevelsObject->setProperty("Zone", zoneName);
		mixLevelsObject->setProperty("Channel", channel);
		mixLevelsObject->setProperty("MixValues", speakerMixVarLevels);

		var mixLevelsVar(mixLevelsObject.get());
		mixMatrixObjects.add(mixLevelsVar); //Add it to the array of speaker mix levels for each source

```

Thank you very much!
