My var class confusion


Hi all.

I just finished my implementation for the TracktionMarketPlaceStatus with my own web server.

Everything seems to go ok, except one !!  

Web server answers to my program with a JSON data file. 

So, my choise was a var object, to parse JSON data and to handle these data in my program.

The data I receive seems like :



 and my code is :

    var AppRegistrationInfo;
    Result Res = JSON::parse(Server_Reply, AppRegistrationInfo);

But....but, I have not realize how to get out any specific value, e.g, the value of "success" property.

I really appreciate any help..



For example like this:

String json ("{\"success\":true, \"license\":\"invalid\", \"item_name\":\"JuceAppEnvTest\"}");

var parsedJson;

if (JSON::parse (json, parsedJson).wasOk())
    if (DynamicObject* obj = parsedJson.getDynamicObject())
        NamedValueSet& props (obj->getProperties());
        bool success = props.getWithDefault ("success", false);

This is based on me staring at the existing JUCE code. There might be a more elegant way to do it I haven't found yet.


Thanks for your help.. 

I tested the following expressions and all them seem to work ok.  

bool     Success_Status = AppRegistrationInfo.getProperty(Identifier("success"), false);
String  Product_ID_From_Server = AppRegistrationInfo.getProperty(Identifier(Val_ItemName), "Not Set");
int       Site_Count = AppRegistrationInfo.getProperty(Identifier("site_count"), 0);

where AppRegistrationInfo is the var object produced by JSON parser. 



I do wish there was a more elegant way to deal with var and json parsing in JUCE. Been doing a lot of javascript / ES6 recently and coming to do the JUCE side of things feels quite abrasive in comparison! 


Ok, some var class futures are not documended in the class documentation, but,  I must had a look in the var class code before I put my question here.

I tested the above expressions, based on my suspicion because of the name of the class, and after that I had a closer look on var's code.

Anyway, I feel that Jules dislikes a little bit  to what is not xml, but I'm sure that it will be better because many times out there, data exchanged in this JSON way. 



FYI var also has some operator[] methods and var::getProperty, which skip the need to get the DynamicObject first.


var parsedJson;

if (JSON::parse (json, parsedJson).wasOk())
    bool success = parsedJson["success"];




oh.. and you don't need to get a Result object either, e.g. 

var parsedJson = JSON::parse (json);

bool success = parsedJson["success"];   // will still return false if the parse failed and parsedJson is void


Ok, thanks Jules 

I saw all these futures within the var's code.


Sorry if my above post seemed negative. I really do appreciate the var and DynamicObject classes I just find using them a little convoluted in some cases. 

For instance, if I want to add a property to a var, I initially expected to be able to just do something like:

​myVar["prop"] = String("blah");

But instead I have to get the DynamicObject from the var, then its NamedValueSet, then set the property... I guess thats just how the class is structured. 

I wrote a helper method which might be useful to someone: 

    /** Function to simplify adding properties with / to vars
    var propertyAsVar(const String& name, const String &value, var varToAddTo = var())
        DynamicObject* obj = nullptr;
        if (varToAddTo.isVoid())
           obj = new DynamicObject();
           obj = varToAddTo.getDynamicObject();
        NamedValueSet& properties = obj->getProperties();
        properties.set (name, value);
        return var (obj);

I wonder if a method like that could be added to the [] operator of the var class? 


BTW Jules how do you get your code snippets syntax highlighted like that? 


You write your code into the post, then select it, and then at the top left in the "Styles" menu (the one which says "Paragraph" initially) you scroll all the way down and select the style "code: highlighted".


That's not a feature we common peasants have, must be restricted to admin only.

In the "style" menu all we have is a single entry named "Code" (with capital C)



Hm, well, then I guess you can't do it then :-( sorry.


Just as well IMO… The black background makes printing the code impractical (impossible to read and empties the black ink cartridge).



I tumbled on the same question for conveniently exchanging structured messages between Action Broadcasters and Listener in my App. After reviewing StringPairArray and PropertySet that lacked simple serialize/deserialize facilities, the juce::var <-> JSON synergy was most promising but how to set a property on a juce::var? thank you for your tip! coding a structured Action message becomes:

// sending side (readability can be improved with a bunch of #define's)
var v{ new DynamicObject() };
v.getDynamicObject()->setProperty(Identifier("HELLO"), var("world"));
v.getDynamicObject()->setProperty(Identifier("FOO"), var(1234));
v.getDynamicObject()->setProperty(Identifier("BAR"), var(juce::String(L"âéïôù")));
sendActionMessage(JSON::toString(v, true));

// receiver side, inside the actionListenerCallback(const String& message)
var v = JSON::fromString(message);
String s = v["HELLO"]; // by magic of operators on var
int i = v["FOO"];

the receiving side is quite elegant!