summaryrefslogtreecommitdiffhomepage
path: root/serialize/json-helper.cpp
blob: dae3ad96c3f8ecceb9e88995e9720b8aa88ee80f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "json-helper.hpp"
#include <fstream>
#include <filesystem>

namespace floormat {

template<typename T, typename P, std::ios_base::openmode open_mode>
static T open_stream(const std::remove_cvref_t<P>& filename)
{
    T s;
    s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
    s.open(filename, open_mode);
    return s;
}

auto json_helper::from_json_(const fspath& pathname) -> json
{
    json j;
    open_stream<std::ifstream, fspath, std::ios_base::in>(pathname) >> j;
    return j;
}

void json_helper::to_json_(const json& j, const fspath& pathname, int indent)
{
    (open_stream<std::ofstream, fspath, std::ios_base::out>(pathname) << j.dump(indent, '\t') << '\n').flush();
}

} // namespace floormat