mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-03-03 12:58:03 +01:00
Implement nlohmann/json utils
This commit is contained in:
parent
c1ac8ba194
commit
8b069ccd23
35
include/valijson/utils/nlohmann_json_utils.hpp
Normal file
35
include/valijson/utils/nlohmann_json_utils.hpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user