From 1f2d902cd44e0447fc50844a12201069b41490ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andreas=20D=C3=BCring?= Date: Tue, 22 Nov 2016 16:30:39 +0100 Subject: [PATCH] Experimental: Add converter to JSON maybe for local cache file --- src/ToJsonWriter.cpp | 27 +++++++++++++++++++++++++++ src/ToJsonWriter.h | 16 ++++++++++++++++ test/Makefile | 6 ++++-- test/ToJsonWriterTests.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 src/ToJsonWriter.cpp create mode 100644 src/ToJsonWriter.h create mode 100644 test/ToJsonWriterTests.cpp diff --git a/src/ToJsonWriter.cpp b/src/ToJsonWriter.cpp new file mode 100644 index 0000000..9cdf60b --- /dev/null +++ b/src/ToJsonWriter.cpp @@ -0,0 +1,27 @@ +#include "ToJsonWriter.h" + +#include + +std::string ToJsonWriter::convertToJson(const Article* a) +{ + Json::Value val; + + auto array = val[a->getTitle()]; + + for(auto ali = a->linkBegin(); ali != a->linkEnd(); ali++) { + std::string tit = (*ali)->getTitle(); + array.append(Json::Value(tit)); + } + + val[a->getTitle()] = array; + + Json::FastWriter jsw; + + return jsw.write(val); +} + +std::string ToJsonWriter::convertToJson(const ArticleCollection&) +{ + return ""; +} + diff --git a/src/ToJsonWriter.h b/src/ToJsonWriter.h new file mode 100644 index 0000000..6f22a97 --- /dev/null +++ b/src/ToJsonWriter.h @@ -0,0 +1,16 @@ +#ifndef _TOJSONWRITER_H +#define _TOJSONWRITER_H + +#include +#include "Article.h" +#include "ArticleCollection.h" + +class ToJsonWriter +{ +public: + std::string convertToJson(const Article*); + std::string convertToJson(const ArticleCollection&); +}; + +#endif // _TOJSONWRITER_H + diff --git a/test/Makefile b/test/Makefile index f79acea..3bcefd4 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ .PHONY: clean -CFLAGS = `pkg-config --cflags unittest++ ` -std=c++11 +CFLAGS = `pkg-config --cflags unittest++ ` -std=c++11 -MMD CXXFLAGS = $(CFLAGS) SRCS = $(wildcard *.cpp) OBJS = $(SRCS:.cpp=.o) @@ -12,7 +12,9 @@ $(BINARY): $(OBJS) g++ $(CXXFLAGS) $(OBJS) -o $(BINARY) $(LIBS) \ ../src/ArticleCollection.o ../src/CurlUrlCreator.o \ ../src/JsonToArticleConverter.o ../lib/jsoncpp/jsoncpp.o \ - ../src/Article.o + ../src/Article.o ../src/ToJsonWriter.o clean: rm -f *.o *.d $(BINARY) + +-include $(OBJS:%.o=%.d) diff --git a/test/ToJsonWriterTests.cpp b/test/ToJsonWriterTests.cpp new file mode 100644 index 0000000..e48b453 --- /dev/null +++ b/test/ToJsonWriterTests.cpp @@ -0,0 +1,31 @@ +#include + +#include "../src/ToJsonWriter.h" + +SUITE(ArticleToJsonWriterTests) +{ + TEST(Unidentified) + { + ToJsonWriter atj; + Article a("Farm"); + CHECK_EQUAL("", atj.convertToJson(&a)); + } + + TEST(Unidentified2) + { + ToJsonWriter atj; + Article a("Farm"); + a.addLink(new Article("Animal")); + CHECK_EQUAL("", atj.convertToJson(&a)); + } + + TEST(Unidentified3) + { + ToJsonWriter atj; + Article a("Farm"); + a.addLink(new Article("Animal")); + a.addLink(new Article("Pig")); + a.addLink(new Article("Equality")); + CHECK_EQUAL("", atj.convertToJson(&a)); + } +} -- 2.11.4.GIT