Permalink
Browse files

Add a toJson method to database

- only tag information for tags with count>0 is written out
  • Loading branch information...
lauft committed Jul 31, 2018
1 parent 1420ca2 commit daee9ed3c727c2e133b56821f386f3cd81fb55bc
Showing with 50 additions and 0 deletions.
  1. +16 −0 src/TagInfo.cpp
  2. +5 −0 src/TagInfo.h
  3. +27 −0 src/TagInfoDatabase.cpp
  4. +2 −0 src/TagInfoDatabase.h
@@ -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 ();
}
@@ -27,6 +27,8 @@
#ifndef INCLUDED_TAGINFO
#define INCLUDED_TAGINFO

#include <string>

class TagInfo
{
public:
@@ -35,6 +37,9 @@ class TagInfo
unsigned int increment ();
unsigned int decrement ();

bool hasCount ();

std::string toJson ();

private:
unsigned int _count = 0;
@@ -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 ();
}
@@ -39,6 +39,8 @@ class TagInfoDatabase

void add (const std::string&, const TagInfo&);

std::string toJson ();

private:
std::map <std::string, TagInfo> _tagInformation {};
};

0 comments on commit daee9ed

Please sign in to comment.