Commit 42c2294b authored by Alexander Lapshin's avatar Alexander Lapshin

json io

parent 74e024b9
......@@ -554,6 +554,23 @@ template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::stri
ss.AddMember(jname, obj, alc);
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::vector<T>& vec)
{
for (const auto& el : vec) {
PutValue(alc, ss, el);
}
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::vector<T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{
for (auto it = vec.begin(); it != vec.end(); ++it) {
jsonval child(rapidjson::kObjectType);
if (f(alc, child, *it)) {
ss.PushBack(child, alc);
}
}
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::vector<T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{
jsonval jsvec(rapidjson::kArrayType);
......@@ -615,19 +632,6 @@ template <typename T, typename TK> void PutMapAsVec(jsonalloc& alc, jsonval& ss,
ss.AddMember(jname, jsvec, alc);
}
// template <typename T>
// void put_vec_simple(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::vector<T>& vec)
// {
// jsonval jsvec(rapidjson::kArrayType);
// for (const auto& it: vec) {
// jsonval jvalue(it);
// // jsvec.PushBack(jvalue, alc);
// }
//
// jsonval jname(conName, alc);
// ss.AddMember(jname, jsvec, alc);
// }
inline void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::vector<std::string>& vec)
{
jsonval jsvec(rapidjson::kArrayType);
......
......@@ -17,28 +17,7 @@
#define SPACES " \t\r\n"
static std::string ToStr(const int arg, const int leading_zeros)
{
std::ostringstream ss;
if (leading_zeros) {
ss << std::setw(leading_zeros) << std::setfill('0');
}
ss << arg;
return ss.str();
}
static std::string ToStr(const size_t arg, const int leading_zeros)
{
std::ostringstream ss;
if (leading_zeros) {
ss << std::setw(leading_zeros) << std::setfill('0');
}
ss << arg;
return ss.str();
}
template <typename T>
void hangle_to_chars_error(const std::to_chars_result& res, const T& arg)
template <typename T> void hangle_to_chars_error(const std::to_chars_result& res, const T& arg)
{
if (res.ec == std::errc::invalid_argument) {
throw Exp() << "failed to convert value [" << arg << "] to string, invalid argument";
......@@ -51,23 +30,7 @@ void hangle_to_chars_error(const std::to_chars_result& res, const T& arg)
throw Exp() << "failed to convert value [" << arg << "] to string";
}
static std::string ToStr(const double value, const int precision = 0)
{
std::ostringstream ss;
if (precision) {
ss << std::setprecision(precision);
}
else {
ss << std::setprecision(std::numeric_limits<double>::digits10 + 2);
}
ss << value;
return ss.str();
}
template <typename T>
static std::string ToStr(const T arg)
template <typename T> static std::string ToStr(const int arg)
{
std::array<char, 32> buf;
......@@ -97,36 +60,45 @@ static std::string ToStr(const bool arg)
return "";
}
// static std::string ToStr(const size_t arg)
// {
// std::array<char, 32> buf;
//
// const auto res = std::to_chars(buf.data(), buf.data() + buf.size(), arg);
//
// if (res.ec == std::errc()) {
// return std::string(buf.data(), res.ptr - buf.data());
// }
//
// hangle_to_chars_error(res, arg);
//
// return "";
// }
//
// static std::string ToStr(const double arg)
static std::string ToStr(const int arg, const int leading_zeros)
{
if (leading_zeros) {
std::ostringstream ss;
ss << std::setw(leading_zeros) << std::setfill('0');
ss << arg;
return ss.str();
}else {
return ToStr(arg);
}
}
// static std::string ToStr(const size_t arg, const int leading_zeros)
// {
// std::array<char, 32> buf;
//
// const auto res = std::to_chars(buf.data(), buf.data() + buf.size(), arg);
//
// if (res.ec == std::errc()) {
// return std::string(buf.data(), res.ptr - buf.data());
// std::ostringstream ss;
// if (leading_zeros) {
// ss << std::setw(leading_zeros) << std::setfill('0');
// }
//
// hangle_to_chars_error(res, arg);
//
// return "";
// ss << arg;
// return ss.str();
// }
static std::string ToStr(const double value, const int precision)
{
std::ostringstream ss;
if (precision) {
ss << std::setprecision(precision);
}
else {
ss << std::setprecision(std::numeric_limits<double>::digits10 + 2);
}
ss << value;
return ss.str();
}
static void string_replace(std::string& str, const std::string& from, const std::string& to)
{
if (from.empty())
......@@ -156,8 +128,7 @@ static std::string trim_left(const std::string& s, const std::string& t = SPACES
return d.erase(0, s.find_first_not_of(t));
}
template <typename T, typename T2>
bool GetMapItem(const std::map<T, T2>& map, const T& key, T2& ret)
template <typename T, typename T2> bool GetMapItem(const std::map<T, T2>& map, const T& key, T2& ret)
{
typename std::map<T, T2>::const_iterator it = map.find(key);
if (it != map.end()) {
......@@ -169,8 +140,7 @@ bool GetMapItem(const std::map<T, T2>& map, const T& key, T2& ret)
}
}
template <typename T, typename T2>
bool GetMapItem(std::map<T, T2>& map, const T& key, T2*& ret)
template <typename T, typename T2> bool GetMapItem(std::map<T, T2>& map, const T& key, T2*& ret)
{
typename std::map<T, T2>::iterator it = map.find(key);
if (it != map.end()) {
......@@ -183,8 +153,7 @@ bool GetMapItem(std::map<T, T2>& map, const T& key, T2*& ret)
}
}
template <typename T, typename T2>
bool GetMapItem(const std::map<T, T2>& map, const T& key, const T2*& ret)
template <typename T, typename T2> bool GetMapItem(const std::map<T, T2>& map, const T& key, const T2*& ret)
{
typename std::map<T, T2>::const_iterator it = map.find(key);
if (it != map.end()) {
......@@ -197,8 +166,7 @@ bool GetMapItem(const std::map<T, T2>& map, const T& key, const T2*& ret)
}
}
template <typename T, typename T2>
bool GetMapItem(const std::map<T, T2>& map, const T& key)
template <typename T, typename T2> bool GetMapItem(const std::map<T, T2>& map, const T& key)
{
return map.find(key) != map.end();
}
......@@ -208,8 +176,7 @@ static std::string trim(const std::string& s, const std::string& t = SPACES)
return trim_left(trim_right(s, t), t);
}
template <typename ContainerT>
void splitstrf(const std::string& str, ContainerT& tokens, const std::string& delimiters = " ", const bool trimEmpty = false)
template <typename ContainerT> void splitstrf(const std::string& str, ContainerT& tokens, const std::string& delimiters = " ", const bool trimEmpty = false)
{
std::string::size_type lastPos = 0;
while (true) {
......@@ -292,8 +259,7 @@ static bool WriteFile(const std::string& path, const std::string& str)
}
}
template <typename T, typename Tid>
void GetAsMap(const std::vector<T>& vec, std::map<Tid, T>& res)
template <typename T, typename Tid> void GetAsMap(const std::vector<T>& vec, std::map<Tid, T>& res)
{
for (typename std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
res[(*it).GetId()] = *it;
......@@ -414,8 +380,7 @@ static void parseStr(const std::string& data, double& ret)
parseStrA(data, -1, -1, ret, nullptr, nullptr, false);
}
template <typename T, typename T2>
std::vector<T> map_keys(const std::map<T, T2>& map)
template <typename T, typename T2> std::vector<T> map_keys(const std::map<T, T2>& map)
{
std::vector<T> result;
result.reserve(map.size());
......@@ -427,8 +392,7 @@ std::vector<T> map_keys(const std::map<T, T2>& map)
return result;
}
template <typename T>
std::string implode(std::string delim, const std::vector<T>& vec)
template <typename T> std::string implode(std::string delim, const std::vector<T>& vec)
{
std::ostringstream ss;
......@@ -443,8 +407,7 @@ std::string implode(std::string delim, const std::vector<T>& vec)
}
template <typename T>
bool in_array(const std::vector<T>& src, T subject)
template <typename T> bool in_array(const std::vector<T>& src, T subject)
{
return std::find(std::begin(src), std::end(src), subject) != std::end(src);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment