Add a toJson method to database

- only tag information for tags with count>0 is written out
This commit is contained in:
Thomas Lauf 2018-07-31 07:00:15 +02:00 committed by lauft
parent bfde36a740
commit b32237cfb8
4 changed files with 50 additions and 0 deletions

View file

@ -25,6 +25,7 @@
////////////////////////////////////////////////////////////////////////////////
#include <TagInfo.h>
#include <sstream>
////////////////////////////////////////////////////////////////////////////////
TagInfo::TagInfo (unsigned int count)
@ -43,3 +44,18 @@ unsigned int TagInfo::decrement ()
{
return --_count;
}
////////////////////////////////////////////////////////////////////////////////
bool TagInfo::hasCount ()
{
return _count > 0;
}
////////////////////////////////////////////////////////////////////////////////
std::string TagInfo::toJson ()
{
std::stringstream output;
output << "{\"count\":" << _count << "}";
return output.str ();
}

View file

@ -27,6 +27,8 @@
#ifndef INCLUDED_TAGINFO
#define INCLUDED_TAGINFO
#include <string>
class TagInfo
{
public:
@ -35,6 +37,9 @@ public:
unsigned int increment ();
unsigned int decrement ();
bool hasCount ();
std::string toJson ();
private:
unsigned int _count = 0;

View file

@ -73,3 +73,30 @@ void TagInfoDatabase::add (const std::string& tag, const TagInfo& tagInfo)
{
_tagInformation.emplace (tag, tagInfo);
}
///////////////////////////////////////////////////////////////////////////////
std::string TagInfoDatabase::toJson ()
{
std::stringstream json;
bool first = true;
json << "{";
for (auto& pair : _tagInformation)
{
auto tagInfo = pair.second;
if (tagInfo.hasCount ())
{
json << (first ? "" : ",")
<< "\"" << escape(pair.first, '"') << "\":"
<< tagInfo.toJson ();
first = (first ? false : first);
}
}
json << "}";
return json.str ();
}

View file

@ -39,6 +39,8 @@ public:
void add (const std::string&, const TagInfo&);
std::string toJson ();
private:
std::map <std::string, TagInfo> _tagInformation {};
};