2018-08-13 19:23:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <json/json.h>
|
|
|
|
|
|
|
|
namespace waybar::util {
|
|
|
|
|
2018-08-18 15:54:20 +00:00
|
|
|
struct JsonParser {
|
|
|
|
|
|
|
|
JsonParser()
|
|
|
|
: _reader(_builder.newCharReader())
|
|
|
|
{}
|
|
|
|
|
|
|
|
Json::Value parse(const std::string data)
|
|
|
|
{
|
|
|
|
Json::Value root;
|
|
|
|
std::string err;
|
2018-08-19 11:39:57 +00:00
|
|
|
if (_reader == nullptr) {
|
|
|
|
throw std::runtime_error("Unable to parse");
|
|
|
|
}
|
2018-08-18 15:54:20 +00:00
|
|
|
bool res =
|
|
|
|
_reader->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
|
|
|
if (!res)
|
|
|
|
throw std::runtime_error(err);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
~JsonParser()
|
|
|
|
{
|
|
|
|
delete _reader;
|
2018-08-19 11:39:57 +00:00
|
|
|
_reader = nullptr;
|
2018-08-18 15:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Json::CharReaderBuilder _builder;
|
|
|
|
Json::CharReader *_reader;
|
|
|
|
};
|
2018-08-13 19:23:43 +00:00
|
|
|
|
|
|
|
}
|