class Poco::Util::JSONConfiguration

Overview

This configuration class extracts configuration properties from a JSON object. More…

#include <JSONConfiguration.h>

class JSONConfiguration: public Poco::Util::AbstractConfiguration
{
public:
    // construction

    JSONConfiguration();
    JSONConfiguration(const std::string& path);
    JSONConfiguration(std::istream& istr);
    JSONConfiguration(const JSON::Object::Ptr& object);

    // methods

    void
    load(const std::string& path);

    void
    load(std::istream& istr);

    void
    loadEmpty(const std::string& root);

    void
    save(
        std::ostream& ostr,
        unsigned int indent = 2
        ) const;

    virtual
    void
    setInt(
        const std::string& key,
        int value
        );

    virtual
    void
    setBool(
        const std::string& key,
        bool value
        );

    virtual
    void
    setDouble(
        const std::string& key,
        double value
        );

    virtual
    void
    setString(
        const std::string& key,
        const std::string& value
        );

    virtual
    void
    removeRaw(const std::string& key);

protected:
    // methods

    virtual
    bool
    getRaw(
        const std::string& key,
        std::string& value
        ) const;

    virtual
    void
    setRaw(
        const std::string& key,
        const std::string& value
        );

    virtual
    void
    enumerate(
        const std::string& key,
        Keys& range
        ) const;
};

Inherited Members

public:
    // typedefs

    typedef std::vector<std::string> Keys;

    // classes

    class KeyValue;

    // fields

    Poco::BasicEvent<KeyValue> propertyChanging;
    Poco::BasicEvent<const KeyValue> propertyChanged;
    Poco::BasicEvent<const std::string> propertyRemoving;
    Poco::BasicEvent<const std::string> propertyRemoved;

    // methods

    void
    duplicate() const;

    void
    release() const;

    int
    referenceCount() const;

    bool
    hasProperty(const std::string& key) const;

    bool
    hasOption(const std::string& key) const;

    bool
    has(const std::string& key) const;

    std::string
    getString(const std::string& key) const;

    std::string
    getString(
        const std::string& key,
        const std::string& defaultValue
        ) const;

    std::string
    getRawString(const std::string& key) const;

    std::string
    getRawString(
        const std::string& key,
        const std::string& defaultValue
        ) const;

    int
    getInt(const std::string& key) const;

    unsigned int
    getUInt(const std::string& key) const;

    int
    getInt(
        const std::string& key,
        int defaultValue
        ) const;

    unsigned int
    getUInt(
        const std::string& key,
        unsigned int defaultValue
        ) const;

    double
    getDouble(const std::string& key) const;

    double
    getDouble(
        const std::string& key,
        double defaultValue
        ) const;

    bool
    getBool(const std::string& key) const;

    bool
    getBool(
        const std::string& key,
        bool defaultValue
        ) const;

    virtual
    void
    setString(
        const std::string& key,
        const std::string& value
        );

    virtual
    void
    setInt(
        const std::string& key,
        int value
        );

    virtual
    void
    setUInt(
        const std::string& key,
        unsigned int value
        );

    virtual
    void
    setDouble(
        const std::string& key,
        double value
        );

    virtual
    void
    setBool(
        const std::string& key,
        bool value
        );

    void
    keys(Keys& range) const;

    void
    keys(
        const std::string& key,
        Keys& range
        ) const;

    const AbstractConfiguration*
    createView(const std::string& prefix) const;

    AbstractConfiguration*
    createView(const std::string& prefix);

    std::string
    expand(const std::string& value) const;

    void
    remove(const std::string& key);

    void
    enableEvents(bool enable = true);

    bool
    eventsEnabled() const;

protected:
    // methods

    virtual
    bool
    getRaw(
        const std::string& key,
        std::string& value
        ) const = 0;

    virtual
    void
    setRaw(
        const std::string& key,
        const std::string& value
        ) = 0;

    virtual
    void
    enumerate(
        const std::string& key,
        Keys& range
        ) const = 0;

    virtual
    void
    removeRaw(const std::string& key);

    void
    setRawWithEvent(
        const std::string& key,
        std::string value
        );

    static
    int
    parseInt(const std::string& value);

    static
    unsigned
    parseUInt(const std::string& value);

    static
    bool
    parseBool(const std::string& value);

Detailed Documentation

This configuration class extracts configuration properties from a JSON object.

An XPath-like syntax for property names is supported to allow full access to the JSON object.

Given the following JSON object as an example: {

"config" : {
   "prop1" : "value1",
   "prop2" : 10,
   "prop3" : [
     "element1",
     "element2"
   ],
   "prop4" : {
     "prop5" : false,
     "prop6" : null
   }
}

} The following property names would be valid and would yield the shown values:

config.prop1> “value1” config.prop3[1]> “element2” config.prop4.prop5> false

Construction

JSONConfiguration()

Creates an empty configuration.

JSONConfiguration(const std::string& path)

Creates a configuration and loads the JSON structure from the given file.

JSONConfiguration(std::istream& istr)

Creates a configuration and loads the JSON structure from the given stream.

JSONConfiguration(const JSON::Object::Ptr& object)

Creates a configuration from the given JSON object.

Methods

void
load(const std::string& path)

Loads the configuration from the given file.

void
load(std::istream& istr)

Loads the configuration from the given stream.

void
loadEmpty(const std::string& root)

Loads an empty object containing only a root object with the given name.

void
save(
    std::ostream& ostr,
    unsigned int indent = 2
    ) const

Saves the configuration to the given stream.

virtual
void
setInt(
    const std::string& key,
    int value
    )

Sets the property with the given key to the given value.

An already existing value for the key is overwritten.

virtual
void
setBool(
    const std::string& key,
    bool value
    )

Sets the property with the given key to the given value.

An already existing value for the key is overwritten.

virtual
void
setDouble(
    const std::string& key,
    double value
    )

Sets the property with the given key to the given value.

An already existing value for the key is overwritten.

virtual
void
setString(
    const std::string& key,
    const std::string& value
    )

Sets the property with the given key to the given value.

An already existing value for the key is overwritten.

virtual
void
removeRaw(const std::string& key)

Removes the property with the given key.

Does nothing if the key does not exist.

Should be overridden by subclasses; the default implementation throws a Poco::NotImplementedException.

virtual
bool
getRaw(
    const std::string& key,
    std::string& value
    ) const

If the property with the given key exists, stores the property’s value in value and returns true.

Otherwise, returns false.

Must be overridden by subclasses.

virtual
void
setRaw(
    const std::string& key,
    const std::string& value
    )

Sets the property with the given key to the given value.

An already existing value for the key is overwritten.

Must be overridden by subclasses.

virtual
void
enumerate(
    const std::string& key,
    Keys& range
    ) const

Returns in range the names of all subkeys under the given key.

If an empty key is passed, all root level keys are returned.