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 {
|
2019-04-18 17:52:00 +02:00
|
|
|
JsonParser() : reader_(builder_.newCharReader()) {}
|
2018-08-18 17:54:20 +02:00
|
|
|
|
2019-04-23 15:56:38 +02:00
|
|
|
const Json::Value parse(const std::string& data) const {
|
2019-04-23 11:41:49 +02:00
|
|
|
Json::Value root(Json::objectValue);
|
2019-04-18 17:43:16 +02:00
|
|
|
if (data.empty()) {
|
|
|
|
return root;
|
|
|
|
}
|
2019-04-23 11:41:49 +02:00
|
|
|
std::string err;
|
2019-04-23 15:56:38 +02:00
|
|
|
bool res = reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
2019-04-18 17:52:00 +02:00
|
|
|
if (!res) throw std::runtime_error(err);
|
2018-08-18 17:54:20 +02:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:50:45 +02:00
|
|
|
~JsonParser() = default;
|
2018-08-18 17:54:20 +02:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
private:
|
|
|
|
Json::CharReaderBuilder builder_;
|
2018-08-20 14:50:45 +02:00
|
|
|
std::unique_ptr<Json::CharReader> const reader_;
|
2018-08-18 17:54:20 +02:00
|
|
|
};
|
2018-08-13 21:23:43 +02:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
} // namespace waybar::util
|