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