29 #include <json/reader.h>
30 #include <boost/filesystem.hpp>
48 static inline std::pair<FileLoadAttempt, Json::Value>
49 attemptFileLoad(std::string
const &fullFn) {
51 Json::Value ret{Json::nullValue};
52 auto attempt = FileLoadAttempt{fullFn};
54 std::ifstream file{fullFn};
56 attempt.status = FileLoadStatus::CouldNotOpenFile;
57 return make_pair(attempt, ret);
61 if (!reader.parse(file, ret)) {
62 attempt.status = FileLoadStatus::CouldNotParseFile;
63 attempt.details = reader.getFormattedErrorMessages();
65 ret = Json::nullValue;
66 return make_pair(attempt, ret);
69 attempt.status = FileLoadStatus::FileOpenedAndParsed;
70 return make_pair(attempt, ret);
77 static inline std::pair<bool, Json::Value>
78 loadFromFile(FileLoadAttempts &attempts, std::string
const &fn,
79 std::vector<std::string>
const &searchPath) {
80 Json::Value ret{Json::nullValue};
81 for (
auto const &path : searchPath) {
82 auto fullFn = boost::filesystem::path(path) / fn;
83 FileLoadAttempt attempt;
84 tie(attempt, ret) = attemptFileLoad(fullFn.string());
85 attempts.push_back(attempt);
86 if (attempt.status == FileLoadStatus::FileOpenedAndParsed) {
87 return make_pair(
true, ret);
93 FileLoadAttempt attempt;
94 tie(attempt, ret) = attemptFileLoad(fn);
95 attempts.push_back(attempt);
96 return make_pair(FileLoadStatus::FileOpenedAndParsed == attempt.status,
102 bool stringAcceptableResult,
103 std::vector<std::string>
const &searchPath) {
105 ret.result = Json::nullValue;
107 if (input.isString()) {
108 tie(ret.resolved, ret.result) =
109 loadFromFile(ret.fileAttempts, input.asString(), searchPath);
111 ret.handledAs = ValueHandledAs::Filename;
114 if (stringAcceptableResult) {
116 ret.handledAs = ValueHandledAs::String;
124 if (input.isObject() && input.isMember(
"$ref")) {
126 tie(ret.resolved, ret.result) = loadFromFile(
127 ret.fileAttempts, input[
"$ref"].asString(), searchPath);
129 ret.handledAs = ValueHandledAs::JsonRefToFile;
139 bool stringAcceptableResult,
140 std::vector<std::string>
const &searchPath) {
147 const char *fileLoadStatusToString(FileLoadStatus status) {
149 case FileLoadStatus::CouldNotOpenFile:
150 return "Could not open file";
152 case FileLoadStatus::CouldNotParseFile:
153 return "Could not parse file, ";
155 case FileLoadStatus::FileOpenedAndParsed:
156 return "File opened and parsed";
Json::Value resolvePossibleRef(Json::Value const &input, bool stringAcceptableResult, std::vector< std::string > const &searchPath)
Given an input that might be a filename, might be a JSON Pointer-style $ref object, and might just directly be an object, return the object desired.
ResolveRefResult resolvePossibleRefWithDetails(Json::Value const &input, bool stringAcceptableResult, std::vector< std::string > const &searchPath)