Waybar/include/util/json.hpp

32 lines
545 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 {
JsonParser()
2018-08-20 12:50:45 +00:00
: reader_(builder_.newCharReader())
{}
2018-12-26 10:13:36 +00:00
const Json::Value parse(const std::string& data) const
{
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);
if (!res)
throw std::runtime_error(err);
return root;
}
2018-08-20 12:50:45 +00:00
~JsonParser() = default;
private:
2018-08-20 12:50:45 +00:00
Json::CharReaderBuilder builder_;
std::unique_ptr<Json::CharReader> const reader_;
};
2018-08-13 19:23:43 +00:00
}