| 367 | } |
| 368 | |
| 369 | nlohmann::json readJSON(fs::path const &filePath, std::ios_base::openmode mode) |
| 370 | { |
| 371 | |
| 372 | // Shenanigans would not be needed with fmt 10+ (maybe earlier), because fmt has native fs::path support |
| 373 | if (!fileExists(filePath)) { |
| 374 | throw FatalError(std::format("File does not exists: {}", filePath.string())); |
| 375 | } |
| 376 | |
| 377 | // Can only be 'r', 'b' or 'rb' |
| 378 | if ((mode & (std::ios_base::in | std::ios_base::binary)) == 0) { |
| 379 | throw FatalError("ERROR - readFile: Bad openmode argument. Must be std::ios_base::in or std::ios_base::binary"); |
| 380 | } |
| 381 | |
| 382 | std::ifstream file(filePath, mode); |
| 383 | if (!file.is_open()) { |
| 384 | throw FatalError(std::format("Could not open file: {}", filePath.string())); |
| 385 | } |
| 386 | |
| 387 | FileTypes const ext = getFileType(filePath); |
| 388 | switch (ext) { |
| 389 | case FileTypes::EpJSON: |
| 390 | case FileTypes::JSON: |
| 391 | case FileTypes::GLHE: |
| 392 | return nlohmann::json::parse(file, nullptr, true, true); |
| 393 | case FileTypes::CBOR: |
| 394 | return nlohmann::json::from_cbor(file); |
| 395 | case FileTypes::MsgPack: |
| 396 | return nlohmann::json::from_msgpack(file); |
| 397 | case FileTypes::UBJSON: |
| 398 | return nlohmann::json::from_ubjson(file); |
| 399 | case FileTypes::BSON: |
| 400 | return nlohmann::json::from_bson(file); |
| 401 | default: |
| 402 | throw FatalError("Invalid file extension. Must be epJSON, JSON, or other experimental extensions"); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | std::string toString(fs::path const &p) |
| 407 | { |
no test coverage detected