50 lines
1.0 KiB
C++
50 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <nlohmann/json.hpp>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
namespace bts::io {
|
|
|
|
class JsonDumper {
|
|
public:
|
|
template <class T>
|
|
static std::string dump(const T &value, int indentation = 2);
|
|
|
|
template <class Iter>
|
|
static std::string dump(
|
|
Iter first,
|
|
Iter last,
|
|
int indentation = 2,
|
|
const char *separator = ",\n");
|
|
};
|
|
|
|
//////////////////////////////////////////////////
|
|
// Definition
|
|
//////////////////////////////////////////////////
|
|
|
|
template <class T>
|
|
std::string JsonDumper::dump(const T &value, int indentation) {
|
|
nlohmann::json j;
|
|
to_json(j, value);
|
|
return j.dump(indentation);
|
|
}
|
|
|
|
template <class Iter>
|
|
std::string JsonDumper::dump(
|
|
Iter first, Iter last, int indentation, const char *separator) {
|
|
if (first == last) {
|
|
return "";
|
|
}
|
|
std::stringstream str;
|
|
auto it = first;
|
|
str << "[" << dump(*it, indentation);
|
|
while (++it != last) {
|
|
str << separator << dump(*it, indentation);
|
|
}
|
|
str << "]";
|
|
return str.str();
|
|
}
|
|
|
|
} // namespace bts::io
|