mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Add a toJson method to database
- only tag information for tags with count>0 is written out
This commit is contained in:
parent
bfde36a740
commit
b32237cfb8
4 changed files with 50 additions and 0 deletions
|
@ -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 @@ public:
|
|||
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 @@ public:
|
|||
|
||||
void add (const std::string&, const TagInfo&);
|
||||
|
||||
std::string toJson ();
|
||||
|
||||
private:
|
||||
std::map <std::string, TagInfo> _tagInformation {};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue