Waybar/include/util/json.hpp

30 lines
708 B
C++
Raw Normal View History

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