summaryrefslogtreecommitdiffhomepage
path: root/serialize/json-helper.cpp
blob: 5504ff526c09760632c2fae6ae2fabd4d734593a (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
#include "json-helper.hpp"
#include <fstream>

namespace floormat {

template<typename T, std::ios_base::openmode mode>
static T open_stream(StringView filename)
{
    T s;
    s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
    s.open(filename.data(), mode);
    return s;
}

auto json_helper::from_json_(StringView filename) -> json
{
    json j;
    open_stream<std::ifstream, std::ios_base::in>(filename) >> j;
    return j;
}

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

} // namespace floormat