Loading...
Searching...
No Matches
json_file.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * write_json_file — a JSON document to a path, the way every results file of
5 * the engine and its utility is written: the parent directories created,
6 * four-space indents, a trailing newline, and the path logged.
7 */
8
9#include <filesystem>
10#include <fstream>
11#include <stdexcept>
12#include <string>
13
14#include <nlohmann/json.hpp>
15
16#include "io/logger.hpp"
17
18namespace q3 {
19
20inline void write_json_file(const std::string& path, const nlohmann::json& document) {
21 const std::filesystem::path file_path(path);
22 if (file_path.has_parent_path()) {
23 std::filesystem::create_directories(file_path.parent_path());
24 }
25 std::ofstream stream(file_path);
26 if (!stream.is_open()) {
27 throw std::runtime_error("write_json_file: cannot open '" + path + "' for writing");
28 }
29 stream << document.dump(4) << "\n";
30 log::info(" Results written to: {}", path);
31}
32
33} // namespace q3
void info(spdlog::format_string_t< Args... > fmt, Args &&... args)
Definition logger.hpp:183
void write_json_file(const std::string &path, const nlohmann::json &document)
Definition json_file.hpp:20