Introduce version 0.1
[dueringa_WikiWalker.git] / src / CacheJsonToArticleConverter.cpp
blobd6e5c1c058631e032ec22b4b82c3033a93b956d2
1 //! \file CacheJsonToArticleConverter.cpp
3 #include "CacheJsonToArticleConverter.h"
5 #include <json/json.h>
7 #include "WalkerException.h"
8 #include "Article.h"
10 ArticleCollection& CacheJsonToArticleConverter::convertToArticle(std::string json,
11 ArticleCollection& articleCache)
13 Json::Reader reader;
14 Json::Value document;
15 bool success = reader.parse(json, document, false);
17 if(!success) {
18 throw WalkerException("Error parsing JSON");
21 // get all "main" articles first
22 for(auto& titleElement : document.getMemberNames()) {
23 std::string title = titleElement;
25 Article* a = articleCache.get(title);
27 if(a == nullptr) {
28 a = new Article(title);
29 articleCache.add(a);
32 auto links = document
33 .get(title, Json::Value::nullSingleton())
34 .get("forward_links", Json::Value::nullSingleton());
36 if(links.isNull()) {
37 /* don't need to set article analyzed to false,
38 * since that's the default */
39 continue;
42 a->setAnalyzed(true);
44 for(auto linkedArticle : links) {
45 std::string linkedTitle = linkedArticle.asString();
46 Article* la = articleCache.get(linkedTitle);
48 if(la == nullptr) {
49 la = new Article(linkedTitle);
50 articleCache.add(la);
53 a->addLink(la);
57 return articleCache;
59 a->setAnalyzed(true); ?