Merge pull request #268 from RangelReale/develop

* Method to get JSON object value using Poco::Nullable
This commit is contained in:
Aleksandar Fabijanic 2013-09-14 19:18:53 -07:00
commit 106d011db0
2 changed files with 21 additions and 0 deletions

View File

@ -46,6 +46,7 @@
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/Dynamic/Struct.h"
#include "Poco/Nullable.h"
#include <map>
#include <vector>
#include <deque>
@ -144,6 +145,22 @@ public:
return value.convert<T>();
}
template<typename T>
Poco::Nullable<T> getNullableValue(const std::string& key) const
/// Retrieves the property with the given name and will
/// try to convert the value to the given template type.
/// Returns null if isNull.
/// The convert<T> method of Dynamic is called
/// which can also throw exceptions for invalid values.
/// Note: This will not work for an array or an object.
{
if (isNull(key))
return Poco::Nullable<T>();
Dynamic::Var value = get(key);
return value.convert<T>();
}
void getNames(std::vector<std::string>& names) const;
/// Returns all property names

View File

@ -51,6 +51,7 @@
#include "Poco/UTF8Encoding.h"
#include "Poco/Latin1Encoding.h"
#include "Poco/TextConverter.h"
#include "Poco/Nullable.h"
#include "Poco/Dynamic/Struct.h"
@ -105,6 +106,9 @@ void JSONTest::testNullProperty()
Var test = object->get("test");
assert(test.isEmpty());
Poco::Nullable<int> test2 = object->getNullableValue<int>("test");
assert(test2.isNull());
DynamicStruct ds = *object;
assert (ds["test"].isEmpty());