Introduce version 0.1
[dueringa_WikiWalker.git] / src / ToJsonWriter.cpp
blob5205813629cc6359888c3053dc7a004bc90db6c7
1 //! \file ToJsonWriter.cpp
3 #include "ToJsonWriter.h"
5 #include <json/json.h>
7 #include "Article.h"
9 /*! Get article links in an array.
10 * Basically undoing the Wikipedia to article conversion...
11 * \param article pointer to article which links should be extracted
12 * \return Json::Value array with titles as string
13 * \todo should / could be a member function, but then I'd have to expose
14 * Json::Value, which is ugly and clutters up other classes...
16 static Json::Value getArticleLinks(const Article* article)
18 Json::Value array(Json::ValueType::arrayValue);
20 for(auto ali = article->linkBegin(); ali != article->linkEnd(); ali++) {
21 std::string tit = (*ali)->getTitle();
22 array.append(Json::Value(tit));
25 return array;
28 std::string ToJsonWriter::convertToJson(const Article* a)
30 Json::Value val(Json::ValueType::objectValue);
32 Json::Value linkObj(Json::ValueType::objectValue);
34 if(a->isAnalyzed()) {
35 linkObj["forward_links"] = getArticleLinks(a);
36 } else {
37 linkObj["forward_links"] = Json::Value::nullSingleton();
40 val[a->getTitle()] = linkObj;
42 Json::FastWriter jsw;
43 jsw.omitEndingLineFeed();
45 return jsw.write(val);
48 std::string ToJsonWriter::convertToJson(const ArticleCollection& ac)
50 Json::Value val(Json::ValueType::objectValue);
52 for(auto ar : ac) {
53 Json::Value linkObj(Json::ValueType::objectValue);
55 if(ar.second->isAnalyzed()) {
56 linkObj["forward_links"] = getArticleLinks(ar.second);
57 } else {
58 linkObj["forward_links"] = Json::Value::nullSingleton();
61 val[ar.first] = linkObj;
64 Json::FastWriter jsw;
65 jsw.omitEndingLineFeed();
67 return jsw.write(val);
70 void ToJsonWriter::output(const Article* article, std::ostream& outstream)
72 outstream << convertToJson(article);
75 void ToJsonWriter::output(const ArticleCollection& collection,
76 std::ostream& outstream)
78 outstream << convertToJson(collection);