| 17 | #include "cmSystemTools.h" |
| 18 | |
| 19 | cmJSONState::cmJSONState(std::string jsonFile, Json::Value* root) |
| 20 | : Filename(std::move(jsonFile)) |
| 21 | { |
| 22 | cmsys::ifstream fin(this->Filename.c_str(), std::ios::in | std::ios::binary); |
| 23 | if (!fin) { |
| 24 | this->AddError(cmStrCat("File not found: ", this->Filename)); |
| 25 | return; |
| 26 | } |
| 27 | // If there's a BOM, toss it. |
| 28 | cmsys::FStream::ReadBOM(fin); |
| 29 | |
| 30 | // Save the entire document. |
| 31 | std::streampos finBegin = fin.tellg(); |
| 32 | this->doc = std::string(std::istreambuf_iterator<char>(fin), |
| 33 | std::istreambuf_iterator<char>()); |
| 34 | if (this->doc.empty()) { |
| 35 | this->AddError("A JSON document cannot be empty"); |
| 36 | return; |
| 37 | } |
| 38 | fin.seekg(finBegin); |
| 39 | |
| 40 | Json::CharReaderBuilder builder; |
| 41 | Json::CharReaderBuilder::strictMode(&builder.settings_); |
| 42 | std::string errMsg; |
| 43 | |
| 44 | #if JSONCPP_VERSION_HEXA >= 0x01090600 |
| 45 | // Has StructuredError |
| 46 | std::unique_ptr<Json::CharReader> const reader(builder.newCharReader()); |
| 47 | reader->parse(doc.data(), doc.data() + doc.size(), root, &errMsg); |
| 48 | std::vector<Json::CharReader::StructuredError> structuredErrors = |
| 49 | reader->getStructuredErrors(); |
| 50 | for (auto const& structuredError : structuredErrors) { |
| 51 | this->AddErrorAtOffset(structuredError.message, |
| 52 | structuredError.offset_start); |
| 53 | } |
| 54 | #else |
| 55 | // No StructuredError Available, Use error string from jsonCpp |
| 56 | if (!Json::parseFromStream(builder, fin, root, &errMsg)) { |
| 57 | errMsg = cmStrCat("JSON Parse Error: ", this->Filename, ":\n", errMsg); |
| 58 | this->AddError(errMsg); |
| 59 | } |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | void cmJSONState::AddError(std::string const& errMsg) |
| 64 | { |
nothing calls this directly
no test coverage detected