|
1 | 1 | #include "JsonUtils.h"
|
2 |
| - |
| 2 | +#include "StringUtils.h" |
3 | 3 | #include "utils/FileSystemUtils.h"
|
| 4 | +#include "utils/path/FileSystemPath.h" |
4 | 5 |
|
5 | 6 | #include "loguru.h"
|
6 | 7 |
|
7 |
| -#include "utils/path/FileSystemPath.h" |
8 |
| -#include <fstream> |
9 | 8 | #include <exception>
|
| 9 | +#include <fstream> |
10 | 10 |
|
11 | 11 | namespace JsonUtils {
|
12 | 12 | nlohmann::json getJsonFromFile(const fs::path &path) {
|
13 | 13 | std::ifstream stream(path.string());
|
14 | 14 | std::stringstream buffer;
|
15 | 15 | buffer << stream.rdbuf();
|
| 16 | + std::string contentToParse = buffer.str(); |
16 | 17 | try {
|
17 |
| - nlohmann::json coverageJson = nlohmann::json::parse(buffer.str()); |
| 18 | + // skip warning header lines from input stream like |
| 19 | + // warning: 1 functions have mismatched data |
| 20 | + // {JSON} |
| 21 | + std::string::size_type pos = 0; |
| 22 | + while (pos < contentToParse.size()) { |
| 23 | + const char ch = contentToParse[pos]; |
| 24 | + if (!std::isspace(ch)) { |
| 25 | + if (ch == '{' || ch == '[') { |
| 26 | + break; |
| 27 | + } |
| 28 | + pos = contentToParse.find('\n', pos); |
| 29 | + if (pos == std::string::npos) { |
| 30 | + const std::invalid_argument e("empty json file"); |
| 31 | + LOG_S(ERROR) << e.what(); |
| 32 | + throw e; |
| 33 | + } |
| 34 | + } |
| 35 | + ++pos; |
| 36 | + } |
| 37 | + if (pos > 0) { |
| 38 | + std::string warningHeader = contentToParse.substr(0, pos); |
| 39 | + StringUtils::trim(warningHeader); |
| 40 | + if (!warningHeader.empty()) { |
| 41 | + LOG_S(WARNING) << "JSON header: [" << warningHeader << "] in: " << path.string(); |
| 42 | + } |
| 43 | + } |
| 44 | + nlohmann::json coverageJson = nlohmann::json::parse(contentToParse.substr(pos)); |
18 | 45 | return coverageJson;
|
19 | 46 | } catch (const std::exception &e) {
|
20 |
| - LOG_S(ERROR) << e.what() << ": " << buffer.str() << " in: " << path.string(); |
| 47 | + LOG_S(ERROR) << e.what() << ": " << contentToParse << " in: " << path.string(); |
21 | 48 | throw e;
|
22 | 49 | } catch (...) {
|
23 |
| - LOG_S(ERROR) << buffer.str(); |
| 50 | + LOG_S(ERROR) << "Crash: " << contentToParse << " in: " << path.string(); |
24 | 51 | throw;
|
25 | 52 | }
|
26 | 53 | }
|
|
0 commit comments