Files
wlan-cloud-lib-poco/JSON/include/Poco/JSON/Query.h
Aleksandar Fabijanic d8397b9153 - VS2012: build/tests
- JSON: style and exceptions catch by ref fix
- NumberParser: case-insensitive hex parsing
- Dynamic::Var: const std::string& cast operator specialization (coaxing std::string ctor)
- LocalDateTime: strftime fix (buffer too small for full tz string, use %Z instead of %z - makes no difference on windows but is more portable)
- small buildwin.cmd vsvars32.bat call fix
2012-09-24 03:51:40 +00:00

111 lines
3.2 KiB
C++

//
// Query.h
//
// $Id$
//
// Library: JSON
// Package: JSON
// Module: Query
//
// Definition of the Query class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_JSONQuery_INCLUDED
#define JSON_JSONQuery_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
namespace Poco {
namespace JSON {
class JSON_API Query
/// Class that can be used to search for a value in a JSON object or array.
{
public:
Query(const Dynamic::Var& source);
/// Constructor. Pass the start object/array.
virtual ~Query();
/// Destructor
Object::Ptr findObject(const std::string& path) const;
/// Search for an object. When the object can't be found, an empty
/// SharedPtr is returned.
Array::Ptr findArray(const std::string& path) const;
/// Search for an array. When the array can't be found, an empty
/// SharedPtr is returned.
Dynamic::Var find(const std::string& path) const;
/// Searches a value
/// For 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.
{
T result = def;
Dynamic::Var value = find(path);
if ( ! value.isEmpty() )
{
try
{
result = value.convert<T>();
}
catch(...) { }
}
return result;
}
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.
{
return findValue<std::string>(path, def);
}
private:
Dynamic::Var _source;
};
}} // namespace Poco::JSON
#endif // JSON_JSONQuery_INCLUDED