Reading and writing application settings as JSON

I’m new to C++ and JUCE. I want to read and write my application settings from a file. I’m using a simple JSON structure:

{"property1":"value1", "prop2":"value2"}

I can use JSON class to parse the file and access the properties through a var object, but I see no way to set new values, except by creating a DynamicObject like this:
config = JSON::parse (“thefile.json”);
auto obj (config.getDynamicObject());
obj->setProperty(“projectName”, “This is the new project name”);

Seems like an extra step. Is there a better way of doing this? Thanks for any suggestions!

You may want to check out juce::PropertiesFile, it lets you manage a properties file as a juce::PropertySet. With the JSON approach you probably will be constrained by having to use getDynamicObject()

Thanks! I ended up using DynamicObject to do this because I can’t figure out where to instantiate the PropertiesFile object. I wanted it to be a member of my Config class and have the class read/write the properties and manage them for the rest of the application. It has been hard to find examples of relevant code online!

Look in the Useful Tools category on the forum for my ScopedValueSaver class

Yeah, JUCE and JSON are not the best of friends… There’s a bit of hoop-jumping required to create and manipulate JSON structures using var and the underlying DynamicObject and NamePropertySet. I created a helper method that might be useful:

/** Create a new var property, and either add it to an existing var object
    or create a new one. Returns the new or updated var object.
    @param name         property name
    @param value        property value
    @param varToAddTo   var to add property to, defaults to empty
static var propertyAsVar(const String& name, const var &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);

If you want to use JSON heavily in your project it might be good to look at e.g. JsonCpp