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

Base class for all entities. More...

#include <easy.hpp>

Inheritance diagram for Mastodon::Easy::Entity:
Mastodon::Easy::Account 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.
 
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...
 

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
 

Detailed Description

Base class for all entities.

Constructor & Destructor Documentation

◆ Entity()

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

Constructs an Entity object from a JSON string.

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

Member Function Documentation

◆ 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
38 {
39  std::stringstream ss(json);
40  ss >> _tree;
41 
42  // If the JSON is a single object encapsulated in an array,
43  // transform it into an object. If the JSON string is [], transform to null
44  if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1)
45  {
46  _tree = _tree[0];
47  }
48 
49  if (_tree.isNull())
50  {
51  ttdebug << "ERROR: JSON string holds no object\n";
52  ttdebug << "String was: " << json << '\n';
53  }
54  else if (!_tree["error"].isNull())
55  {
56  ttdebug << "ERROR: Server returned an error\n";
57  ttdebug << "String was: " << json << '\n';
58  }
59  else
60  {
61  _valid = true;
62  }
63 }

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

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

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

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

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

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

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

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

189 {
190  const Json::Value node = get(key);
191 
192  if (node.isString())
193  {
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);
198  _was_set = true;
199  return system_clock::from_time_t(time);
200  }
201 
202  _was_set = false;
203  // Return clocks epoch
204  return system_clock::time_point();
205 }

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

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

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

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

◆ set()

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

Sets the value of key.

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

◆ to_object()

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

Returns the JSON object of the Entity.

Returns
JSON object
66 {
67  return _tree;
68 }

◆ 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";
}
}
86 {
87  return _was_set;
88 }

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