Implement nlohmann/json utils

This commit is contained in:
Tengiz Sharafiev 2016-02-02 17:18:28 +03:00
parent c1ac8ba194
commit 8b069ccd23

View File

@ -0,0 +1,35 @@
#pragma once
#ifndef VALIJSON_NLOHMANN_JSON_UTILS_HPP_HPP
#define VALIJSON_NLOHMANN_JSON_UTILS_HPP_HPP
#include <json.hpp>
#include <valijson/utils/file_utils.hpp>
namespace valijson {
namespace utils {
inline bool loadDocument(const std::string &path, nlohmann::json &document) {
// Load schema JSON from file
std::string file;
if (!loadFile(path, file)) {
std::cerr << "Failed to load json from file '" << path << "'." << std::endl;
return false;
}
// Parse schema
std::string err;
try {
document = nlohmann::json::parse(file);
} catch (std::invalid_argument const& exception) {
std::cerr << "nlohmann::json failed to parse the document\n"
<< "Parse error:" << exception.what() << "\n";
return false;
}
return true;
}
}
}
#endif //VALIJSON_NLOHMANN_JSON_UTILS_HPP_HPP