From f1ac0703efde48ebabed248a4f73e811f6bc220c Mon Sep 17 00:00:00 2001 From: fuwangqin <382018309@qq.com> Date: Wed, 14 Jun 2017 13:55:05 +0800 Subject: [PATCH] add set support in Gson. and fix potemtial bug in gson_helper.ipp --- acl.props | 1 + app/gson/demo/Makefile | 2 +- app/gson/demo/demo.vcxproj | 5 +- app/gson/demo/demo.vcxproj.filters | 8 +- app/gson/demo/main.cpp | 12 +- app/gson/demo/{gson.cpp => struct.gson.cpp} | 306 +++++++++++------- app/gson/demo/{gson.h => struct.gson.h} | 6 + app/gson/demo/struct.h | 17 +- app/gson/demo/struct.stub | 64 +++- app/gson/gson.vcxproj | 1 + .../include/acl_cpp/serialize/gson_helper.ipp | 146 +++++++-- .../include/acl_cpp/serialize/gsoner.hpp | 6 +- lib_acl_cpp/src/serialize/gsoner.cpp | 84 ++++- 13 files changed, 499 insertions(+), 159 deletions(-) rename app/gson/demo/{gson.cpp => struct.gson.cpp} (62%) rename app/gson/demo/{gson.h => struct.gson.h} (81%) diff --git a/acl.props b/acl.props index d85ac0ee2..3c8874395 100644 --- a/acl.props +++ b/acl.props @@ -7,6 +7,7 @@ $(ProjectDir)include;$(SolutionDir)lib_acl_cpp\include;$(SolutionDir)lib_acl\include;$(SolutionDir)lib_protocol\include;%(AdditionalIncludeDirectories) MultiThreadedDebug + _CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) $(SolutionDir)lib_acl_cpp;$(SolutionDir)lib_acl;$(SolutionDir)lib_protocol;%(AdditionalLibraryDirectories) diff --git a/app/gson/demo/Makefile b/app/gson/demo/Makefile index a2b79375c..cac6f4a9e 100644 --- a/app/gson/demo/Makefile +++ b/app/gson/demo/Makefile @@ -2,4 +2,4 @@ include ./Makefile.in ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) EXTLIBS += -L/usr/local/lib -liconv endif -PROG = json +PROG = demo diff --git a/app/gson/demo/demo.vcxproj b/app/gson/demo/demo.vcxproj index c32821995..4f321c9f5 100644 --- a/app/gson/demo/demo.vcxproj +++ b/app/gson/demo/demo.vcxproj @@ -63,6 +63,7 @@ + @@ -116,14 +117,14 @@ - + - + diff --git a/app/gson/demo/demo.vcxproj.filters b/app/gson/demo/demo.vcxproj.filters index 1e3e2d098..4ba41f55b 100644 --- a/app/gson/demo/demo.vcxproj.filters +++ b/app/gson/demo/demo.vcxproj.filters @@ -15,15 +15,15 @@ - - 头文件 - 头文件 头文件 + + 头文件 + @@ -32,7 +32,7 @@ 源文件 - + 源文件 diff --git a/app/gson/demo/main.cpp b/app/gson/demo/main.cpp index 4eadd0d97..e0e714414 100644 --- a/app/gson/demo/main.cpp +++ b/app/gson/demo/main.cpp @@ -5,7 +5,7 @@ #include #include #include "struct.h" -#include "gson.h" +#include "struct.gson.h" void test_base() { @@ -52,6 +52,16 @@ void test_base() obj.base_list_map.insert( std::make_pair("base", obj.bases_list)); obj.vector_list_base.push_back(obj.bases_list); + //set + obj.str_set_.insert("hello1"); + obj.str_set_.insert("hello2"); + obj.str_set_.insert("hello3"); + obj.bool_set_.insert(true); + obj.bool_set_.insert(false); + obj.int_set_.insert(1); + obj.int_set_.insert(11); + obj.int_set_.insert(111); + acl::json json; acl::json_node &node = acl::gson(json, obj); printf("%s\n\n", node.to_string().c_str()); diff --git a/app/gson/demo/gson.cpp b/app/gson/demo/struct.gson.cpp similarity index 62% rename from app/gson/demo/gson.cpp rename to app/gson/demo/struct.gson.cpp index d7323b71d..b58e88c70 100644 --- a/app/gson/demo/gson.cpp +++ b/app/gson/demo/struct.gson.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "struct.h" -#include "gson.h" +#include "struct.gson.h" #include "acl_cpp/serialize/gson_helper.ipp" namespace acl { @@ -160,73 +160,73 @@ namespace acl acl::json_node *h_ptr = $node["h_ptr"]; acl::json_node *i = $node["i"]; acl::json_node *i_ptr = $node["i_ptr"]; - std::pair result; + std::pair $result; - if(!string ||!(result = gson(*string, &$obj.string), result.first)) - return std::make_pair(false, "required [base.string] failed:{"+result.second+"}"); + if(!string ||!($result = gson(*string, &$obj.string), $result.first)) + return std::make_pair(false, "required [base.string] failed:{"+$result.second+"}"); if(string_ptr) gson(*string_ptr, &$obj.string_ptr); - if(!a ||!(result = gson(*a, &$obj.a), result.first)) - return std::make_pair(false, "required [base.a] failed:{"+result.second+"}"); + if(!a ||!($result = gson(*a, &$obj.a), $result.first)) + return std::make_pair(false, "required [base.a] failed:{"+$result.second+"}"); - if(!a_ptr ||!(result = gson(*a_ptr, &$obj.a_ptr), result.first)) - return std::make_pair(false, "required [base.a_ptr] failed:{"+result.second+"}"); + if(!a_ptr ||!($result = gson(*a_ptr, &$obj.a_ptr), $result.first)) + return std::make_pair(false, "required [base.a_ptr] failed:{"+$result.second+"}"); - if(!b ||!(result = gson(*b, &$obj.b), result.first)) - return std::make_pair(false, "required [base.b] failed:{"+result.second+"}"); + if(!b ||!($result = gson(*b, &$obj.b), $result.first)) + return std::make_pair(false, "required [base.b] failed:{"+$result.second+"}"); - if(!b_ptr ||!(result = gson(*b_ptr, &$obj.b_ptr), result.first)) - return std::make_pair(false, "required [base.b_ptr] failed:{"+result.second+"}"); + if(!b_ptr ||!($result = gson(*b_ptr, &$obj.b_ptr), $result.first)) + return std::make_pair(false, "required [base.b_ptr] failed:{"+$result.second+"}"); - if(!c ||!(result = gson(*c, &$obj.c), result.first)) - return std::make_pair(false, "required [base.c] failed:{"+result.second+"}"); + if(!c ||!($result = gson(*c, &$obj.c), $result.first)) + return std::make_pair(false, "required [base.c] failed:{"+$result.second+"}"); - if(!c_ptr ||!(result = gson(*c_ptr, &$obj.c_ptr), result.first)) - return std::make_pair(false, "required [base.c_ptr] failed:{"+result.second+"}"); + if(!c_ptr ||!($result = gson(*c_ptr, &$obj.c_ptr), $result.first)) + return std::make_pair(false, "required [base.c_ptr] failed:{"+$result.second+"}"); - if(!d ||!(result = gson(*d, &$obj.d), result.first)) - return std::make_pair(false, "required [base.d] failed:{"+result.second+"}"); + if(!d ||!($result = gson(*d, &$obj.d), $result.first)) + return std::make_pair(false, "required [base.d] failed:{"+$result.second+"}"); - if(!d_ptr ||!(result = gson(*d_ptr, &$obj.d_ptr), result.first)) - return std::make_pair(false, "required [base.d_ptr] failed:{"+result.second+"}"); + if(!d_ptr ||!($result = gson(*d_ptr, &$obj.d_ptr), $result.first)) + return std::make_pair(false, "required [base.d_ptr] failed:{"+$result.second+"}"); - if(!e ||!(result = gson(*e, &$obj.e), result.first)) - return std::make_pair(false, "required [base.e] failed:{"+result.second+"}"); + if(!e ||!($result = gson(*e, &$obj.e), $result.first)) + return std::make_pair(false, "required [base.e] failed:{"+$result.second+"}"); - if(!e_ptr ||!(result = gson(*e_ptr, &$obj.e_ptr), result.first)) - return std::make_pair(false, "required [base.e_ptr] failed:{"+result.second+"}"); + if(!e_ptr ||!($result = gson(*e_ptr, &$obj.e_ptr), $result.first)) + return std::make_pair(false, "required [base.e_ptr] failed:{"+$result.second+"}"); - if(!f ||!(result = gson(*f, &$obj.f), result.first)) - return std::make_pair(false, "required [base.f] failed:{"+result.second+"}"); + if(!f ||!($result = gson(*f, &$obj.f), $result.first)) + return std::make_pair(false, "required [base.f] failed:{"+$result.second+"}"); - if(!f_ptr ||!(result = gson(*f_ptr, &$obj.f_ptr), result.first)) - return std::make_pair(false, "required [base.f_ptr] failed:{"+result.second+"}"); + if(!f_ptr ||!($result = gson(*f_ptr, &$obj.f_ptr), $result.first)) + return std::make_pair(false, "required [base.f_ptr] failed:{"+$result.second+"}"); - if(!g ||!(result = gson(*g, &$obj.g), result.first)) - return std::make_pair(false, "required [base.g] failed:{"+result.second+"}"); + if(!g ||!($result = gson(*g, &$obj.g), $result.first)) + return std::make_pair(false, "required [base.g] failed:{"+$result.second+"}"); - if(!g_ptr ||!(result = gson(*g_ptr, &$obj.g_ptr), result.first)) - return std::make_pair(false, "required [base.g_ptr] failed:{"+result.second+"}"); + if(!g_ptr ||!($result = gson(*g_ptr, &$obj.g_ptr), $result.first)) + return std::make_pair(false, "required [base.g_ptr] failed:{"+$result.second+"}"); - if(!acl_string ||!(result = gson(*acl_string, &$obj.acl_string), result.first)) - return std::make_pair(false, "required [base.acl_string] failed:{"+result.second+"}"); + if(!acl_string ||!($result = gson(*acl_string, &$obj.acl_string), $result.first)) + return std::make_pair(false, "required [base.acl_string] failed:{"+$result.second+"}"); - if(!acl_string_ptr ||!(result = gson(*acl_string_ptr, &$obj.acl_string_ptr), result.first)) - return std::make_pair(false, "required [base.acl_string_ptr] failed:{"+result.second+"}"); + if(!acl_string_ptr ||!($result = gson(*acl_string_ptr, &$obj.acl_string_ptr), $result.first)) + return std::make_pair(false, "required [base.acl_string_ptr] failed:{"+$result.second+"}"); - if(!h ||!(result = gson(*h, &$obj.h), result.first)) - return std::make_pair(false, "required [base.h] failed:{"+result.second+"}"); + if(!h ||!($result = gson(*h, &$obj.h), $result.first)) + return std::make_pair(false, "required [base.h] failed:{"+$result.second+"}"); - if(!h_ptr ||!(result = gson(*h_ptr, &$obj.h_ptr), result.first)) - return std::make_pair(false, "required [base.h_ptr] failed:{"+result.second+"}"); + if(!h_ptr ||!($result = gson(*h_ptr, &$obj.h_ptr), $result.first)) + return std::make_pair(false, "required [base.h_ptr] failed:{"+$result.second+"}"); - if(!i ||!(result = gson(*i, &$obj.i), result.first)) - return std::make_pair(false, "required [base.i] failed:{"+result.second+"}"); + if(!i ||!($result = gson(*i, &$obj.i), $result.first)) + return std::make_pair(false, "required [base.i] failed:{"+$result.second+"}"); - if(!i_ptr ||!(result = gson(*i_ptr, &$obj.i_ptr), result.first)) - return std::make_pair(false, "required [base.i_ptr] failed:{"+result.second+"}"); + if(!i_ptr ||!($result = gson(*i_ptr, &$obj.i_ptr), $result.first)) + return std::make_pair(false, "required [base.i_ptr] failed:{"+$result.second+"}"); return std::make_pair(true,""); } @@ -238,6 +238,18 @@ namespace acl } + std::pair gson(const acl::string &$str, 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 hello::world &$obj) { acl::json_node &$node = $json.create_node(); @@ -267,6 +279,16 @@ namespace acl else $node.add_child("bases_ptr_list_ptr", acl::gson($json, $obj.bases_ptr_list_ptr)); + if (check_nullptr($obj.vector_string)) + $node.add_null("vector_string"); + else + $node.add_child("vector_string", acl::gson($json, $obj.vector_string)); + + if (check_nullptr($obj.vector_list_base)) + $node.add_null("vector_list_base"); + else + $node.add_child("vector_list_base", acl::gson($json, $obj.vector_list_base)); + if (check_nullptr($obj.base_map)) $node.add_null("base_map"); else @@ -292,15 +314,20 @@ namespace acl else $node.add_child("base_list_map", acl::gson($json, $obj.base_list_map)); - if (check_nullptr($obj.vector_string)) - $node.add_null("vector_string"); + if (check_nullptr($obj.str_set_)) + $node.add_null("str_set_"); else - $node.add_child("vector_string", acl::gson($json, $obj.vector_string)); + $node.add_child("str_set_", acl::gson($json, $obj.str_set_)); - if (check_nullptr($obj.vector_list_base)) - $node.add_null("vector_list_base"); + if (check_nullptr($obj.int_set_)) + $node.add_null("int_set_"); else - $node.add_child("vector_list_base", acl::gson($json, $obj.vector_list_base)); + $node.add_child("int_set_", acl::gson($json, $obj.int_set_)); + + if (check_nullptr($obj.bool_set_)) + $node.add_null("bool_set_"); + else + $node.add_child("bool_set_", acl::gson($json, $obj.bool_set_)); if (check_nullptr($obj.me)) $node.add_null("me"); @@ -332,54 +359,66 @@ namespace acl acl::json_node *bases_list = $node["bases_list"]; acl::json_node *bases_list_ptr = $node["bases_list_ptr"]; acl::json_node *bases_ptr_list_ptr = $node["bases_ptr_list_ptr"]; + acl::json_node *vector_string = $node["vector_string"]; + acl::json_node *vector_list_base = $node["vector_list_base"]; acl::json_node *base_map = $node["base_map"]; acl::json_node *string_map = $node["string_map"]; acl::json_node *int_map = $node["int_map"]; acl::json_node *bool_map = $node["bool_map"]; acl::json_node *base_list_map = $node["base_list_map"]; - acl::json_node *vector_string = $node["vector_string"]; - acl::json_node *vector_list_base = $node["vector_list_base"]; + acl::json_node *str_set_ = $node["str_set_"]; + acl::json_node *int_set_ = $node["int_set_"]; + acl::json_node *bool_set_ = $node["bool_set_"]; acl::json_node *me = $node["me"]; - std::pair result; + std::pair $result; - if(!b ||!b->get_obj()||!(result = gson(*b->get_obj(), &$obj.b), result.first)) - return std::make_pair(false, "required [hello::world.b] failed:{"+result.second+"}"); + if(!b ||!b->get_obj()||!($result = gson(*b->get_obj(), &$obj.b), $result.first)) + return std::make_pair(false, "required [hello::world.b] failed:{"+$result.second+"}"); - if(!b_ptr ||!b_ptr->get_obj()||!(result = gson(*b_ptr->get_obj(), &$obj.b_ptr), result.first)) - return std::make_pair(false, "required [hello::world.b_ptr] failed:{"+result.second+"}"); + if(!b_ptr ||!b_ptr->get_obj()||!($result = gson(*b_ptr->get_obj(), &$obj.b_ptr), $result.first)) + return std::make_pair(false, "required [hello::world.b_ptr] failed:{"+$result.second+"}"); - if(!bases_list ||!bases_list->get_obj()||!(result = gson(*bases_list->get_obj(), &$obj.bases_list), result.first)) - return std::make_pair(false, "required [hello::world.bases_list] failed:{"+result.second+"}"); + if(!bases_list ||!bases_list->get_obj()||!($result = gson(*bases_list->get_obj(), &$obj.bases_list), $result.first)) + return std::make_pair(false, "required [hello::world.bases_list] failed:{"+$result.second+"}"); - if(!bases_list_ptr ||!bases_list_ptr->get_obj()||!(result = gson(*bases_list_ptr->get_obj(), &$obj.bases_list_ptr), result.first)) - return std::make_pair(false, "required [hello::world.bases_list_ptr] failed:{"+result.second+"}"); + if(!bases_list_ptr ||!bases_list_ptr->get_obj()||!($result = gson(*bases_list_ptr->get_obj(), &$obj.bases_list_ptr), $result.first)) + return std::make_pair(false, "required [hello::world.bases_list_ptr] failed:{"+$result.second+"}"); if(bases_ptr_list_ptr&& bases_ptr_list_ptr->get_obj()) gson(*bases_ptr_list_ptr->get_obj(), &$obj.bases_ptr_list_ptr); - if(!base_map ||!base_map->get_obj()||!(result = gson(*base_map->get_obj(), &$obj.base_map), result.first)) - return std::make_pair(false, "required [hello::world.base_map] failed:{"+result.second+"}"); + if(!vector_string ||!vector_string->get_obj()||!($result = gson(*vector_string->get_obj(), &$obj.vector_string), $result.first)) + return std::make_pair(false, "required [hello::world.vector_string] failed:{"+$result.second+"}"); - if(!string_map ||!string_map->get_obj()||!(result = gson(*string_map->get_obj(), &$obj.string_map), result.first)) - return std::make_pair(false, "required [hello::world.string_map] failed:{"+result.second+"}"); + if(!vector_list_base ||!vector_list_base->get_obj()||!($result = gson(*vector_list_base->get_obj(), &$obj.vector_list_base), $result.first)) + return std::make_pair(false, "required [hello::world.vector_list_base] failed:{"+$result.second+"}"); - if(!int_map ||!int_map->get_obj()||!(result = gson(*int_map->get_obj(), &$obj.int_map), result.first)) - return std::make_pair(false, "required [hello::world.int_map] failed:{"+result.second+"}"); + if(!base_map ||!base_map->get_obj()||!($result = gson(*base_map->get_obj(), &$obj.base_map), $result.first)) + return std::make_pair(false, "required [hello::world.base_map] failed:{"+$result.second+"}"); - if(!bool_map ||!bool_map->get_obj()||!(result = gson(*bool_map->get_obj(), &$obj.bool_map), result.first)) - return std::make_pair(false, "required [hello::world.bool_map] failed:{"+result.second+"}"); + if(!string_map ||!string_map->get_obj()||!($result = gson(*string_map->get_obj(), &$obj.string_map), $result.first)) + return std::make_pair(false, "required [hello::world.string_map] failed:{"+$result.second+"}"); - if(!base_list_map ||!base_list_map->get_obj()||!(result = gson(*base_list_map->get_obj(), &$obj.base_list_map), result.first)) - return std::make_pair(false, "required [hello::world.base_list_map] failed:{"+result.second+"}"); + if(!int_map ||!int_map->get_obj()||!($result = gson(*int_map->get_obj(), &$obj.int_map), $result.first)) + return std::make_pair(false, "required [hello::world.int_map] failed:{"+$result.second+"}"); - if(!vector_string ||!vector_string->get_obj()||!(result = gson(*vector_string->get_obj(), &$obj.vector_string), result.first)) - return std::make_pair(false, "required [hello::world.vector_string] failed:{"+result.second+"}"); + if(!bool_map ||!bool_map->get_obj()||!($result = gson(*bool_map->get_obj(), &$obj.bool_map), $result.first)) + return std::make_pair(false, "required [hello::world.bool_map] failed:{"+$result.second+"}"); - if(!vector_list_base ||!vector_list_base->get_obj()||!(result = gson(*vector_list_base->get_obj(), &$obj.vector_list_base), result.first)) - return std::make_pair(false, "required [hello::world.vector_list_base] failed:{"+result.second+"}"); + if(!base_list_map ||!base_list_map->get_obj()||!($result = gson(*base_list_map->get_obj(), &$obj.base_list_map), $result.first)) + return std::make_pair(false, "required [hello::world.base_list_map] failed:{"+$result.second+"}"); - if(!me ||!(result = gson(*me, &$obj.me), result.first)) - return std::make_pair(false, "required [hello::world.me] failed:{"+result.second+"}"); + if(!str_set_ ||!str_set_->get_obj()||!($result = gson(*str_set_->get_obj(), &$obj.str_set_), $result.first)) + return std::make_pair(false, "required [hello::world.str_set_] failed:{"+$result.second+"}"); + + if(!int_set_ ||!int_set_->get_obj()||!($result = gson(*int_set_->get_obj(), &$obj.int_set_), $result.first)) + return std::make_pair(false, "required [hello::world.int_set_] failed:{"+$result.second+"}"); + + if(!bool_set_ ||!bool_set_->get_obj()||!($result = gson(*bool_set_->get_obj(), &$obj.bool_set_), $result.first)) + return std::make_pair(false, "required [hello::world.bool_set_] failed:{"+$result.second+"}"); + + if(!me ||!($result = gson(*me, &$obj.me), $result.first)) + return std::make_pair(false, "required [hello::world.me] failed:{"+$result.second+"}"); return std::make_pair(true,""); } @@ -391,6 +430,18 @@ namespace acl } + std::pair gson(const acl::string &$str, hello::world &$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 list1 &$obj) { acl::json_node &$node = $json.create_node(); @@ -420,6 +471,16 @@ namespace acl else $node.add_child("bases_ptr_list_ptr", acl::gson($json, $obj.bases_ptr_list_ptr)); + if (check_nullptr($obj.vector_string)) + $node.add_null("vector_string"); + else + $node.add_child("vector_string", acl::gson($json, $obj.vector_string)); + + if (check_nullptr($obj.vector_list_base)) + $node.add_null("vector_list_base"); + else + $node.add_child("vector_list_base", acl::gson($json, $obj.vector_list_base)); + if (check_nullptr($obj.base_map)) $node.add_null("base_map"); else @@ -445,15 +506,20 @@ namespace acl else $node.add_child("base_list_map", acl::gson($json, $obj.base_list_map)); - if (check_nullptr($obj.vector_string)) - $node.add_null("vector_string"); + if (check_nullptr($obj.str_set_)) + $node.add_null("str_set_"); else - $node.add_child("vector_string", acl::gson($json, $obj.vector_string)); + $node.add_child("str_set_", acl::gson($json, $obj.str_set_)); - if (check_nullptr($obj.vector_list_base)) - $node.add_null("vector_list_base"); + if (check_nullptr($obj.int_set_)) + $node.add_null("int_set_"); else - $node.add_child("vector_list_base", acl::gson($json, $obj.vector_list_base)); + $node.add_child("int_set_", acl::gson($json, $obj.int_set_)); + + if (check_nullptr($obj.bool_set_)) + $node.add_null("bool_set_"); + else + $node.add_child("bool_set_", acl::gson($json, $obj.bool_set_)); return $node; @@ -480,50 +546,62 @@ namespace acl acl::json_node *bases_list = $node["bases_list"]; acl::json_node *bases_list_ptr = $node["bases_list_ptr"]; acl::json_node *bases_ptr_list_ptr = $node["bases_ptr_list_ptr"]; + acl::json_node *vector_string = $node["vector_string"]; + acl::json_node *vector_list_base = $node["vector_list_base"]; acl::json_node *base_map = $node["base_map"]; acl::json_node *string_map = $node["string_map"]; acl::json_node *int_map = $node["int_map"]; acl::json_node *bool_map = $node["bool_map"]; acl::json_node *base_list_map = $node["base_list_map"]; - acl::json_node *vector_string = $node["vector_string"]; - acl::json_node *vector_list_base = $node["vector_list_base"]; - std::pair result; + acl::json_node *str_set_ = $node["str_set_"]; + acl::json_node *int_set_ = $node["int_set_"]; + acl::json_node *bool_set_ = $node["bool_set_"]; + std::pair $result; - if(!b ||!b->get_obj()||!(result = gson(*b->get_obj(), &$obj.b), result.first)) - return std::make_pair(false, "required [list1.b] failed:{"+result.second+"}"); + if(!b ||!b->get_obj()||!($result = gson(*b->get_obj(), &$obj.b), $result.first)) + return std::make_pair(false, "required [list1.b] failed:{"+$result.second+"}"); - if(!b_ptr ||!b_ptr->get_obj()||!(result = gson(*b_ptr->get_obj(), &$obj.b_ptr), result.first)) - return std::make_pair(false, "required [list1.b_ptr] failed:{"+result.second+"}"); + if(!b_ptr ||!b_ptr->get_obj()||!($result = gson(*b_ptr->get_obj(), &$obj.b_ptr), $result.first)) + return std::make_pair(false, "required [list1.b_ptr] failed:{"+$result.second+"}"); - if(!bases_list ||!bases_list->get_obj()||!(result = gson(*bases_list->get_obj(), &$obj.bases_list), result.first)) - return std::make_pair(false, "required [list1.bases_list] failed:{"+result.second+"}"); + if(!bases_list ||!bases_list->get_obj()||!($result = gson(*bases_list->get_obj(), &$obj.bases_list), $result.first)) + return std::make_pair(false, "required [list1.bases_list] failed:{"+$result.second+"}"); - if(!bases_list_ptr ||!bases_list_ptr->get_obj()||!(result = gson(*bases_list_ptr->get_obj(), &$obj.bases_list_ptr), result.first)) - return std::make_pair(false, "required [list1.bases_list_ptr] failed:{"+result.second+"}"); + if(!bases_list_ptr ||!bases_list_ptr->get_obj()||!($result = gson(*bases_list_ptr->get_obj(), &$obj.bases_list_ptr), $result.first)) + return std::make_pair(false, "required [list1.bases_list_ptr] failed:{"+$result.second+"}"); if(bases_ptr_list_ptr&& bases_ptr_list_ptr->get_obj()) gson(*bases_ptr_list_ptr->get_obj(), &$obj.bases_ptr_list_ptr); - if(!base_map ||!base_map->get_obj()||!(result = gson(*base_map->get_obj(), &$obj.base_map), result.first)) - return std::make_pair(false, "required [list1.base_map] failed:{"+result.second+"}"); + if(!vector_string ||!vector_string->get_obj()||!($result = gson(*vector_string->get_obj(), &$obj.vector_string), $result.first)) + return std::make_pair(false, "required [list1.vector_string] failed:{"+$result.second+"}"); - if(!string_map ||!string_map->get_obj()||!(result = gson(*string_map->get_obj(), &$obj.string_map), result.first)) - return std::make_pair(false, "required [list1.string_map] failed:{"+result.second+"}"); + if(!vector_list_base ||!vector_list_base->get_obj()||!($result = gson(*vector_list_base->get_obj(), &$obj.vector_list_base), $result.first)) + return std::make_pair(false, "required [list1.vector_list_base] failed:{"+$result.second+"}"); - if(!int_map ||!int_map->get_obj()||!(result = gson(*int_map->get_obj(), &$obj.int_map), result.first)) - return std::make_pair(false, "required [list1.int_map] failed:{"+result.second+"}"); + if(!base_map ||!base_map->get_obj()||!($result = gson(*base_map->get_obj(), &$obj.base_map), $result.first)) + return std::make_pair(false, "required [list1.base_map] failed:{"+$result.second+"}"); - if(!bool_map ||!bool_map->get_obj()||!(result = gson(*bool_map->get_obj(), &$obj.bool_map), result.first)) - return std::make_pair(false, "required [list1.bool_map] failed:{"+result.second+"}"); + if(!string_map ||!string_map->get_obj()||!($result = gson(*string_map->get_obj(), &$obj.string_map), $result.first)) + return std::make_pair(false, "required [list1.string_map] failed:{"+$result.second+"}"); - if(!base_list_map ||!base_list_map->get_obj()||!(result = gson(*base_list_map->get_obj(), &$obj.base_list_map), result.first)) - return std::make_pair(false, "required [list1.base_list_map] failed:{"+result.second+"}"); + if(!int_map ||!int_map->get_obj()||!($result = gson(*int_map->get_obj(), &$obj.int_map), $result.first)) + return std::make_pair(false, "required [list1.int_map] failed:{"+$result.second+"}"); - if(!vector_string ||!vector_string->get_obj()||!(result = gson(*vector_string->get_obj(), &$obj.vector_string), result.first)) - return std::make_pair(false, "required [list1.vector_string] failed:{"+result.second+"}"); + if(!bool_map ||!bool_map->get_obj()||!($result = gson(*bool_map->get_obj(), &$obj.bool_map), $result.first)) + return std::make_pair(false, "required [list1.bool_map] failed:{"+$result.second+"}"); - if(!vector_list_base ||!vector_list_base->get_obj()||!(result = gson(*vector_list_base->get_obj(), &$obj.vector_list_base), result.first)) - return std::make_pair(false, "required [list1.vector_list_base] failed:{"+result.second+"}"); + if(!base_list_map ||!base_list_map->get_obj()||!($result = gson(*base_list_map->get_obj(), &$obj.base_list_map), $result.first)) + return std::make_pair(false, "required [list1.base_list_map] failed:{"+$result.second+"}"); + + if(!str_set_ ||!str_set_->get_obj()||!($result = gson(*str_set_->get_obj(), &$obj.str_set_), $result.first)) + return std::make_pair(false, "required [list1.str_set_] failed:{"+$result.second+"}"); + + if(!int_set_ ||!int_set_->get_obj()||!($result = gson(*int_set_->get_obj(), &$obj.int_set_), $result.first)) + return std::make_pair(false, "required [list1.int_set_] failed:{"+$result.second+"}"); + + if(!bool_set_ ||!bool_set_->get_obj()||!($result = gson(*bool_set_->get_obj(), &$obj.bool_set_), $result.first)) + return std::make_pair(false, "required [list1.bool_set_] failed:{"+$result.second+"}"); return std::make_pair(true,""); } @@ -535,4 +613,16 @@ namespace acl } + std::pair gson(const acl::string &$str, list1 &$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); + } + + }///end of acl. diff --git a/app/gson/demo/gson.h b/app/gson/demo/struct.gson.h similarity index 81% rename from app/gson/demo/gson.h rename to app/gson/demo/struct.gson.h index 25baac05f..b9ef7cb48 100644 --- a/app/gson/demo/gson.h +++ b/app/gson/demo/struct.gson.h @@ -6,16 +6,22 @@ namespace acl acl::json_node& gson(acl::json &$json, const base *$obj); std::pair gson(acl::json_node &$node, base &$obj); std::pair gson(acl::json_node &$node, base *$obj); + std::pair gson(const acl::string &str, base &$obj); + //hello::world acl::string gson(const hello::world &$obj); acl::json_node& gson(acl::json &$json, const hello::world &$obj); acl::json_node& gson(acl::json &$json, const hello::world *$obj); std::pair gson(acl::json_node &$node, hello::world &$obj); std::pair gson(acl::json_node &$node, hello::world *$obj); + std::pair gson(const acl::string &str, hello::world &$obj); + //list1 acl::string gson(const list1 &$obj); acl::json_node& gson(acl::json &$json, const list1 &$obj); acl::json_node& gson(acl::json &$json, const list1 *$obj); std::pair gson(acl::json_node &$node, list1 &$obj); std::pair gson(acl::json_node &$node, list1 *$obj); + std::pair gson(const acl::string &str, list1 &$obj); + }///end of acl. diff --git a/app/gson/demo/struct.h b/app/gson/demo/struct.h index cd68c2339..33e648a46 100644 --- a/app/gson/demo/struct.h +++ b/app/gson/demo/struct.h @@ -1,4 +1,5 @@ #pragma once +#include struct base { @@ -37,13 +38,19 @@ struct list1 std::list *bases_list_ptr; //Gson@optional std::list *bases_ptr_list_ptr; - std::map base_map; - std::map string_map; + std::vector vector_string; + std::vector > vector_list_base; + + std::map base_map; + std::map string_map; std::map int_map; std::map bool_map; - std::map > base_list_map; - std::vector vector_string; - std::vector > vector_list_base; + std::map > base_list_map; + + //set + std::set str_set_; + std::set int_set_; + std::set bool_set_; }; namespace hello diff --git a/app/gson/demo/struct.stub b/app/gson/demo/struct.stub index 448817592..01270dd61 100644 --- a/app/gson/demo/struct.stub +++ b/app/gson/demo/struct.stub @@ -1,4 +1,66 @@ -#pragma once +#pragma once#pragma once +#include + +struct base +{ + std::string string ; + //Gson@optional + std::string *string_ptr ; + int a ; + int *a_ptr ; + unsigned int b ; + unsigned int *b_ptr; + int64_t c ; + int64_t *c_ptr ; + unsigned long d; + unsigned long *d_ptr ; + unsigned long long e ; + unsigned long long *e_ptr; + long f ; + long *f_ptr ; + long long g; + + long long *g_ptr ; + acl::string acl_string; + acl::string *acl_string_ptr; + + float h; + float *h_ptr; + double i; + double *i_ptr; +}; + +struct list1 +{ + base b; + base *b_ptr; + std::list bases_list; + std::list *bases_list_ptr; + //Gson@optional + std::list *bases_ptr_list_ptr; + std::vector vector_string; + std::vector > vector_list_base; + + std::map base_map; + std::map string_map; + std::map int_map; + std::map bool_map; + std::map > base_list_map; + + //set + std::set str_set_; + std::set int_set_; + std::set bool_set_; +}; + +namespace hello +{ +struct world : list1 +{ +int me; +}; +} + struct base { diff --git a/app/gson/gson.vcxproj b/app/gson/gson.vcxproj index 8d9e5cff4..6e47dcd76 100644 --- a/app/gson/gson.vcxproj +++ b/app/gson/gson.vcxproj @@ -63,6 +63,7 @@ + diff --git a/lib_acl_cpp/include/acl_cpp/serialize/gson_helper.ipp b/lib_acl_cpp/include/acl_cpp/serialize/gson_helper.ipp index 48907537d..a6c499916 100644 --- a/lib_acl_cpp/include/acl_cpp/serialize/gson_helper.ipp +++ b/lib_acl_cpp/include/acl_cpp/serialize/gson_helper.ipp @@ -15,7 +15,7 @@ #include "../stdlib/string.hpp" #include "../stdlib/json.hpp" #include "../stdlib/string.hpp" - +#include namespace acl { @@ -431,7 +431,7 @@ static inline void add_item(acl::json &, acl::json_node &node, char *value) else node.add_array_text(value); } - +//list template static inline acl::json_node &gson(acl::json &json, const std::list &objects) @@ -445,7 +445,6 @@ static inline acl::json_node &gson(acl::json &json, return node; } - template static inline acl::json_node &gson(acl::json &json, const std::list *objects) @@ -453,6 +452,7 @@ static inline acl::json_node &gson(acl::json &json, return gson(json, *objects); } +//vector template static inline acl::json_node &gson(acl::json &json, const std::vector &objects) @@ -473,6 +473,26 @@ static inline acl::json_node &gson(acl::json &json, { return gson(json, *objects); } +//set +template +static inline acl::json_node &gson(acl::json &json, const std::set *objects) +{ + return gson(json, *objects); +} + +template +static inline acl::json_node &gson(acl::json &json, + const std::set &objects) +{ + acl::json_node &node = json.create_array(); + for (typename std::set::const_iterator + itr = objects.begin(); itr != objects.end(); ++itr) + { + add_item(json, node, *itr); + } + + return node; +} //define number map template @@ -860,7 +880,6 @@ static inline std::pair gson(acl::json_node &node, std::list *objs) { std::pair result; - std::string error_string; acl::json_node *itr = node.first_child(); while (itr) @@ -873,16 +892,18 @@ gson(acl::json_node &node, std::list *objs) objs->push_back(obj); typename std::list::iterator it = objs->end(); --it; - result = gson(*itr, *it); + result = gson(*itr, &*it); if (!result.first) { - error_string.append(result.second); - objs->erase(it); + break; } itr = node.next_child(); } - - return std::make_pair(!!!objs->empty(), error_string); + if (result.first) + return result; + + objs->clear(); + return result; } // list @@ -891,28 +912,25 @@ static inline std::pair gson(acl::json_node &node, std::list *objs) { std::pair result; - std::string error_string; acl::json_node *itr = node.first_child(); while (itr) { - // for avoiding object's member pointor copy - // ---lindawei - T* obj = new T; objs->push_back(obj); - typename std::list::iterator it = objs->end(); - --it; - result = gson(*itr, *it); + result = gson(*itr, obj); if (!result.first) { - error_string.append(result.second); - objs->erase(it); + break; } itr = node.next_child(); } - - return std::make_pair(!!!objs->empty(), error_string); + if(objs->size()) + { + delete *objs->begin(); + objs->pop_front(); + } + return result; } // vector @@ -922,7 +940,6 @@ static inline gson(acl::json_node &node, std::vector *objs) { std::pair result; acl::json_node *itr = node.first_child(); - std::string error_string; while (itr) { @@ -933,16 +950,17 @@ static inline gson(acl::json_node &node, std::vector *objs) objs->push_back(obj); typename std::vector::iterator it = objs->end(); --it; - result = gson(*itr, *it); + result = gson(*itr, &*it); if (!result.first) { - error_string.append(result.second); - objs->erase(it); + break; } itr = node.next_child(); } - - return std::make_pair(!!!objs->empty(), error_string); + if(result.first) + return result; + objs->clear(); + return result; } // vector @@ -952,7 +970,6 @@ static inline gson(acl::json_node &node, std::vector *objs) { std::pair result; acl::json_node *itr = node.first_child(); - std::string error_string; while (itr) { @@ -963,13 +980,78 @@ static inline gson(acl::json_node &node, std::vector *objs) result = gson(*itr, *it); if (!result.first) { - error_string.append(result.second); - objs->erase(it); + break; } itr = node.next_child(); } + if(result.first) + return result; - return std::make_pair(!!!objs->empty(), error_string); + while(objs->size()) + { + delete objs[0]; + objs->pop_back(); + } + return result; +} + +//set +template +std::pair +static inline gson(acl::json_node &node, std::set *objs) +{ + std::pair result; + acl::json_node *itr = node.first_child(); + + while (itr) + { + T* obj = new T; + result = gson(*itr, obj); + if (!result.first) + { + delete obj; + break; + } + objs->insert(obj); + itr = node.next_child(); + } + + if (result.first) + return result; + + while(objs->size()) + { + T* obj = *objs->begin(); + objs->erase(objs->begin()); + delete obj; + } + return result; +} + +template +std::pair +static inline gson(acl::json_node &node, std::set *objs) +{ + std::pair result; + acl::json_node *itr = node.first_child(); + + while (itr) + { + T obj; + result = gson(*itr, &obj); + if (!result.first) + { + break; + } + objs->insert(obj); + itr = node.next_child(); + } + + if (result.first) + return result; + + objs->clear(); + return result; } template @@ -1108,7 +1190,7 @@ static inline expand(acl::json_node &node, std::map *objs) for (typename std::map::iterator it = objs->begin(); it != objs->end(); ++it) { - del(it->second); + del(&it->second); } objs->clear(); @@ -1140,7 +1222,7 @@ static inline expand(acl::json_node &node, std::map *objs) for (typename std::map::iterator itr2 = objs->begin(); itr2 != objs->end(); ++itr2) { - del(itr2->second); + del(&itr2->second); } objs->clear(); diff --git a/lib_acl_cpp/include/acl_cpp/serialize/gsoner.hpp b/lib_acl_cpp/include/acl_cpp/serialize/gsoner.hpp index 20a7184a6..686906670 100644 --- a/lib_acl_cpp/include/acl_cpp/serialize/gsoner.hpp +++ b/lib_acl_cpp/include/acl_cpp/serialize/gsoner.hpp @@ -55,6 +55,7 @@ private: e_list, e_vector, e_map, + e_set, e_object, }; @@ -104,8 +105,8 @@ private: std::string get_node_func (const field_t &field); std::string get_gson_func_laber (const field_t &field); function_code_t gen_pack_code (const object_t &obj); - std::string get_unpack_code(const std::string &obj_name, - const field_t &field); + std::string get_unpack_code(const std::string &obj_name, + const field_t &field)const ; std::string get_node_name(const std::string &name); std::string next_token(std::string delimiters); std::string get_namespace(); @@ -116,6 +117,7 @@ private: bool skip_space_comment(); + bool check_use_namespace(); bool check_namespace(); bool check_namespace_end(); bool check_struct_begin (); diff --git a/lib_acl_cpp/src/serialize/gsoner.cpp b/lib_acl_cpp/src/serialize/gsoner.cpp index 296974b0c..e3c604523 100644 --- a/lib_acl_cpp/src/serialize/gsoner.cpp +++ b/lib_acl_cpp/src/serialize/gsoner.cpp @@ -144,6 +144,7 @@ std::string gsoner::get_node_func(const field_t &field) case gsoner::field_t::e_vector: case gsoner::field_t::e_map: case gsoner::field_t::e_object: + case gsoner::field_t::e_set: return "add_child"; default: break; @@ -161,6 +162,7 @@ std::string gsoner::get_gson_func_laber(const field_t &field) case gsoner::field_t::e_list: case gsoner::field_t::e_vector: case gsoner::field_t::e_map: + case gsoner::field_t::e_set: case gsoner::field_t::e_object: return "acl::gson($json, "; default: @@ -248,7 +250,7 @@ gsoner::function_code_t gsoner::gen_pack_code(const object_t &obj) } std::string gsoner::get_unpack_code(const std::string &obj_name, - const field_t &field) + const field_t &field)const { if (field.type_ == field_t::e_bool || field.type_ == field_t::e_bool_ptr || @@ -336,9 +338,42 @@ gsoner::function_code_t gsoner::gen_unpack_code(const object_t &obj) code.definition_ptr_ += prefix + obj.name_ + " *$obj)"; code.definition_ptr_ += "\n{\n" + tab_ + "return gson($node, *$obj);\n}\n\n"; + code.declare2_ = "std::pair " + "gson(const acl::string &str, "+ obj.name_ +" &$obj);"; + code.definition2_ = " std::pair " + "gson(const acl::string &$str, " + obj.name_ + " &$obj)\n" + "{\n" + + tab_ + "acl::json _json;\n" + + tab_ + "_json.update($str.c_str());\n" + + tab_ + "if (!_json.finish())\n" + + tab_ + "{\n" + + tab_ + tab_ + "return std::make_pair(false, \"json not finish error\");\n" + + tab_ + "}\n" + + tab_ + "return gson(_json.get_root(), $obj);\n" + "}\n\n"; return code; } +bool gsoner::check_use_namespace() +{ + //using namespace xxx; + int pos = pos_; + std::string token = codes_.substr(pos_, strlen("using")); + if (token == "using") + { + pos_ += (int)strlen("using"); + if(next_token(default_delimiters_) == "namespace") + { + token = next_token(default_delimiters_ + ";"); + pos_++;//skip ; + std::cout << "find:using namespace " << token << std::endl;; + return true; + } + } + pos_ = pos; + return false; +} + bool gsoner::check_namespace() { std::string temp = codes_.substr(pos_, strlen("namespace")); @@ -1181,6 +1216,15 @@ bool gsoner::check_member() f.type_ = field_t::e_map; current_obj_.fields_.push_back(f); + return true;; + } + else if(itr->find("set") != std::string::npos) + { + field_t f; + f.name_ = name; + f.type_ = field_t::e_set; + current_obj_.fields_.push_back(f); + return true;; } } @@ -1347,6 +1391,9 @@ void gsoner::parse_code() case 's': if(check_struct_begin()) continue; + case 'u': + if (check_use_namespace()) + continue; default: if(check_function()) continue; @@ -1428,12 +1475,40 @@ std::string gsoner::get_include_files() return str; } +static std::string get_filename_without_ext(std::string filename) +{ + std::string result; + if (filename.empty()) + return{}; + + std::size_t pos = filename.find_last_of('.'); + if (pos != filename.npos) + filename = filename.substr(0, pos); + + for(int i = (int)filename.size() -1; i >= 0; --i) + { + char ch = filename[i]; + if (ch == '/' || ch == '\\') + break; + result += filename[i]; + } + if (result.empty()) + return{}; + std::reverse(result.begin(), result.end()); + return result; +} void gsoner::gen_gson() { const char *namespace_start = "namespace acl\n{"; const char *namespace_end = "\n}///end of acl.\n"; + if(files_.size() == 1) + { + std::string filename = get_filename_without_ext(files_.front()); + gen_header_filename_ = filename + ".gson.h"; + gen_source_filename_ = filename + ".gson.cpp"; + } write_source("#include \"stdafx.h\"\n"); write_source(get_include_files()); write_source("#include \"" + gen_header_filename_ + "\"\n"); @@ -1452,14 +1527,16 @@ void gsoner::gen_gson() write_header(('\n' + tab_ + pack.declare2_)); write_header(('\n' + tab_ + pack.declare_)); write_header(('\n' + tab_ + pack.declare_ptr_)); - write_header('\n' + tab_ + unpack.declare_); - write_header('\n' + tab_ + unpack.declare_ptr_); + write_header('\n' + tab_ + unpack.declare_); + write_header('\n' + tab_ + unpack.declare_ptr_); + write_header('\n' + tab_ + unpack.declare2_+"\n"); write_source(add_4space(pack.definition_)); write_source(add_4space(pack.definition_ptr_)); write_source(add_4space(pack.definition2_)); write_source(add_4space(unpack.definition_)); write_source(add_4space(unpack.definition_ptr_)); + write_source(add_4space(unpack.definition2_)); } write_header(namespace_end); @@ -1613,3 +1690,4 @@ bool gsoner::check_pragma() } } // namespace acl +