waybar/include/util/json.hpp

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