Base class for all entities.
More...
#include <easy.hpp>
|
| Entity (const string &json) |
| Constructs an Entity object from a JSON string. More...
|
|
| Entity () |
| Constructs an empty Entity object.
|
|
const void | from_string (const string &json) |
| Replaces the Entity with a new one from a JSON string. More...
|
|
const Json::Value | to_object () const |
| Returns the JSON object of the Entity. More...
|
|
const bool | valid () const |
| Returns true if the Entity holds valid data.
|
|
const string | error () const |
| Returns error string sent by the server.
|
|
const bool | was_set () const |
| Returns true if the last requested value was set, false if it was unset. More...
|
|
|
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 std::uint_fast64_t | stouint64 (const string &str) const |
|
Base class for all entities.
◆ Entity()
Easy::Entity::Entity |
( |
const string & |
json | ) |
|
|
explicit |
Constructs an Entity object from a JSON string.
- Parameters
-
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:37
◆ from_string()
const void Easy::Entity::from_string |
( |
const string & |
json | ) |
|
Replaces the Entity with a new one from a JSON string.
- Parameters
-
39 std::stringstream ss(json);
44 if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1)
51 ttdebug <<
"ERROR: JSON string holds no object\n";
52 ttdebug <<
"String was: " << json <<
'\n';
54 else if (!_tree[
"error"].isNull())
56 ttdebug <<
"ERROR: Server returned an error\n";
57 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.
92 const Json::Value *node;
93 if (key.find(
'.') == std::string::npos)
101 string current_key = key;
103 while ((pos = current_key.find(
'.')) != std::string::npos)
107 node = &(*node)[current_key.substr(0, pos)];
108 current_key = current_key.substr(pos + 1);
110 catch (
const Json::LogicError &e)
112 ttdebug << e.what() <<
'\n';
116 node = &(*node)[current_key];
126 ttdebug <<
"Could not get data: " << key <<
'\n';
128 return Json::Value();
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:80
◆ 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.
175 const Json::Value node =
get(key);
180 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.
161 const Json::Value node =
get(key);
166 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.
133 const Json::Value node =
get(key);
138 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.
190 const Json::Value node =
get(key);
194 std::stringstream sstime(node.asString());
195 struct std::tm tm = {0};
196 sstime >> std::get_time(&tm,
"%Y-%m-%dT%T");
197 std::time_t time = timegm(&tm);
199 return system_clock::from_time_t(time);
204 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.
147 const Json::Value node =
get(key);
152 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.
209 const Json::Value node =
get(key);
213 std::vector<string> vec;
214 for (
const Json::Value &value : node)
216 vec.push_back(value.asString());
◆ to_object()
const Json::Value Easy::Entity::to_object |
( |
| ) |
const |
Returns the JSON object of the Entity.
- Returns
- JSON object
◆ 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";
}
}
The documentation for this class was generated from the following files: