21 #ifndef _ENABLE_QT_EXPERIMENTAL_
22 #error You should define _ENABLE_QT_EXPERIMENTAL_ in order to use this experimental header file.
25 #include <unity/scopes/qt/HttpAsyncReader.h>
26 #include <unity/util/DefinesPtrs.h>
27 #include <unity/util/NonCopyable.h>
29 #include <QtCore/QJsonArray>
30 #include <QtCore/QJsonDocument>
31 #include <QtCore/QJsonObject>
32 #include <QtCore/QStringList>
33 #include <QtCore/QVector>
48 template <
typename TYPE>
49 using ResultsList = std::deque<std::shared_ptr<TYPE>>;
51 template <
typename B,
typename T>
52 static bool get_results_json(QJsonDocument& json,
53 std::string
const& object_name,
54 ResultsList<B>& results,
79 std::function<bool(QJsonDocument& root, const std::string&, std::deque<std::shared_ptr<B>>&, std::string&)>;
85 using ResultsFuture = std::future<std::deque<std::shared_ptr<T>>>;
90 template <
typename TYPE>
91 using ResultsList = std::deque<std::shared_ptr<TYPE>>;
96 typedef std::future<std::shared_ptr<QJsonDocument>> JsonDocumentFuture;
101 typedef std::shared_ptr<QJsonDocument> QJsonDocumentSptr;
106 typedef std::vector<std::pair<std::string, std::string>> JsonParameters;
134 template <
typename BASE,
typename TYPE>
135 ResultsFuture<BASE> async_get(std::string
const& uri,
136 std::string
const& object_name,
137 ParseFunc<BASE>
const& parse = get_results_json<BASE, TYPE>)
const;
156 template <
typename T>
157 ResultsFuture<T> async_get(std::string
const& uri,
158 std::string
const& object_name,
159 ParseFunc<T>
const& parse = get_results_json<T, T>)
const;
184 template <
typename BASE,
typename TYPE>
185 ResultsFuture<BASE> async_get(std::string
const& host,
186 JsonParameters
const& params,
187 std::string
const& object_name,
188 ParseFunc<BASE>
const& parse = get_results_json<BASE, TYPE>)
const;
211 template <
typename T>
212 ResultsFuture<T> async_get(std::string
const& host,
213 JsonParameters
const& params,
214 std::string
const& object_name,
215 ParseFunc<T>
const& parse = get_results_json<T, T>)
const;
227 JsonDocumentFuture async_get_parser(std::string
const& uri)
const;
240 JsonDocumentFuture async_get_parser(std::string
const& host, JsonParameters
const& params)
const;
251 static QJsonDocumentSptr create_parser_with_data(std::string
const& data, std::string& error);
254 std::shared_ptr<HttpAsyncReader> p_;
259 template <
typename BASE,
typename TYPE>
260 JsonAsyncReader::ResultsFuture<BASE> JsonAsyncReader::async_get(std::string
const& uri,
261 std::string
const& object_name,
262 ParseFunc<BASE>
const& parse)
const
264 return p_->async_get<BASE, TYPE, QJsonDocument>(uri, object_name, JsonAsyncReader::create_parser_with_data, parse);
267 template <
typename T>
268 JsonAsyncReader::ResultsFuture<T> JsonAsyncReader::async_get(std::string
const& uri,
269 std::string
const& object_name,
270 ParseFunc<T>
const& parse)
const
272 return p_->async_get<T, T, QJsonDocument>(uri, object_name, JsonAsyncReader::create_parser_with_data, parse);
275 template <
typename BASE,
typename TYPE>
276 JsonAsyncReader::ResultsFuture<BASE> JsonAsyncReader::async_get(std::string
const& host,
277 JsonParameters
const& params,
278 std::string
const& object_name,
279 ParseFunc<BASE>
const& parse)
const
281 std::string uri = p_->get_uri(host, params);
282 return p_->async_get<BASE, TYPE, QJsonDocument>(uri, object_name, JsonAsyncReader::create_parser_with_data, parse);
285 template <
typename T>
286 JsonAsyncReader::ResultsFuture<T> JsonAsyncReader::async_get(std::string
const& host,
287 JsonParameters
const& params,
288 std::string
const& object_name,
289 ParseFunc<T>
const& parse)
const
291 std::string uri = p_->get_uri(host, params);
292 return p_->async_get<T, T, QJsonDocument>(uri, object_name, JsonAsyncReader::create_parser_with_data, parse);
295 template <
typename B,
typename T>
296 static bool get_results_json_object(QJsonObject& root,
297 const std::string& object_name,
298 JsonAsyncReader::ResultsList<B>& results,
299 std::string& error_string)
301 QString qobject_name(object_name.c_str());
302 if (root.contains(qobject_name))
304 if (root[qobject_name].isArray())
306 auto sub_root = root[qobject_name];
307 QJsonArray levelArray = sub_root.toArray();
308 for (
int levelIndex = 0; levelIndex < levelArray.size(); ++levelIndex)
310 QJsonObject levelObject = levelArray[levelIndex].toObject();
311 results.emplace_back(std::make_shared<T>(levelObject));
314 else if (root[qobject_name].isObject())
316 auto sub_root = root[qobject_name].toObject();
317 results.emplace_back(std::make_shared<T>(sub_root));
323 for (
auto member : root.keys())
325 auto item = root[member];
328 QJsonObject obj = item.toObject();
329 get_results_json_object<B, T>(obj, object_name, results, error_string);
336 template <
typename B,
typename T>
337 static bool get_results_json(QJsonDocument& doc,
338 const std::string& object_name,
339 JsonAsyncReader::ResultsList<B>& results,
340 std::string& error_string)
344 QJsonArray levelArray = doc.array();
345 for (
int levelIndex = 0; levelIndex < levelArray.size(); ++levelIndex)
347 QJsonObject levelObject = levelArray[levelIndex].toObject();
348 get_results_json_object<B, T>(levelObject, object_name, results, error_string);
351 else if (doc.isObject())
353 QJsonObject levelObject = doc.object();
354 get_results_json_object<B, T>(levelObject, object_name, results, error_string);
Top-level namespace for all things Unity-related.
Definition: Version.h:49
Class that downloads http JSON files asynchronously.
Definition: JsonAsyncReader.h:67