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 ...@@ -554,6 +554,23 @@ template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::stri
ss.AddMember(jname, obj, alc); 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&)) 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); jsonval jsvec(rapidjson::kArrayType);
...@@ -615,19 +632,6 @@ template <typename T, typename TK> void PutMapAsVec(jsonalloc& alc, jsonval& ss, ...@@ -615,19 +632,6 @@ template <typename T, typename TK> void PutMapAsVec(jsonalloc& alc, jsonval& ss,
ss.AddMember(jname, jsvec, alc); 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) inline void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::vector<std::string>& vec)
{ {
jsonval jsvec(rapidjson::kArrayType); jsonval jsvec(rapidjson::kArrayType);
......
...@@ -17,28 +17,7 @@ ...@@ -17,28 +17,7 @@
#define SPACES " \t\r\n" #define SPACES " \t\r\n"
static std::string ToStr(const int arg, const int leading_zeros) template <typename T> void hangle_to_chars_error(const std::to_chars_result& res, const T& arg)
{
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)
{ {
if (res.ec == std::errc::invalid_argument) { if (res.ec == std::errc::invalid_argument) {
throw Exp() << "failed to convert value [" << arg << "] to string, 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) ...@@ -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"; throw Exp() << "failed to convert value [" << arg << "] to string";
} }
static std::string ToStr(const double value, const int precision = 0) template <typename T> static std::string ToStr(const int arg)
{
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)
{ {
std::array<char, 32> buf; std::array<char, 32> buf;
...@@ -97,36 +60,45 @@ static std::string ToStr(const bool arg) ...@@ -97,36 +60,45 @@ static std::string ToStr(const bool arg)
return ""; return "";
} }
// static std::string ToStr(const size_t arg)
// { static std::string ToStr(const int arg, const int leading_zeros)
// std::array<char, 32> buf; {
// if (leading_zeros) {
// const auto res = std::to_chars(buf.data(), buf.data() + buf.size(), arg); std::ostringstream ss;
// ss << std::setw(leading_zeros) << std::setfill('0');
// if (res.ec == std::errc()) { ss << arg;
// return std::string(buf.data(), res.ptr - buf.data()); return ss.str();
// } }else {
// return ToStr(arg);
// hangle_to_chars_error(res, arg); }
//
// return ""; }
// }
// // static std::string ToStr(const size_t arg, const int leading_zeros)
// static std::string ToStr(const double arg)
// { // {
// std::array<char, 32> buf; // std::ostringstream ss;
// // if (leading_zeros) {
// const auto res = std::to_chars(buf.data(), buf.data() + buf.size(), arg); // ss << std::setw(leading_zeros) << std::setfill('0');
//
// if (res.ec == std::errc()) {
// return std::string(buf.data(), res.ptr - buf.data());
// } // }
// // ss << arg;
// hangle_to_chars_error(res, arg); // return ss.str();
//
// return "";
// } // }
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) static void string_replace(std::string& str, const std::string& from, const std::string& to)
{ {
if (from.empty()) if (from.empty())
...@@ -156,8 +128,7 @@ static std::string trim_left(const std::string& s, const std::string& t = SPACES ...@@ -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)); return d.erase(0, s.find_first_not_of(t));
} }
template <typename T, typename T2> template <typename T, typename T2> bool GetMapItem(const std::map<T, T2>& map, const T& key, T2& ret)
bool GetMapItem(const std::map<T, T2>& map, const T& key, T2& ret)
{ {
typename std::map<T, T2>::const_iterator it = map.find(key); typename std::map<T, T2>::const_iterator it = map.find(key);
if (it != map.end()) { if (it != map.end()) {
...@@ -169,8 +140,7 @@ bool GetMapItem(const std::map<T, T2>& map, const T& key, T2& ret) ...@@ -169,8 +140,7 @@ bool GetMapItem(const std::map<T, T2>& map, const T& key, T2& ret)
} }
} }
template <typename T, typename T2> template <typename T, typename T2> bool GetMapItem(std::map<T, T2>& map, const T& key, T2*& ret)
bool GetMapItem(std::map<T, T2>& map, const T& key, T2*& ret)
{ {
typename std::map<T, T2>::iterator it = map.find(key); typename std::map<T, T2>::iterator it = map.find(key);
if (it != map.end()) { if (it != map.end()) {
...@@ -183,8 +153,7 @@ bool GetMapItem(std::map<T, T2>& map, const T& key, T2*& ret) ...@@ -183,8 +153,7 @@ bool GetMapItem(std::map<T, T2>& map, const T& key, T2*& ret)
} }
} }
template <typename T, typename T2> template <typename T, typename T2> bool GetMapItem(const std::map<T, T2>& map, const T& key, const T2*& ret)
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); typename std::map<T, T2>::const_iterator it = map.find(key);
if (it != map.end()) { if (it != map.end()) {
...@@ -197,8 +166,7 @@ bool GetMapItem(const std::map<T, T2>& map, const T& key, const T2*& ret) ...@@ -197,8 +166,7 @@ bool GetMapItem(const std::map<T, T2>& map, const T& key, const T2*& ret)
} }
} }
template <typename T, typename T2> template <typename T, typename T2> bool GetMapItem(const std::map<T, T2>& map, const T& key)
bool GetMapItem(const std::map<T, T2>& map, const T& key)
{ {
return map.find(key) != map.end(); return map.find(key) != map.end();
} }
...@@ -208,8 +176,7 @@ static std::string trim(const std::string& s, const std::string& t = SPACES) ...@@ -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); return trim_left(trim_right(s, t), t);
} }
template <typename ContainerT> template <typename ContainerT> void splitstrf(const std::string& str, ContainerT& tokens, const std::string& delimiters = " ", const bool trimEmpty = false)
void splitstrf(const std::string& str, ContainerT& tokens, const std::string& delimiters = " ", const bool trimEmpty = false)
{ {
std::string::size_type lastPos = 0; std::string::size_type lastPos = 0;
while (true) { while (true) {
...@@ -292,8 +259,7 @@ static bool WriteFile(const std::string& path, const std::string& str) ...@@ -292,8 +259,7 @@ static bool WriteFile(const std::string& path, const std::string& str)
} }
} }
template <typename T, typename Tid> template <typename T, typename Tid> void GetAsMap(const std::vector<T>& vec, std::map<Tid, T>& res)
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) { for (typename std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
res[(*it).GetId()] = *it; res[(*it).GetId()] = *it;
...@@ -414,8 +380,7 @@ static void parseStr(const std::string& data, double& ret) ...@@ -414,8 +380,7 @@ static void parseStr(const std::string& data, double& ret)
parseStrA(data, -1, -1, ret, nullptr, nullptr, false); parseStrA(data, -1, -1, ret, nullptr, nullptr, false);
} }
template <typename T, typename T2> template <typename T, typename T2> std::vector<T> map_keys(const std::map<T, T2>& map)
std::vector<T> map_keys(const std::map<T, T2>& map)
{ {
std::vector<T> result; std::vector<T> result;
result.reserve(map.size()); result.reserve(map.size());
...@@ -427,8 +392,7 @@ std::vector<T> map_keys(const std::map<T, T2>& map) ...@@ -427,8 +392,7 @@ std::vector<T> map_keys(const std::map<T, T2>& map)
return result; return result;
} }
template <typename T> template <typename T> std::string implode(std::string delim, const std::vector<T>& vec)
std::string implode(std::string delim, const std::vector<T>& vec)
{ {
std::ostringstream ss; std::ostringstream ss;
...@@ -443,8 +407,7 @@ std::string implode(std::string delim, const std::vector<T>& vec) ...@@ -443,8 +407,7 @@ std::string implode(std::string delim, const std::vector<T>& vec)
} }
template <typename T> template <typename T> bool in_array(const std::vector<T>& src, T subject)
bool in_array(const std::vector<T>& src, T subject)
{ {
return std::find(std::begin(src), std::end(src), subject) != std::end(src); 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