20 #ifndef _ENABLE_QT_EXPERIMENTAL_
21 #error You should define _ENABLE_QT_EXPERIMENTAL_ in order to use this experimental header file.
24 #include <unity/scopes/qt/HttpAsyncReader.h>
25 #include <unity/util/DefinesPtrs.h>
26 #include <unity/util/NonCopyable.h>
28 #include <QtCore/QJsonArray>
29 #include <QtCore/QJsonDocument>
30 #include <QtCore/QJsonObject>
31 #include <QtCore/QStringList>
32 #include <QtCore/QVector>
47 template <
typename TYPE>
48 using ResultsList = std::deque<std::shared_ptr<TYPE>>;
50 template <
typename B,
typename T>
51 static bool get_results_json(QJsonDocument& json,
52 std::string
const& object_name,
53 ResultsList<B>& results,
78 std::function<bool(QJsonDocument& root, const std::string&, std::deque<std::shared_ptr<B>>&, std::string&)>;
84 using ResultsFuture = std::future<std::deque<std::shared_ptr<T>>>;
89 template <
typename TYPE>
90 using ResultsList = std::deque<std::shared_ptr<TYPE>>;
95 typedef std::future<std::shared_ptr<QJsonDocument>> JsonDocumentFuture;
100 typedef std::shared_ptr<QJsonDocument> QJsonDocumentSptr;
105 typedef std::vector<std::pair<std::string, std::string>> JsonParameters;
133 template <
typename BASE,
typename TYPE>
134 ResultsFuture<BASE> async_get(std::string
const& uri,
135 std::string
const& object_name,
136 ParseFunc<BASE>
const& parse = get_results_json<BASE, TYPE>)
const;
155 template <
typename T>
156 ResultsFuture<T> async_get(std::string
const& uri,
157 std::string
const& object_name,
158 ParseFunc<T>
const& parse = get_results_json<T, T>)
const;
183 template <
typename BASE,
typename TYPE>
184 ResultsFuture<BASE> async_get(std::string
const& host,
185 JsonParameters
const& params,
186 std::string
const& object_name,
187 ParseFunc<BASE>
const& parse = get_results_json<BASE, TYPE>)
const;
210 template <
typename T>
211 ResultsFuture<T> async_get(std::string
const& host,
212 JsonParameters
const& params,
213 std::string
const& object_name,
214 ParseFunc<T>
const& parse = get_results_json<T, T>)
const;
226 JsonDocumentFuture async_get_parser(std::string
const& uri)
const;
239 JsonDocumentFuture async_get_parser(std::string
const& host, JsonParameters
const& params)
const;
250 static QJsonDocumentSptr create_parser_with_data(std::string
const& data, std::string& error);
253 std::shared_ptr<HttpAsyncReader> p_;
258 template <
typename BASE,
typename TYPE>
259 JsonAsyncReader::ResultsFuture<BASE> JsonAsyncReader::async_get(std::string
const& uri,
260 std::string
const& object_name,
261 ParseFunc<BASE>
const& parse)
const
263 return p_->async_get<BASE, TYPE, QJsonDocument>(uri, object_name, JsonAsyncReader::create_parser_with_data, parse);
266 template <
typename T>
267 JsonAsyncReader::ResultsFuture<T> JsonAsyncReader::async_get(std::string
const& uri,
268 std::string
const& object_name,
269 ParseFunc<T>
const& parse)
const
271 return p_->async_get<T, T, QJsonDocument>(uri, object_name, JsonAsyncReader::create_parser_with_data, parse);
274 template <
typename BASE,
typename TYPE>
275 JsonAsyncReader::ResultsFuture<BASE> JsonAsyncReader::async_get(std::string
const& host,
276 JsonParameters
const& params,
277 std::string
const& object_name,
278 ParseFunc<BASE>
const& parse)
const
280 std::string uri = p_->get_uri(host, params);
281 return p_->async_get<BASE, TYPE, QJsonDocument>(uri, object_name, JsonAsyncReader::create_parser_with_data, parse);
284 template <
typename T>
285 JsonAsyncReader::ResultsFuture<T> JsonAsyncReader::async_get(std::string
const& host,
286 JsonParameters
const& params,
287 std::string
const& object_name,
288 ParseFunc<T>
const& parse)
const
290 std::string uri = p_->get_uri(host, params);
291 return p_->async_get<T, T, QJsonDocument>(uri, object_name, JsonAsyncReader::create_parser_with_data, parse);
294 template <
typename B,
typename T>
295 static bool get_results_json_object(QJsonObject& root,
296 const std::string& object_name,
297 JsonAsyncReader::ResultsList<B>& results,
298 std::string& error_string)
300 QString qobject_name(object_name.c_str());
301 if (root.contains(qobject_name))
303 if (root[qobject_name].isArray())
305 auto sub_root = root[qobject_name];
306 QJsonArray levelArray = sub_root.toArray();
307 for (
int levelIndex = 0; levelIndex < levelArray.size(); ++levelIndex)
309 QJsonObject levelObject = levelArray[levelIndex].toObject();
310 results.emplace_back(std::make_shared<T>(levelObject));
313 else if (root[qobject_name].isObject())
315 auto sub_root = root[qobject_name].toObject();
316 results.emplace_back(std::make_shared<T>(sub_root));
322 for (
auto member : root.keys())
324 auto item = root[member];
327 QJsonObject obj = item.toObject();
328 get_results_json_object<B, T>(obj, object_name, results, error_string);
335 template <
typename B,
typename T>
336 static bool get_results_json(QJsonDocument& doc,
337 const std::string& object_name,
338 JsonAsyncReader::ResultsList<B>& results,
339 std::string& error_string)
343 QJsonArray levelArray = doc.array();
344 for (
int levelIndex = 0; levelIndex < levelArray.size(); ++levelIndex)
346 QJsonObject levelObject = levelArray[levelIndex].toObject();
347 get_results_json_object<B, T>(levelObject, object_name, results, error_string);
350 else if (doc.isObject())
352 QJsonObject levelObject = doc.object();
353 get_results_json_object<B, T>(levelObject, object_name, results, error_string);
Definition: HttpAsyncReader.h:39
Class that downloads http JSON files asynchronously.
Definition: JsonAsyncReader.h:66