Loading [MathJax]/jax/input/TeX/config.js
cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
json_reporter.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <fstream>
23#include <string_view>
24#include <utility>
25
26#include <fmt/format.h>
27#include <nlohmann/json.hpp>
28
30#include "stat_bench/reporter/json_data_file_helper.h" // IWYU pragma: keep
33
34namespace stat_bench {
35namespace reporter {
36
37JsonReporter::JsonReporter(std::string file_path)
38 : DataFileReporterBase(std::move(file_path)) {}
39
41 const std::string& file_path, const data_file_spec::RootData& data) {
43 std::ofstream stream{file_path};
44 if (!stream) {
46 fmt::format(FMT_STRING("Failed to open {}."), file_path));
47 }
48
49 auto json_data = nlohmann::json(data);
50 json_data["$schema"] = data_file_schema_url;
51
52 constexpr int indent = 2;
53 constexpr char indent_char = ' ';
54 constexpr bool ensure_ascii = true;
55 stream << json_data.dump(indent, indent_char, ensure_ascii);
56}
57
58} // namespace reporter
59} // namespace stat_bench
Class of exceptions in this library.
DataFileReporterBase(std::string file_path)
Constructor.
void write_data_file(const std::string &file_path, const data_file_spec::RootData &data) override
Write a data file.
JsonReporter(std::string file_path)
Constructor.
Definition of data_file_schema_url constant.
Helper of JSON data files.
Definition of JsonReporter class.
Namespace of reporters of results of benchmarks.
constexpr std::string_view data_file_schema_url
URL of the schema of data files.
void prepare_directory_for(const std::string &path)
Prepare a directory for a file.
Namespace of stat_bench source codes.
STL namespace.
Declaration of prepare_directory function.
Definition of StatBenchException class.
Struct of root objects in data files.