Base class for all entities.
More...
#include <easy.hpp>
|
const Json::Value | get (const string &key) const |
| Returns the value of key as Json::Value. More...
|
|
const string | get_string (const string &key) const |
| Returns the value of key as std::string. More...
|
|
const uint_fast64_t | get_uint64 (const string &key) const |
| Returns the value of key as std::uint_fast64_t. More...
|
|
const double | get_double (const string &key) const |
| Returns the value of key as double. More...
|
|
const bool | get_bool (const string &key) const |
| Returns the value of key as bool. More...
|
|
const system_clock::time_point | get_time_point (const string &key) const |
| Returns the value of key as time_point. More...
|
|
const std::vector< string > | get_vector (const string &key) const |
| Returns the value of key as vector. More...
|
|
const void | set (const string &key, const Json::Value &value) |
| Sets the value of key. More...
|
|
const std::uint_fast64_t | stouint64 (const string &str) const |
|
const bool | check_valid (const std::vector< string > &attributes) const |
| Checks if an Entity is valid. More...
|
|
Base class for all entities.
- Since
- before 0.11.0
◆ Entity() [1/2]
Easy::Entity::Entity |
( |
const string & |
json | ) |
|
|
explicit |
Constructs an Entity object from a JSON string.
- Parameters
-
- Since
- before 0.11.0
30 : _tree(Json::nullValue)
const void from_string(const string &json)
Replaces the Entity with a new one from a JSON string.
Definition: entity.cpp:36
◆ Entity() [2/2]
Constructs an empty Entity object.
- Since
- before 0.11.0
◆ check_valid()
const bool Easy::Entity::check_valid |
( |
const std::vector< string > & |
attributes | ) |
const |
|
protected |
Checks if an Entity is valid.
- Parameters
-
attributes | The attributes to check |
- Returns
- true if all attributes are set
- Since
- 0.18.2
72 for (
const string &attribute: attributes)
const bool was_set() const
Returns true if the last requested value was set, false if it was unset.
Definition: entity.cpp:89
◆ error()
const string Easy::Entity::error |
( |
| ) |
const |
Returns error string sent by the server.
- Since
- before 0.11.0
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:135
◆ from_string()
const void Easy::Entity::from_string |
( |
const string & |
json | ) |
|
Replaces the Entity with a new one from a JSON string.
- Parameters
-
- Since
- before 0.11.0
38 std::stringstream ss(json);
43 if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1)
50 ttdebug <<
"ERROR: JSON string holds no object\n";
51 ttdebug <<
"String was: " << json <<
'\n';
53 else if (!_tree[
"error"].isNull())
55 ttdebug <<
"ERROR: Server returned an error\n";
56 ttdebug <<
"String was: " << json <<
'\n';
◆ get()
const Json::Value Easy::Entity::get |
( |
const string & |
key | ) |
const |
|
protected |
Returns the value of key as Json::Value.
Returns an empty object if the value does not exist or is null.
96 const Json::Value *node;
97 if (key.find(
'.') == std::string::npos)
105 string current_key = key;
107 while ((pos = current_key.find(
'.')) != std::string::npos)
111 node = &(*node)[current_key.substr(0, pos)];
112 current_key = current_key.substr(pos + 1);
114 catch (
const Json::LogicError &e)
116 ttdebug << e.what() <<
'\n';
120 node = &(*node)[current_key];
130 ttdebug <<
"Could not get data: " << key <<
'\n';
132 return Json::Value();
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:84
◆ get_bool()
const bool Easy::Entity::get_bool |
( |
const string & |
key | ) |
const |
|
protected |
Returns the value of key as bool.
Returns false if the value does not exist or is null.
179 const Json::Value node =
get(key);
184 return node.asBool();
◆ get_double()
const double Easy::Entity::get_double |
( |
const string & |
key | ) |
const |
|
protected |
Returns the value of key as double.
Returns 0.0 if the value does not exist or is null.
165 const Json::Value node =
get(key);
170 return node.asDouble();
◆ get_string()
const string Easy::Entity::get_string |
( |
const string & |
key | ) |
const |
|
protected |
Returns the value of key as std::string.
returns "" if the value does not exist or is null.
137 const Json::Value node =
get(key);
142 return node.asString();
◆ get_time_point()
const system_clock::time_point Easy::Entity::get_time_point |
( |
const string & |
key | ) |
const |
|
protected |
Returns the value of key as time_point.
Returns clocks epoch if the value does not exist or is null.
194 const Json::Value node =
get(key);
198 std::stringstream sstime(node.asString());
199 struct std::tm tm = {0};
200 sstime >> std::get_time(&tm,
"%Y-%m-%dT%T");
201 std::time_t time = timegm(&tm);
203 return system_clock::from_time_t(time);
208 return system_clock::time_point();
◆ get_uint64()
const uint_fast64_t Easy::Entity::get_uint64 |
( |
const string & |
key | ) |
const |
|
protected |
Returns the value of key as std::uint_fast64_t.
Returns 0 if the value does not exist or is null.
151 const Json::Value node =
get(key);
156 return node.asUInt64();
◆ get_vector()
const std::vector< string > Easy::Entity::get_vector |
( |
const string & |
key | ) |
const |
|
protected |
Returns the value of key as vector.
Returns an empty vector if the value does not exist or is null.
213 const Json::Value node =
get(key);
217 std::vector<string> vec;
218 for (
const Json::Value &value : node)
220 vec.push_back(value.asString());
◆ set()
const void Easy::Entity::set |
( |
const string & |
key, |
|
|
const Json::Value & |
value |
|
) |
| |
|
protected |
Sets the value of key.
- Since
- 0.17.0
232 if (key.find(
'.') == std::string::npos)
240 string current_key = key;
241 Json::Value *node = &_tree;
243 while ((pos = current_key.find(
'.')) != std::string::npos)
247 node = &(*node)[current_key.substr(0, pos)];
250 *node = Json::Value(Json::objectValue);
252 current_key = current_key.substr(pos + 1);
254 catch (
const Json::LogicError &e)
256 ttdebug << e.what() <<
'\n';
260 (*node)[current_key] = value;
265 ttdebug <<
"Could not set data: " << key <<
'\n';
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:84
◆ to_object()
const Json::Value Easy::Entity::to_object |
( |
| ) |
const |
Returns the JSON object of the Entity.
- Returns
- JSON object
- Since
- before 0.11.0
◆ valid()
virtual const bool Mastodon::Easy::Entity::valid |
( |
| ) |
const |
|
pure virtual |
Returns true if the Entity holds valid data.
- Since
- before 0.11.0 (virtual since 0.18.2)
Implemented in Mastodon::Easy::Account::Source, Mastodon::Easy::Tag, Mastodon::Easy::Status, Mastodon::Easy::Account, Mastodon::Easy::Tag::History, Mastodon::Easy::Notification, Mastodon::Easy::Results, Mastodon::Easy::Attachment, Mastodon::Easy::Context, Mastodon::Easy::Instance, Mastodon::Easy::List, Mastodon::Easy::Mention, Mastodon::Easy::PushSubscription, Mastodon::Easy::Card, Mastodon::Easy::Relationship, Mastodon::Easy::Report, Mastodon::Easy::Application, and Mastodon::Easy::Emoji.
◆ was_set()
const bool Easy::Entity::was_set |
( |
| ) |
const |
Returns true if the last requested value was set, false if it was unset.
Members of Easy::Entity-derived classes return a default value depending on its type when the requested value is not found in the JSON. "" for strings, false for bools and so on. Most of the time this is no problem, but sometimes you need to know for sure.
Example:
Easy::Account a(jsonstring);
if (a.note().empty())
{
if (a.was_set())
{
cout << "Account has an empty description.\n";
}
else
{
cout << "Account has no description.\n";
}
}
- Since
- before 0.11.0
The documentation for this class was generated from the following files: