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...
|
|
uint_fast64_t | get_uint64 (const string &key) const |
| Returns the value of key as std::uint_fast64_t. More...
|
|
double | get_double (const string &key) const |
| Returns the value of key as double. More...
|
|
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...
|
|
void | set (const string &key, const Json::Value &value) |
| Sets the value of key. More...
|
|
std::uint_fast64_t | stouint64 (const string &str) 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)
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()
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
71 for (
const string &attribute: attributes)
bool was_set() const
Returns true if the last requested value was set, false if it was unset.
Definition: entity.cpp:88
◆ 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:134
◆ from_string()
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.
95 const Json::Value *node;
96 if (key.find(
'.') == std::string::npos)
104 string current_key = key;
106 while ((pos = current_key.find(
'.')) != std::string::npos)
110 node = &(*node)[current_key.substr(0, pos)];
111 current_key = current_key.substr(pos + 1);
113 catch (
const Json::LogicError &e)
115 ttdebug << e.what() <<
'\n';
119 node = &(*node)[current_key];
129 ttdebug <<
"Could not get data: " << key <<
'\n';
131 return Json::Value();
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:83
◆ get_bool()
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.
178 const Json::Value node =
get(key);
183 return node.asBool();
◆ get_double()
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.
164 const Json::Value node =
get(key);
169 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.
136 const Json::Value node =
get(key);
141 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.
193 const Json::Value node =
get(key);
197 std::stringstream sstime(node.asString());
199 sstime >> std::get_time(&tm,
"%Y-%m-%dT%T");
200 std::time_t time = timegm(&tm);
202 return system_clock::from_time_t(time);
207 return system_clock::time_point();
◆ get_uint64()
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.
150 const Json::Value node =
get(key);
155 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.
212 const Json::Value node =
get(key);
216 std::vector<string> vec;
217 for (
const Json::Value &value : node)
219 vec.push_back(value.asString());
◆ set()
void Easy::Entity::set |
( |
const string & |
key, |
|
|
const Json::Value & |
value |
|
) |
| |
|
protected |
Sets the value of key.
- Since
- 0.17.0
231 if (key.find(
'.') == std::string::npos)
239 string current_key = key;
240 Json::Value *node = &_tree;
242 while ((pos = current_key.find(
'.')) != std::string::npos)
246 node = &(*node)[current_key.substr(0, pos)];
249 *node = Json::Value(Json::objectValue);
251 current_key = current_key.substr(pos + 1);
253 catch (
const Json::LogicError &e)
255 ttdebug << e.what() <<
'\n';
259 (*node)[current_key] = value;
264 ttdebug <<
"Could not set data: " << key <<
'\n';
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:83
◆ 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 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::Instance, Mastodon::Easy::Attachment, Mastodon::Easy::Context, 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()
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: