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

Base class for all entities. More...

#include <entity.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::Filter 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 Mastodon::Easy::Token

Public Member Functions

 Entity (const string &json)
 Constructs an Entity object from a JSON string. More...
 
 Entity (const Json::Value &object)
 Constructs an Entity object from a JSON object. More...
 
 Entity ()
 Constructs an empty Entity object. More...
 
virtual ~Entity ()
 Destroys the object. More...
 
 operator const Json::Value () const
 
void from_string (const string &json)
 Replaces the Entity with a new one from a JSON string. More...
 
const string to_string () const
 Returns the JSON of the Entity as string. More...
 
void from_object (const Json::Value &object)
 Replaces the Entity with a new one from a JSON object. 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...
 
uint64_t get_uint64 (const string &key) const
 Returns the value of key as std::uint64_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 Easy::time get_time (const string &key) const
 Returns the value of key as Easy::time. 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::uint64_t stouint64 (const string &str) const
 Returns value of str as uint64_t.
 
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/3]

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

Constructs an Entity object from a JSON string.

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

◆ Entity() [2/3]

Easy::Entity::Entity ( const Json::Value &  object)
explicit

Constructs an Entity object from a JSON object.

Parameters
objectJSON object
Since
0.100.0
38 : _tree(object)
39 ,_was_set(false)
40 {}

◆ Entity() [3/3]

Easy::Entity::Entity ( )

Constructs an empty Entity object.

Since
before 0.11.0
43 : _tree(Json::nullValue)
44 , _was_set(false)
45 {}

◆ ~Entity()

Easy::Entity::~Entity ( )
virtual

Destroys the object.

Since
0.100.0
48 {}

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
102 {
103  for (const string &attribute: attributes)
104  {
105  get(attribute);
106  if (!was_set())
107  {
108  return false;
109  }
110  }
111 
112  return true;
113 }
bool was_set() const
Returns true if the last requested value was set, false if it was unset.
Definition: entity.cpp:127

◆ error()

const string Easy::Entity::error ( ) const

Returns error string sent by the server.

Since
before 0.11.0
116 {
117  string error = get_string("error");
118  if (error.empty())
119  {
120  // Pleroma uses {"errors":{"detail":"[…]"}} sometimes.
121  const Json::Value node = get("errors.detail");
122  error = node.asString();
123  }
124  return error;
125 }
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:115
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:173

◆ from_object()

void Easy::Entity::from_object ( const Json::Value &  object)

Replaces the Entity with a new one from a JSON object.

Parameters
objectJSON object
Since
0.100.0
92 {
93  _tree = object;
94 }

◆ 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
56 {
57  if (json.find('{') != std::string::npos)
58  {
59  std::stringstream ss(json);
60  ss >> _tree;
61  }
62  else
63  {
64  _tree.clear();
65  }
66 
67  // If the JSON is a single object encapsulated in an array,
68  // transform it into an object. If the JSON string is [], transform to null
69  if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1)
70  {
71  _tree = _tree[0];
72  }
73 
74  if (_tree.isNull())
75  {
76  ttdebug << "ERROR: JSON string holds no object\n";
77  ttdebug << "String was: " << json << '\n';
78  }
79  else if (!_tree["error"].isNull() || !_tree["errors"].isNull())
80  {
81  ttdebug << "ERROR: Server returned an error\n";
82  ttdebug << "String was: " << json << '\n';
83  }
84 }

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

133 {
134  const Json::Value *node;
135  if (key.find('.') == std::string::npos)
136  {
137  node = &_tree[key];
138  }
139  else
140  {
141  // If dots in key, we have to walk through the tree
142  std::size_t pos = 0;
143  string current_key = key;
144  node = &_tree;
145  while ((pos = current_key.find('.')) != std::string::npos)
146  {
147  try
148  {
149  node = &(*node)[current_key.substr(0, pos)];
150  current_key = current_key.substr(pos + 1);
151  }
152  catch (const Json::LogicError &e)
153  {
154  ttdebug << e.what() << '\n';
155  goto error;
156  }
157  }
158  node = &(*node)[current_key];
159  }
160 
161  if (!node->isNull())
162  {
163  _was_set = true;
164  return *node;
165  }
166 
167  error:
168  ttdebug << "Could not get data: " << key << '\n';
169  _was_set = false;
170  return Json::Value();
171 }
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:115

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

216 {
217  const Json::Value node = get(key);
218 
219  if (node.isBool())
220  {
221  _was_set = true;
222  return node.asBool();
223  }
224 
225  _was_set = false;
226  return false;
227 }

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

202 {
203  const Json::Value node = get(key);
204 
205  if (node.isDouble())
206  {
207  _was_set = true;
208  return node.asDouble();
209  }
210 
211  _was_set = false;
212  return 0.0;
213 }

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

174 {
175  const Json::Value node = get(key);
176 
177  if (node.isString())
178  {
179  _was_set = true;
180  return node.asString();
181  }
182 
183  _was_set = false;
184  return "";
185 }

◆ get_time()

const Easy::time Easy::Entity::get_time ( const string &  key) const
protected

Returns the value of key as Easy::time.

Returns clocks epoch if the value does not exist or is null.

230 {
231  const Json::Value node = get(key);
232 
233  if (node.isString())
234  {
235  std::stringstream sstime(node.asString());
236  struct std::tm tm = {};
237  tm.tm_isdst = -1; // Detect daylight saving time.
238  sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
239  std::time_t time = timegm(&tm); // Assume time is UTC.
240  _was_set = true;
241  return { system_clock::from_time_t(time) };
242  }
243 
244  _was_set = false;
245  // Return clocks epoch
246  return { system_clock::time_point() };
247 }

◆ get_uint64()

uint64_t Easy::Entity::get_uint64 ( const string &  key) const
protected

Returns the value of key as std::uint64_t.

Returns 0 if the value does not exist or is null.

188 {
189  const Json::Value node = get(key);
190 
191  if (node.isUInt64())
192  {
193  _was_set = true;
194  return node.asUInt64();
195  }
196 
197  _was_set = false;
198  return 0;
199 }

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

250 {
251  const Json::Value node = get(key);
252 
253  if (node.isArray())
254  {
255  std::vector<string> vec;
256  std::transform(node.begin(), node.end(), std::back_inserter(vec),
257  [](const Json::Value &value)
258  { return value.asString(); });
259  _was_set = true;
260  return vec;
261  }
262 
263  _was_set = false;
264  return {};
265 }

◆ operator const Json::Value()

Easy::Entity::operator const Json::Value ( ) const

Returns the JSON object of the Entity

Since
0.100.0
51 {
52  return to_object();
53 }
const Json::Value to_object() const
Returns the JSON object of the Entity.
Definition: entity.cpp:96

◆ set()

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

Sets the value of key.

Since
0.17.0
268 {
269  if (key.find('.') == std::string::npos)
270  {
271  _tree[key] = value;
272  return;
273  }
274  else
275  {
276  std::size_t pos = 0;
277  string current_key = key;
278  Json::Value *node = &_tree;
279 
280  while ((pos = current_key.find('.')) != std::string::npos)
281  {
282  try
283  {
284  node = &(*node)[current_key.substr(0, pos)];
285  if (node->isNull())
286  {
287  *node = Json::Value(Json::objectValue);
288  }
289  current_key = current_key.substr(pos + 1);
290  }
291  catch (const Json::LogicError &e)
292  {
293  ttdebug << e.what() << '\n';
294  goto error;
295  }
296  }
297  (*node)[current_key] = value;
298  return;
299  }
300 
301  error:
302  ttdebug << "Could not set data: " << key << '\n';
303 }
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:115

◆ 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
97 {
98  return _tree;
99 }

◆ to_string()

const string Easy::Entity::to_string ( ) const

Returns the JSON of the Entity as string.

Returns
JSON string
Since
before 0.11.0
87 {
88  return _tree.toStyledString();
89 }

◆ 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
128 {
129  return _was_set;
130 }

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