add set support in Gson. and fix potemtial bug in gson_helper.ipp

This commit is contained in:
fuwangqin 2017-06-14 13:55:05 +08:00
parent 5b02378b16
commit f1ac0703ef
13 changed files with 499 additions and 159 deletions

View File

@ -7,6 +7,7 @@
<ClCompile>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(SolutionDir)lib_acl_cpp\include;$(SolutionDir)lib_acl\include;$(SolutionDir)lib_protocol\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(SolutionDir)lib_acl_cpp;$(SolutionDir)lib_acl;$(SolutionDir)lib_protocol;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>

View File

@ -2,4 +2,4 @@ include ./Makefile.in
ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD)
EXTLIBS += -L/usr/local/lib -liconv
endif
PROG = json
PROG = demo

View File

@ -63,6 +63,7 @@
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\..\..\acl.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
@ -116,14 +117,14 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="gson.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="struct.gson.h" />
<ClInclude Include="struct.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="gson.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="stdafx.cpp" />
<ClCompile Include="struct.gson.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="struct.stub" />

View File

@ -15,15 +15,15 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="gson.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="stdafx.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="struct.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="struct.gson.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
@ -32,7 +32,7 @@
<ClCompile Include="stdafx.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="gson.cpp">
<ClCompile Include="struct.gson.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>

View File

@ -5,7 +5,7 @@
#include <stdio.h>
#include <iostream>
#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());

View File

@ -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<bool, std::string> result;
std::pair<bool, std::string> $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<bool,std::string> 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<bool, std::string> result;
std::pair<bool, std::string> $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<bool,std::string> 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<bool, std::string> 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<bool, std::string> $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<bool,std::string> 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.

View File

@ -6,16 +6,22 @@ namespace acl
acl::json_node& gson(acl::json &$json, const base *$obj);
std::pair<bool,std::string> gson(acl::json_node &$node, base &$obj);
std::pair<bool,std::string> gson(acl::json_node &$node, base *$obj);
std::pair<bool,std::string> 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<bool,std::string> gson(acl::json_node &$node, hello::world &$obj);
std::pair<bool,std::string> gson(acl::json_node &$node, hello::world *$obj);
std::pair<bool,std::string> 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<bool,std::string> gson(acl::json_node &$node, list1 &$obj);
std::pair<bool,std::string> gson(acl::json_node &$node, list1 *$obj);
std::pair<bool,std::string> gson(const acl::string &str, list1 &$obj);
}///end of acl.

View File

@ -1,4 +1,5 @@
#pragma once
#include <set>
struct base
{
@ -37,13 +38,19 @@ struct list1
std::list<base> *bases_list_ptr;
//Gson@optional
std::list<base*> *bases_ptr_list_ptr;
std::vector<std::string> vector_string;
std::vector<std::list<base> > vector_list_base;
std::map<std::string, base> base_map;
std::map<std::string, std::string> string_map;
std::map<std::string, int> int_map;
std::map<std::string, bool > bool_map;
std::map<std::string, std::list<base> > base_list_map;
std::vector<std::string> vector_string;
std::vector<std::list<base> > vector_list_base;
//set
std::set<std::string> str_set_;
std::set<int> int_set_;
std::set<bool> bool_set_;
};
namespace hello

View File

@ -1,4 +1,66 @@
#pragma once
#pragma once#pragma once
#include <set>
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<base> bases_list;
std::list<base> *bases_list_ptr;
//Gson@optional
std::list<base*> *bases_ptr_list_ptr;
std::vector<std::string> vector_string;
std::vector<std::list<base> > vector_list_base;
std::map<std::string, base> base_map;
std::map<std::string, std::string> string_map;
std::map<std::string, int> int_map;
std::map<std::string, bool > bool_map;
std::map<std::string, std::list<base> > base_list_map;
//set
std::set<std::string> str_set_;
std::set<int> int_set_;
std::set<bool> bool_set_;
};
namespace hello
{
struct world : list1
{
int me;
};
}
struct base
{

View File

@ -63,6 +63,7 @@
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\..\acl.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />

View File

@ -15,7 +15,7 @@
#include "../stdlib/string.hpp"
#include "../stdlib/json.hpp"
#include "../stdlib/string.hpp"
#include <set>
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<class T>
static inline acl::json_node &gson(acl::json &json,
const std::list<T> &objects)
@ -445,7 +445,6 @@ static inline acl::json_node &gson(acl::json &json,
return node;
}
template<class T>
static inline acl::json_node &gson(acl::json &json,
const std::list<T> *objects)
@ -453,6 +452,7 @@ static inline acl::json_node &gson(acl::json &json,
return gson(json, *objects);
}
//vector
template<class T>
static inline acl::json_node &gson(acl::json &json,
const std::vector<T> &objects)
@ -473,6 +473,26 @@ static inline acl::json_node &gson(acl::json &json,
{
return gson(json, *objects);
}
//set
template<class T>
static inline acl::json_node &gson(acl::json &json, const std::set<T> *objects)
{
return gson(json, *objects);
}
template<class T>
static inline acl::json_node &gson(acl::json &json,
const std::set<T> &objects)
{
acl::json_node &node = json.create_array();
for (typename std::set<T>::const_iterator
itr = objects.begin(); itr != objects.end(); ++itr)
{
add_item(json, node, *itr);
}
return node;
}
//define number map
template<class K, class V>
@ -860,7 +880,6 @@ static inline std::pair<bool, std::string>
gson(acl::json_node &node, std::list<T> *objs)
{
std::pair<bool, std::string> 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<T> *objs)
objs->push_back(obj);
typename std::list<T>::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();
}
if (result.first)
return result;
return std::make_pair(!!!objs->empty(), error_string);
objs->clear();
return result;
}
// list
@ -891,28 +912,25 @@ static inline std::pair<bool, std::string>
gson(acl::json_node &node, std::list<T*> *objs)
{
std::pair<bool, std::string> 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<T*>::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<T> *objs)
{
std::pair<bool, std::string> 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<T> *objs)
objs->push_back(obj);
typename std::vector<T>::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<T*> *objs)
{
std::pair<bool, std::string> 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<T*> *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<class T>
std::pair<bool, std::string>
static inline gson(acl::json_node &node, std::set<T*> *objs)
{
std::pair<bool, std::string> 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<class T>
std::pair<bool, std::string>
static inline gson(acl::json_node &node, std::set<T> *objs)
{
std::pair<bool, std::string> 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 <class T>
@ -1108,7 +1190,7 @@ static inline expand(acl::json_node &node, std::map<K, T*> *objs)
for (typename std::map<K, T*>::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<K, T*> *objs)
for (typename std::map<K, T*>::iterator itr2 = objs->begin();
itr2 != objs->end(); ++itr2)
{
del(itr2->second);
del(&itr2->second);
}
objs->clear();

View File

@ -55,6 +55,7 @@ private:
e_list,
e_vector,
e_map,
e_set,
e_object,
};
@ -105,7 +106,7 @@ private:
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);
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 ();

View File

@ -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<bool,std::string> "
"gson(const acl::string &str, "+ obj.name_ +" &$obj);";
code.definition2_ = " std::pair<bool,std::string> "
"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");
@ -1454,12 +1529,14 @@ void gsoner::gen_gson()
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.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