modify json format

This commit is contained in:
zsx 2018-01-24 16:10:19 +08:00
parent 8619d4db42
commit b7b09bc2a6
5 changed files with 97 additions and 15 deletions

View File

@ -4,6 +4,72 @@
#include "acl_cpp/serialize/gson_helper.ipp"
namespace acl
{
acl::json_node& gson(acl::json &$json, const service_base &$obj)
{
acl::json_node &$node = $json.create_node();
if (check_nullptr($obj.status))
$node.add_null("status");
else
$node.add_number("status", acl::get_value($obj.status));
if (check_nullptr($obj.cmd))
$node.add_null("cmd");
else
$node.add_text("cmd", acl::get_value($obj.cmd));
return $node;
}
acl::json_node& gson(acl::json &$json, const service_base *$obj)
{
return gson ($json, *$obj);
}
acl::string gson(const service_base &$obj)
{
acl::json $json;
acl::json_node &$node = acl::gson ($json, $obj);
return $node.to_string ();
}
std::pair<bool,std::string> gson(acl::json_node &$node, service_base &$obj)
{
acl::json_node *status = $node["status"];
acl::json_node *cmd = $node["cmd"];
std::pair<bool, std::string> $result;
if(!status ||!($result = gson(*status, &$obj.status), $result.first))
return std::make_pair(false, "required [service_base.status] failed:{"+$result.second+"}");
if(!cmd ||!($result = gson(*cmd, &$obj.cmd), $result.first))
return std::make_pair(false, "required [service_base.cmd] failed:{"+$result.second+"}");
return std::make_pair(true,"");
}
std::pair<bool,std::string> gson(acl::json_node &$node, service_base *$obj)
{
return gson($node, *$obj);
}
std::pair<bool,std::string> gson(const acl::string &$str, service_base &$obj)
{
acl::json _json;
_json.update($str.c_str());
if (!_json.finish())
{
return std::make_pair(false, "json not finish error");
}
return gson(_json.get_root(), $obj);
}
acl::json_node& gson(acl::json &$json, const service_info_t &$obj)
{
acl::json_node &$node = $json.create_node();
@ -115,10 +181,10 @@ namespace acl
else
$node.add_number("status", acl::get_value($obj.status));
if (check_nullptr($obj.msg))
$node.add_null("msg");
if (check_nullptr($obj.cmd))
$node.add_null("cmd");
else
$node.add_text("msg", acl::get_value($obj.msg));
$node.add_text("cmd", acl::get_value($obj.cmd));
if (check_nullptr($obj.data))
$node.add_null("data");
@ -146,15 +212,15 @@ namespace acl
std::pair<bool,std::string> gson(acl::json_node &$node, service_list_res_t &$obj)
{
acl::json_node *status = $node["status"];
acl::json_node *msg = $node["msg"];
acl::json_node *cmd = $node["cmd"];
acl::json_node *data = $node["data"];
std::pair<bool, std::string> $result;
if(!status ||!($result = gson(*status, &$obj.status), $result.first))
return std::make_pair(false, "required [service_list_res_t.status] failed:{"+$result.second+"}");
if(!msg ||!($result = gson(*msg, &$obj.msg), $result.first))
return std::make_pair(false, "required [service_list_res_t.msg] failed:{"+$result.second+"}");
if(!cmd ||!($result = gson(*cmd, &$obj.cmd), $result.first))
return std::make_pair(false, "required [service_list_res_t.cmd] failed:{"+$result.second+"}");
if(!data ||!data->get_obj()||!($result = gson(*data->get_obj(), &$obj.data), $result.first))
return std::make_pair(false, "required [service_list_res_t.data] failed:{"+$result.second+"}");

View File

@ -1,5 +1,13 @@
namespace acl
{
//service_base
acl::string gson(const service_base &$obj);
acl::json_node& gson(acl::json &$json, const service_base &$obj);
acl::json_node& gson(acl::json &$json, const service_base *$obj);
std::pair<bool,std::string> gson(acl::json_node &$node, service_base &$obj);
std::pair<bool,std::string> gson(acl::json_node &$node, service_base *$obj);
std::pair<bool,std::string> gson(const acl::string &str, service_base &$obj);
//service_info_t
acl::string gson(const service_info_t &$obj);
acl::json_node& gson(acl::json &$json, const service_info_t &$obj);

View File

@ -12,6 +12,12 @@
#pragma once
struct service_base
{
int status;
acl::string cmd;
};
struct service_info_t
{
int status;
@ -24,9 +30,7 @@ struct service_info_t
acl::string version;
};
struct service_list_res_t
struct service_list_res_t : service_base
{
int status;
acl::string msg;
std::vector<service_info_t> data;
};

View File

@ -12,6 +12,12 @@
#pragma once
struct service_base
{
int status;
acl::string cmd;
};
struct service_info_t
{
int status;
@ -24,9 +30,7 @@ struct service_info_t
acl::string version;
};
struct service_list_res_t
struct service_list_res_t : service_base
{
int status;
acl::string msg;
std::vector<service_info_t> data;
};

View File

@ -1,6 +1,6 @@
#include "stdafx.h"
#include "tools/service_guard/json/service_struct.h"
#include "tools/service_guard/json/service_struct.gson.h"
#include "tools/lib_global/json/service_struct.h"
#include "tools/lib_global/json/service_struct.gson.h"
#include "daemon/json/serialize.h"
#include "tools.h"
#include "guard_report.h"
@ -27,7 +27,7 @@ bool service_list::run(void)
service_list_res_t list_res;
list_res.status = 200;
list_res.msg = "+ok";
list_res.cmd = "service_list";
for (std::vector<serv_info_t>::const_iterator cit = res.data.begin();
cit != res.data.end(); ++cit)