1 //! \file ToJsonWriter.cpp
3 #include "ToJsonWriter.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
));
28 std::string
ToJsonWriter::convertToJson(const Article
* a
)
30 Json::Value
val(Json::ValueType::objectValue
);
32 Json::Value
linkObj(Json::ValueType::objectValue
);
35 linkObj
["forward_links"] = getArticleLinks(a
);
37 linkObj
["forward_links"] = Json::Value::nullSingleton();
40 val
[a
->getTitle()] = linkObj
;
43 jsw
.omitEndingLineFeed();
45 return jsw
.write(val
);
48 std::string
ToJsonWriter::convertToJson(const ArticleCollection
& ac
)
50 Json::Value
val(Json::ValueType::objectValue
);
53 Json::Value
linkObj(Json::ValueType::objectValue
);
55 if(ar
.second
->isAnalyzed()) {
56 linkObj
["forward_links"] = getArticleLinks(ar
.second
);
58 linkObj
["forward_links"] = Json::Value::nullSingleton();
61 val
[ar
.first
] = linkObj
;
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
);