mastodon-cpp  0.30.0
Public Member Functions | Protected Member Functions | List of all members
Mastodon::Easy::Entity Class Referenceabstract

Base class for all entities. More...

#include <easy.hpp>

Inheritance diagram for Mastodon::Easy::Entity:
Mastodon::Easy::Account Mastodon::Easy::Account::Source Mastodon::Easy::Application Mastodon::Easy::Attachment Mastodon::Easy::Card Mastodon::Easy::Context Mastodon::Easy::Emoji Mastodon::Easy::Instance Mastodon::Easy::List Mastodon::Easy::Mention Mastodon::Easy::Notification Mastodon::Easy::PushSubscription Mastodon::Easy::Relationship Mastodon::Easy::Report Mastodon::Easy::Results Mastodon::Easy::Status Mastodon::Easy::Tag Mastodon::Easy::Tag::History

Public Member Functions

 Entity (const string &json)
 Constructs an Entity object from a JSON string. More...
 
 Entity ()
 Constructs an empty Entity object. More...
 
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...
 
virtual bool valid () const =0
 Returns true if the Entity holds valid data. More...
 
const string error () const
 Returns error string sent by the server. More...
 
bool was_set () const
 Returns true if the last requested value was set, false if it was unset. More...
 

Protected Member Functions

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...
 

Detailed Description

Base class for all entities.

Since
before 0.11.0

Constructor & Destructor Documentation

◆ Entity() [1/2]

Easy::Entity::Entity ( const string &  json)
explicit

Constructs an Entity object from a JSON string.

Parameters
jsonJSON string
Since
before 0.11.0
30 : _tree(Json::nullValue)
31 ,_was_set(false)
32 {
33  from_string(json);
34 }
void from_string(const string &json)
Replaces the Entity with a new one from a JSON string.
Definition: entity.cpp:36

◆ Entity() [2/2]

Easy::Entity::Entity ( )

Constructs an empty Entity object.

Since
before 0.11.0
66 : _was_set(false)
67 {}

Member Function Documentation

◆ check_valid()

bool Easy::Entity::check_valid ( const std::vector< string > &  attributes) const
protected

Checks if an Entity is valid.

Parameters
attributesThe attributes to check
Returns
true if all attributes are set
Since
0.18.2
70 {
71  for (const string &attribute: attributes)
72  {
73  get(attribute);
74  if (!was_set())
75  {
76  return false;
77  }
78  }
79 
80  return true;
81 }
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
84 {
85  return get_string("error");
86 }
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
jsonJSON string
Since
before 0.11.0
37 {
38  std::stringstream ss(json);
39  ss >> _tree;
40 
41  // If the JSON is a single object encapsulated in an array,
42  // transform it into an object. If the JSON string is [], transform to null
43  if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1)
44  {
45  _tree = _tree[0];
46  }
47 
48  if (_tree.isNull())
49  {
50  ttdebug << "ERROR: JSON string holds no object\n";
51  ttdebug << "String was: " << json << '\n';
52  }
53  else if (!_tree["error"].isNull())
54  {
55  ttdebug << "ERROR: Server returned an error\n";
56  ttdebug << "String was: " << json << '\n';
57  }
58 }

◆ 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.

94 {
95  const Json::Value *node;
96  if (key.find('.') == std::string::npos)
97  {
98  node = &_tree[key];
99  }
100  else
101  {
102  // If dots in key, we have to walk through the tree
103  std::size_t pos = 0;
104  string current_key = key;
105  node = &_tree;
106  while ((pos = current_key.find('.')) != std::string::npos)
107  {
108  try
109  {
110  node = &(*node)[current_key.substr(0, pos)];
111  current_key = current_key.substr(pos + 1);
112  }
113  catch (const Json::LogicError &e)
114  {
115  ttdebug << e.what() << '\n';
116  goto error;
117  }
118  }
119  node = &(*node)[current_key];
120  }
121 
122  if (!node->isNull())
123  {
124  _was_set = true;
125  return *node;
126  }
127 
128  error:
129  ttdebug << "Could not get data: " << key << '\n';
130  _was_set = false;
131  return Json::Value();
132 }
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.

177 {
178  const Json::Value node = get(key);
179 
180  if (node.isBool())
181  {
182  _was_set = true;
183  return node.asBool();
184  }
185 
186  _was_set = false;
187  return false;
188 }

◆ 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.

163 {
164  const Json::Value node = get(key);
165 
166  if (node.isDouble())
167  {
168  _was_set = true;
169  return node.asDouble();
170  }
171 
172  _was_set = false;
173  return 0.0;
174 }

◆ 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.

135 {
136  const Json::Value node = get(key);
137 
138  if (node.isString())
139  {
140  _was_set = true;
141  return node.asString();
142  }
143 
144  _was_set = false;
145  return "";
146 }

◆ 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.

192 {
193  const Json::Value node = get(key);
194 
195  if (node.isString())
196  {
197  std::stringstream sstime(node.asString());
198  struct std::tm tm;
199  sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
200  std::time_t time = timegm(&tm);
201  _was_set = true;
202  return system_clock::from_time_t(time);
203  }
204 
205  _was_set = false;
206  // Return clocks epoch
207  return system_clock::time_point();
208 }

◆ 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.

149 {
150  const Json::Value node = get(key);
151 
152  if (node.isUInt64())
153  {
154  _was_set = true;
155  return node.asUInt64();
156  }
157 
158  _was_set = false;
159  return 0;
160 }

◆ 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.

211 {
212  const Json::Value node = get(key);
213 
214  if (node.isArray())
215  {
216  std::vector<string> vec;
217  for (const Json::Value &value : node)
218  {
219  vec.push_back(value.asString());
220  }
221  _was_set = true;
222  return vec;
223  }
224 
225  _was_set = false;
226  return {};
227 }

◆ set()

void Easy::Entity::set ( const string &  key,
const Json::Value &  value 
)
protected

Sets the value of key.

Since
0.17.0
230 {
231  if (key.find('.') == std::string::npos)
232  {
233  _tree[key] = value;
234  return;
235  }
236  else
237  {
238  std::size_t pos = 0;
239  string current_key = key;
240  Json::Value *node = &_tree;
241 
242  while ((pos = current_key.find('.')) != std::string::npos)
243  {
244  try
245  {
246  node = &(*node)[current_key.substr(0, pos)];
247  if (node->isNull())
248  {
249  *node = Json::Value(Json::objectValue);
250  }
251  current_key = current_key.substr(pos + 1);
252  }
253  catch (const Json::LogicError &e)
254  {
255  ttdebug << e.what() << '\n';
256  goto error;
257  }
258  }
259  (*node)[current_key] = value;
260  return;
261  }
262 
263  error:
264  ttdebug << "Could not set data: " << key << '\n';
265 }
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
61 {
62  return _tree;
63 }

◆ valid()

virtual bool Mastodon::Easy::Entity::valid ( ) const
pure virtual

◆ 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
89 {
90  return _was_set;
91 }

The documentation for this class was generated from the following files: