refactor: try/catch, sigc trackable

This commit is contained in:
Alex
2019-05-13 15:15:50 +02:00
parent 0c3c548bc0
commit 362c393b1d
7 changed files with 21 additions and 16 deletions

View File

@ -5,15 +5,16 @@
namespace waybar::util {
struct JsonParser {
JsonParser() : reader_(builder_.newCharReader()) {}
JsonParser() {}
const Json::Value parse(const std::string& data) const {
Json::Value root(Json::objectValue);
if (data.empty()) {
return root;
}
std::string err;
bool res = reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
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);
if (!res) throw std::runtime_error(err);
return root;
}
@ -21,8 +22,7 @@ struct JsonParser {
~JsonParser() = default;
private:
Json::CharReaderBuilder builder_;
std::unique_ptr<Json::CharReader> const reader_;
Json::CharReaderBuilder builder_;
};
} // namespace waybar::util