Added Easy::Entity::from_string() and Easy::json_array_to_vector()

This commit is contained in:
tastytea 2018-03-31 20:03:31 +02:00
parent d37f1aefb7
commit 3d2c636d54
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
3 changed files with 44 additions and 1 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.7.15
VERSION 0.7.16
LANGUAGES CXX
)

View File

@ -29,9 +29,35 @@ Easy::Easy(const string &instance, const string &access_token)
: API(instance, access_token)
{}
const std::vector<string> Easy::json_array_to_vector(const string &json)
{
Json::Value json_array;
std::stringstream ss(json);
ss >> json_array;
if (json_array.isArray())
{
std::vector<string> vec;
for (const Json::Value &value : json_array)
{
vec.push_back(value.toStyledString());
}
return vec;
}
ttdebug << "ERROR: JSON string holds no array\n";
ttdebug << "String was: " << json << '\n';
return {};
}
Easy::Entity::Entity(const string &json)
: _tree(Json::nullValue)
, _valid(false)
{
from_string(json);
}
const void Easy::Entity::from_string(const string &json)
{
std::stringstream ss(json);
ss >> _tree;

View File

@ -103,6 +103,15 @@ public:
*/
explicit Easy(const string &instance, const string &access_token);
/*!
* @brief Turns an JSON array into a vector of strings
*
* @param json JSON string holding the array
*
* @return vector of strings or an empty vector on error
*/
static const std::vector<string> json_array_to_vector(const string &json);
/*!
* @brief Base class for entities.
*/
@ -121,6 +130,14 @@ public:
*/
Entity();
/*!
* @brief Replaces the Entity object with a new one from a JSON
* string.
*
* @param json JSON string
*/
const void from_string(const string &json);
/*!
* @brief Returns true if the Entity holds valid data
*/