Entity: Added check for single-object arrays

This commit is contained in:
tastytea 2018-03-31 06:52:22 +02:00
parent 168a4926d1
commit c1174c0061
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
1 changed files with 10 additions and 2 deletions

View File

@ -30,17 +30,25 @@ Easy::Easy(const string &instance, const string &access_token)
{}
Easy::Entity::Entity(const string &json)
: _valid(false)
: _tree(Json::nullValue)
, _valid(false)
{
std::stringstream ss(json);
ss >> _tree;
// If the JSON is a single object encapsulated in an array,
// transform it into an object. If the JSON string is [], transform to null
if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1)
{
_tree = _tree[0];
}
if (_tree.isNull())
{
ttdebug << "ERROR: JSON string holds no object\n";
ttdebug << "String was: " << json << '\n';
}
else if (_tree["error"].isString())
else if (!_tree["error"].isNull())
{
ttdebug << "ERROR: Server returned an error\n";
ttdebug << "String was: " << json << '\n';