2019-07-28 10:31:56 +08:00
|
|
|
#pragma once
|
2018-06-25 10:18:09 +08:00
|
|
|
#include "../acl_cpp_define.hpp"
|
|
|
|
#include "../stdlib/string.hpp"
|
|
|
|
#include "../stdlib/json.hpp"
|
|
|
|
#include <string>
|
|
|
|
|
2023-07-03 12:35:25 +08:00
|
|
|
namespace acl {
|
2018-06-25 10:18:09 +08:00
|
|
|
|
|
|
|
template<typename T>
|
2023-07-03 12:35:25 +08:00
|
|
|
bool deserialize(json& j, T& o, string* err = NULL) {
|
2018-06-25 10:32:14 +08:00
|
|
|
if (!j.finish())
|
2018-06-25 10:18:09 +08:00
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
err->append("json not complete yet!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-25 10:32:14 +08:00
|
|
|
std::pair<bool, std::string> r = gson(j.get_root(), o);
|
2018-06-25 10:18:09 +08:00
|
|
|
if (r.first == false)
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
err->format_append("deserialize error=%s, json=[%s]",
|
2018-06-25 10:32:14 +08:00
|
|
|
r.second.c_str(), j.to_string().c_str());
|
2018-06-25 10:18:09 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-07-03 12:35:25 +08:00
|
|
|
void serialize(T& o, string& buf) {
|
2018-06-25 10:32:14 +08:00
|
|
|
json j;
|
|
|
|
json_node& n = gson(j, o);
|
|
|
|
(void) n.to_string(&buf);
|
2018-06-25 10:18:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace acl
|