mastodon-cpp  0.18.8
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...
 
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...
 
virtual const 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...
 
const 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...
 
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...
 

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 }
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]

Easy::Entity::Entity ( )

Constructs an empty Entity object.

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

Member Function Documentation

◆ check_valid()

const 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
71 {
72  for (const string &attribute: attributes)
73  {
74  get(attribute);
75  if (!was_set())
76  {
77  return false;
78  }
79  }
80 
81  return true;
82 }
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
85 {
86  return get_string("error");
87 }
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
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.

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

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

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

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

◆ 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 {
137  const Json::Value node = get(key);
138 
139  if (node.isString())
140  {
141  _was_set = true;
142  return node.asString();
143  }
144 
145  _was_set = false;
146  return "";
147 }

◆ 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 {
194  const Json::Value node = get(key);
195 
196  if (node.isString())
197  {
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);
202  _was_set = true;
203  return system_clock::from_time_t(time);
204  }
205 
206  _was_set = false;
207  // Return clocks epoch
208  return system_clock::time_point();
209 }

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

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

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

◆ set()

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

Sets the value of key.

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

◆ valid()

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

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

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