2019-07-27 22:44:32 +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>
|
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2018-06-25 10:32:14 +08:00
|
|
|
|
bool deserialize(json& j, T& o, string* err = NULL)
|
2018-06-25 10:18:09 +08:00
|
|
|
|
{
|
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>
|
|
|
|
|
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
|
|
|
|
buf += "\r\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|