2018-08-13 19:23:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-07-12 05:52:33 +00:00
|
|
|
#include <fmt/ostream.h>
|
2018-08-13 19:23:43 +00:00
|
|
|
#include <json/json.h>
|
|
|
|
|
2022-07-12 05:52:33 +00:00
|
|
|
#if (FMT_VERSION >= 90000)
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<Json::Value> : ostream_formatter {};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-08-13 19:23:43 +00:00
|
|
|
namespace waybar::util {
|
|
|
|
|
2018-08-18 15:54:20 +00:00
|
|
|
struct JsonParser {
|
2019-05-13 13:15:50 +00:00
|
|
|
JsonParser() {}
|
2018-08-18 15:54:20 +00:00
|
|
|
|
2019-04-23 13:56:38 +00:00
|
|
|
const Json::Value parse(const std::string& data) const {
|
2019-04-23 09:41:49 +00:00
|
|
|
Json::Value root(Json::objectValue);
|
2019-04-18 15:43:16 +00:00
|
|
|
if (data.empty()) {
|
|
|
|
return root;
|
|
|
|
}
|
2019-05-13 13:15:50 +00:00
|
|
|
std::unique_ptr<Json::CharReader> const reader(builder_.newCharReader());
|
2022-04-06 06:37:19 +00:00
|
|
|
std::string err;
|
2019-05-13 13:15:50 +00:00
|
|
|
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);
|
2018-08-18 15:54:20 +00:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2018-08-20 12:50:45 +00:00
|
|
|
~JsonParser() = default;
|
2018-08-18 15:54:20 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
private:
|
2019-05-13 13:15:50 +00:00
|
|
|
Json::CharReaderBuilder builder_;
|
2018-08-18 15:54:20 +00:00
|
|
|
};
|
2018-08-13 19:23:43 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
} // namespace waybar::util
|