mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-19 08:46:41 +01:00
Merge pull request #268 from RangelReale/develop
* Method to get JSON object value using Poco::Nullable
This commit is contained in:
commit
106d011db0
@ -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
|
||||
|
||||
|
@ -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());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user