waybar/include/util/json.hpp

29 lines
638 B
C++
Raw Normal View History

2018-08-13 21:23:43 +02:00
#pragma once
#include <json/json.h>
namespace waybar::util {
struct JsonParser {
2019-05-13 15:15:50 +02:00
JsonParser() {}
const Json::Value parse(const std::string& data) const {
Json::Value root(Json::objectValue);
if (data.empty()) {
return root;
}
2019-05-13 15:15:50 +02:00
std::unique_ptr<Json::CharReader> const reader(builder_.newCharReader());
std::string err;
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);
return root;
}
2018-08-20 14:50:45 +02:00
~JsonParser() = default;
2019-04-18 17:52:00 +02:00
private:
2019-05-13 15:15:50 +02:00
Json::CharReaderBuilder builder_;
};
2018-08-13 21:23:43 +02:00
2019-04-18 17:52:00 +02:00
} // namespace waybar::util