Switched from Boost to JsonCpp for JSON parsing

This commit is contained in:
tastytea 2018-03-18 14:53:51 +01:00
parent c25d8cc5f9
commit 94b00bca99
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
7 changed files with 56 additions and 57 deletions

View File

@ -10,7 +10,7 @@ addons:
packages:
- g++-5
- libcurlpp-dev
- libboost-all-dev
- libjsoncpp-dev
before_install:
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 60
script:

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.6.5
VERSION 0.6.6
LANGUAGES CXX
)
@ -60,7 +60,7 @@ if(WITH_EXAMPLES)
foreach(src ${sources_examples})
get_filename_component(bin ${src} NAME_WE)
add_executable(${bin} ${src})
target_link_libraries(${bin} -lpthread ${Boost_LIBRARIES} mastodon-cpp)
target_link_libraries(${bin} -lpthread ${Boost_LIBRARIES} -ljsoncpp mastodon-cpp)
endforeach()
endif()

View File

@ -86,7 +86,7 @@ To use the DEB package on stretch, you will need [libcurlpp0](https://packages.d
* [curlpp](http://www.curlpp.org/) (tested: 0.8.1/0.7.3)
* Optional
* Documentation: [doxygen](https://www.stack.nl/~dimitri/doxygen/) (tested: 1.8.13)
* Examples: [boost](http://www.boost.org/) (tested: 1.63.0/1.54.0)
* Examples: [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.1)
* DEB package: [dpkg](https://packages.qa.debian.org/dpkg) (tested: 1.19.0.5)
* RPM package: [rpm](http://www.rpm.org) (tested: 4.11.0.1)

View File

@ -6,13 +6,10 @@
#include <vector>
#include <string>
#include <cstdint>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <jsoncpp/json/json.h>
#include "../mastodon-cpp.hpp"
using Mastodon::API;
namespace pt = boost::property_tree;
using std::cout;
int main(int argc, char *argv[])
@ -30,29 +27,29 @@ int main(int argc, char *argv[])
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::istringstream iss(answer);
pt::ptree tree;
pt::read_json(iss, tree);
std::string uid = tree.get<std::string>("id");
Json::Reader reader;
Json::Value json;
reader.parse(answer, json);
std::string uid = json["id"].asString();
cout << "Your ID is: " << uid << '\n';
cout << "Your whole acount tree:\n";
for (const pt::ptree::value_type &v : tree.get_child(""))
for (auto it = json.begin(); it != json.end(); ++it)
{
cout << " ";
if (v.second.size() > 0)
if (it.name().compare("source") == 0)
{
cout << v.first.data() << ": \n";
for (const pt::ptree::value_type &vc : v.second.get_child(""))
cout << it.name() << '\n';
for (auto it_s = (*it).begin(); it_s != (*it).end(); ++it_s)
{
cout << " ";
cout << vc.first.data() << ": " << vc.second.data() << '\n';
cout << '\t' << it_s.name() << ": ";
cout << *it_s << '\n';
}
}
else
{
cout << v.first.data() << ": " << v.second.data() << '\n';
cout << it.name() << ": ";
cout << *it << '\n';
}
}
}

View File

@ -6,14 +6,12 @@
#include <vector>
#include <string>
#include <cstdint>
#include <sstream>
#include <regex>
#include <cstdlib>
#include <fstream>
#include <jsoncpp/json/json.h>
#include "../mastodon-cpp.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
namespace pt = boost::property_tree;
using Mastodon::API;
using std::cout;
using std::string;
@ -36,14 +34,16 @@ int main(int argc, char *argv[])
string hashtag = argv[2];
string answer;
std::uint16_t ret;
pt::ptree config;
Json::Value config;
string lastid = "0";
string filename = string(getenv("HOME")) + "/.config/mastocron.json";
// Read config file, get last seen toot-id
try {
pt::read_json(filename, config);
lastid = config.get(hashtag, "0");
Json::Reader reader;
std::ifstream file(filename, std::ifstream::binary);
file >> config;
lastid = config.get(hashtag, "0").asString();
}
catch (std::exception &e)
{
@ -73,13 +73,13 @@ int main(int argc, char *argv[])
cout << " + " << hashtag << ": +\n";
cout << ornament << '\n';
std::istringstream iss(answer);
pt::ptree tree;
Json::Value tree;
Json::Reader reader;
pt::read_json(iss, tree);
for (const pt::ptree::value_type &toot : tree.get_child(""))
reader.parse(answer, tree);
for (const auto &toot : tree)
{
string content = toot.second.get<string>("content");
string content = toot["content"].asString();
std::regex reparagraph("</p><p>");
std::regex restrip("<[^>]*>");
@ -87,21 +87,29 @@ int main(int argc, char *argv[])
content = std::regex_replace(content, reparagraph, "\n\n");
cout << std::regex_replace(content, restrip, "") << '\n';
cout << " ";
cout << toot.second.get<string>("account.display_name")
<< " (" << toot.second.get<string>("account.acct") << ") at "
<< toot.second.get<string>("created_at") << "\n";
cout << " " << toot.second.get<string>("url") << '\n';
for (const pt::ptree::value_type &media : toot.second.get_child("media_attachments"))
cout << toot["account"]["display_name"].asString()
<< " (" << toot["account"]["acct"] << ") at "
<< toot["created_at"].asString() << "\n";
cout << " " << toot["url"].asString() << '\n';
for (const auto &media : toot["media_attachments"])
{
cout << "Attachment: <" << media.second.get<string>("url") << ">\n";
cout << "Attachment: <" << media["url"].asString() << ">\n";
}
cout << "++++++++\n";
}
// Write the id of the newest toot in the config file
lastid = tree.front().second.get<string>("id");
config.put(hashtag, lastid);
pt::write_json(filename, config);
lastid = tree[0]["id"].asString();
config[hashtag] = lastid;
Json::StyledWriter writer;
const string output = writer.write(config);
std::ofstream outfile(filename);
if (outfile.is_open())
{
outfile.write(output.c_str(), output.length());
outfile.close();
}
}
}
else if (ret == 13)

View File

@ -7,8 +7,6 @@
#include <string>
#include <cstdint>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "../mastodon-cpp.hpp"
using Mastodon::API;

View File

@ -6,12 +6,10 @@
#include <vector>
#include <string>
#include <cstdint>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <jsoncpp/json/json.h>
#include "../mastodon-cpp.hpp"
using Mastodon::API;
namespace pt = boost::property_tree;
int main(int argc, char *argv[])
{
@ -23,8 +21,8 @@ int main(int argc, char *argv[])
Mastodon::API masto(argv[1], argv[2]);
std::string answer;
std::istringstream iss;
pt::ptree tree;
Json::Value json;
Json::Reader reader;
std::uint16_t ret;
std::string filepath;
@ -38,10 +36,9 @@ int main(int argc, char *argv[])
ret = masto.post(API::v1::media, parameters, answer);
if (ret == 0)
{
iss.str(answer);
pt::read_json(iss, tree);
std::string image1_id = tree.get<std::string>("id");
std::string image1_url = tree.get<std::string>("url");
reader.parse(answer, json);
std::string image1_id = json["id"].asString();
std::string image1_url = json["url"].asString();
parameters =
{
{ "file", { filepath } }
@ -49,10 +46,9 @@ int main(int argc, char *argv[])
ret = masto.post(API::v1::media, parameters, answer);
if (ret == 0)
{
iss.str(answer);
pt::read_json(iss, tree);
std::string image2_id = tree.get<std::string>("id");
std::string image2_url = tree.get<std::string>("url");
reader.parse(answer, json);
std::string image2_id = json["id"].asString();
std::string image2_url = json["url"].asString();
parameters =
{
{ "status", { image1_url + " \n" + image2_url } },