Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions server/src/utils/JsonUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
#include "JsonUtils.h"

#include "StringUtils.h"
#include "utils/FileSystemUtils.h"
#include "utils/path/FileSystemPath.h"

#include "loguru.h"

#include "utils/path/FileSystemPath.h"
#include <fstream>
#include <exception>
#include <fstream>

namespace JsonUtils {
nlohmann::json getJsonFromFile(const fs::path &path) {
std::ifstream stream(path.string());
std::stringstream buffer;
buffer << stream.rdbuf();
std::string contentToParse = buffer.str();
try {
nlohmann::json coverageJson = nlohmann::json::parse(buffer.str());
// skip warning header lines from input stream like
// warning: 1 functions have mismatched data
// {JSON}
std::string::size_type pos = 0;
while (pos < contentToParse.size()) {
const char ch = contentToParse[pos];
if (!std::isspace(ch)) {
if (ch == '{' || ch == '[') {
break;
}
pos = contentToParse.find('\n', pos);
if (pos == std::string::npos) {
const std::invalid_argument e("empty json file");
LOG_S(ERROR) << e.what();
throw e;
}
}
++pos;
}
if (pos > 0) {
std::string warningHeader = contentToParse.substr(0, pos);
StringUtils::trim(warningHeader);
if (!warningHeader.empty()) {
LOG_S(WARNING) << "JSON header: [" << warningHeader << "] in: " << path.string();
}
}
nlohmann::json coverageJson = nlohmann::json::parse(contentToParse.substr(pos));
return coverageJson;
} catch (const std::exception &e) {
LOG_S(ERROR) << e.what() << ": " << buffer.str() << " in: " << path.string();
LOG_S(ERROR) << e.what() << ": " << contentToParse << " in: " << path.string();
throw e;
} catch (...) {
LOG_S(ERROR) << buffer.str();
LOG_S(ERROR) << "Crash: " << contentToParse << " in: " << path.string();
throw;
}
}
Expand Down