2018-08-13 21:23:43 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <json/json.h>
|
|
|
|
|
|
|
|
namespace waybar::util {
|
|
|
|
|
2018-08-18 17:54:20 +02:00
|
|
|
struct JsonParser {
|
|
|
|
|
|
|
|
JsonParser()
|
2018-08-20 14:50:45 +02:00
|
|
|
: reader_(builder_.newCharReader())
|
2018-08-18 17:54:20 +02:00
|
|
|
{}
|
|
|
|
|
2018-12-26 11:13:36 +01:00
|
|
|
const Json::Value parse(const std::string& data) const
|
2018-08-18 17:54:20 +02:00
|
|
|
{
|
|
|
|
Json::Value root;
|
|
|
|
std::string err;
|
|
|
|
bool res =
|
2018-08-20 14:50:45 +02:00
|
|
|
reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
2018-08-18 17:54:20 +02:00
|
|
|
if (!res)
|
|
|
|
throw std::runtime_error(err);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:50:45 +02:00
|
|
|
~JsonParser() = default;
|
2018-08-18 17:54:20 +02:00
|
|
|
|
|
|
|
private:
|
2018-08-20 14:50:45 +02:00
|
|
|
Json::CharReaderBuilder builder_;
|
|
|
|
std::unique_ptr<Json::CharReader> const reader_;
|
2018-08-18 17:54:20 +02:00
|
|
|
};
|
2018-08-13 21:23:43 +02:00
|
|
|
|
|
|
|
}
|