summaryrefslogtreecommitdiffhomepage
path: root/serialize/json-helper.cpp
blob: 538bcdcb9b0075c5b1bad4698f8237ae194db366 (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, 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