class Poco::JSON::Query

Overview

Class that can be used to search for a value in a JSON object or array. More…

#include <Query.h>

class Query
{
public:
    // construction

    Query(const Dynamic::Var& source);

    // methods

    Object::Ptr
    findObject(const std::string& path) const;

    Object&
    findObject(
        const std::string& path,
        Object& obj
        ) const;

    Array::Ptr
    findArray(const std::string& path) const;

    Array&
    findArray(
        const std::string& path,
        Array& obj
        ) const;

    Dynamic::Var
    find(const std::string& path) const;

    template <typename T>
    T
    findValue(
        const std::string& path,
        const T& def
        ) const;

    std::string
    findValue(
        const char* path,
        const char* def
        ) const;
};

Detailed Documentation

Class that can be used to search for a value in a JSON object or array.

Construction

Query(const Dynamic::Var& source)

Creates a Query/.

Source must be JSON Object, Array, Object::Ptr, Array::Ptr or empty Var. Any other type will trigger throwing of InvalidArgumentException.

Creating Query holding Ptr will typically result in faster performance.

Methods

Object::Ptr
findObject(const std::string& path) const

Search for an object.

When the object can’t be found, a zero Ptr is returned; otherwise, a shared pointer to internally held object is returned. If object (as opposed to a pointer to object) is held internally, a shared pointer to new (heap-allocated) Object is returned; this may be expensive operation.

Object&
findObject(
    const std::string& path,
    Object& obj
    ) const

Search for an object.

If object is found, it is assigned to the Object through the reference passed in. When the object can’t be found, the provided Object is emptied and returned.

Array::Ptr
findArray(const std::string& path) const

Search for an array.

When the array can’t be found, a zero Ptr is returned; otherwise, a shared pointer to internally held array is returned. If array (as opposed to a pointer to array) is held internally, a shared pointer to new (heap-allocated) Object is returned; this may be expensive operation.

Array&
findArray(
    const std::string& path,
    Array& obj
    ) const

Search for an array.

If array is found, it is assigned to the Object through the reference passed in. When the array can’t be found, the provided Object is emptied and returned.

Dynamic::Var
find(const std::string& path) const

Searches a value.

Example: “person.children[0].name” will return the the name of the first child. When the value can’t be found an empty value is returned.

template <typename T>
T
findValue(
    const std::string& path,
    const T& def
    ) const

Searches for a value will convert it to the given type.

When the value can’t be found or has an invalid type the default value will be returned.

std::string
findValue(
    const char* path,
    const char* def
    ) const

Searches for a value will convert it to the given type.

When the value can’t be found or has an invalid type the default value will be returned.