mastodon-cpp  0.110.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::Attachment::Meta Mastodon::Easy::Card Mastodon::Easy::Context Mastodon::Easy::Conversation Mastodon::Easy::Emoji Mastodon::Easy::Filter Mastodon::Easy::Instance Mastodon::Easy::List Mastodon::Easy::Mention Mastodon::Easy::Notification Mastodon::Easy::Poll Mastodon::Easy::PushSubscription Mastodon::Easy::Relationship 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 formatted 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_type 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
32 : _tree(Json::nullValue)
33 ,_was_set(false)
34 {
35  from_string(json);
36 }
void from_string(const string &json)
Replaces the Entity with a new one from a JSON string.
Definition: entity.cpp:56

◆ 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
39 : _tree(object)
40 ,_was_set(false)
41 {}

◆ Entity() [3/3]

Easy::Entity::Entity ( )

Constructs an empty Entity object.

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

◆ ~Entity()

Easy::Entity::~Entity ( )
virtual

Destroys the object.

Since
0.100.0
49 {}

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

◆ error()

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

Returns error string sent by the server.

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

◆ 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
93 {
94  _tree = object;
95 }

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

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

◆ 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.
217 {
218  const Json::Value node = get(key);
219 
220  if (node.isBool())
221  {
222  _was_set = true;
223  return node.asBool();
224  }
225 
226  _was_set = false;
227  return false;
228 }
const Json::Value get(const string &key) const
Returns the value of key as Json::Value.
Definition: entity.cpp:133

◆ 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.
203 {
204  const Json::Value node = get(key);
205 
206  if (node.isDouble())
207  {
208  _was_set = true;
209  return node.asDouble();
210  }
211 
212  _was_set = false;
213  return 0.0;
214 }
const Json::Value get(const string &key) const
Returns the value of key as Json::Value.
Definition: entity.cpp:133

◆ 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.
175 {
176  const Json::Value node = get(key);
177 
178  if (node.isString())
179  {
180  _was_set = true;
181  return node.asString();
182  }
183 
184  _was_set = false;
185  return "";
186 }
const Json::Value get(const string &key) const
Returns the value of key as Json::Value.
Definition: entity.cpp:133

◆ get_time()

const Easy::time_type 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.
231 {
232  const Json::Value node = get(key);
233 
234  if (node.isString())
235  {
236  _was_set = true;
237  return Easy::string_to_time(node.asString());
238  }
239 
240  _was_set = false;
241  // Return clocks epoch
242  return { system_clock::time_point() };
243 }
const Json::Value get(const string &key) const
Returns the value of key as Json::Value.
Definition: entity.cpp:133
const Easy::time_type string_to_time(const string &strtime)
Convert ISO 8601 time string to Easy::time.
Definition: easy.cpp:85

◆ 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.
189 {
190  const Json::Value node = get(key);
191 
192  if (node.isUInt64())
193  {
194  _was_set = true;
195  return node.asUInt64();
196  }
197 
198  _was_set = false;
199  return 0;
200 }
const Json::Value get(const string &key) const
Returns the value of key as Json::Value.
Definition: entity.cpp:133

◆ 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.
246 {
247  const Json::Value node = get(key);
248 
249  if (node.isArray())
250  {
251  std::vector<string> vec;
252  std::transform(node.begin(), node.end(), std::back_inserter(vec),
253  [](const Json::Value &value)
254  { return value.asString(); });
255  _was_set = true;
256  return vec;
257  }
258 
259  _was_set = false;
260  return {};
261 }
const Json::Value get(const string &key) const
Returns the value of key as Json::Value.
Definition: entity.cpp:133

◆ operator const Json::Value()

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

Returns the JSON object of the Entity

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

◆ set()

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

Sets the value of key.

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

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

◆ to_string()

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

Returns the JSON of the Entity as formatted string.

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

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

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