From 576a1f6e44ad745f0d554238a3b0b450d7471dcd Mon Sep 17 00:00:00 2001 From: akzi <382018309@qq.com> Date: Mon, 17 Oct 2016 16:02:31 +0200 Subject: [PATCH 1/2] add codes for add_item processing std::string acl::string char * --- .../include/acl_cpp/serialize/gson_helper.ipp | 60 +++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) 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 da4e2b698..781b7fbcc 100644 --- a/lib_acl_cpp/include/acl_cpp/serialize/gson_helper.ipp +++ b/lib_acl_cpp/include/acl_cpp/serialize/gson_helper.ipp @@ -335,31 +335,18 @@ template typename enable_if::value, void>::type static inline add_item(acl::json &json, acl::json_node &node, const T &obj) { - if (check_nullptr(obj)) + if(check_nullptr(obj)) node.add_array_null(); else node.add_child(gson(json, obj)); } -template -typename enable_if::value, void>::type -static inline add_item(acl::json &json, acl::json_node &node, const T *obj) -{ - if (check_nullptr(obj)) - node.add_array_null(); - else - add_item(json, node, *obj); -} - // number template typename enable_if::value, void>::type static inline add_item(acl::json &, acl::json_node &node, T value) { - if (check_nullptr(value)) - node.add_array_null(); - else - node.add_array_number(get_value(value)); + node.add_array_number(get_value(value)); } template @@ -376,7 +363,14 @@ template typename enable_if::value, void>::type static inline add_item(acl::json &, acl::json_node &node, T value) { - if (check_nullptr(value)) + node.add_array_double(get_value(value)); +} + +template +typename enable_if::value, void>::type +static inline add_item(acl::json &, acl::json_node &node, T* value) +{ + if(check_nullptr(value)) node.add_array_null(); else node.add_array_double(get_value(value)); @@ -385,7 +379,7 @@ static inline add_item(acl::json &, acl::json_node &node, T value) //bool template typename enable_if::value, void>::type -static inline add_item(acl::json &, acl::json_node &node, T value) +static inline add_item(acl::json &, acl::json_node &node, T *value) { if (check_nullptr(value)) node.add_array_null(); @@ -393,6 +387,38 @@ static inline add_item(acl::json &, acl::json_node &node, T value) node.add_array_bool(get_value(value)); } +template +typename enable_if::value, void>::type +static inline add_item(acl::json &, acl::json_node &node, T value) +{ + node.add_array_bool(get_value(value)); +} + +template +typename enable_if::value, void>::type +static inline add_item(acl::json &, acl::json_node &node, T value) +{ + node.add_array_text(get_value(value)); +} + +template +typename enable_if::value, void>::type +static inline add_item(acl::json &, acl::json_node &node, T *value) +{ + if(check_nullptr(value)) + node.add_array_null(); + else + node.add_array_text(get_value(value)); +} + +static inline void add_item(acl::json &, acl::json_node &node, char *value) +{ + if(check_nullptr(value)) + node.add_array_null(); + else + node.add_array_text(value); +} + template static inline acl::json_node &gson(acl::json &json, const std::list &objects) { From b52ba569eca7878f7a5b38c39691f49720979817 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Mon, 17 Oct 2016 22:23:21 +0800 Subject: [PATCH 2/2] add one gson sample: test4 --- app/gson/test/test4/Makefile | 5 ++ app/gson/test/test4/main.cpp | 93 +++++++++++++++++++++++++++++++++ app/gson/test/test4/stdafx.cpp | 8 +++ app/gson/test/test4/stdafx.h | 15 ++++++ app/gson/test/test4/struct.stub | 12 +++++ app/gson/test/test4/valgrind.sh | 3 ++ 6 files changed, 136 insertions(+) create mode 100644 app/gson/test/test4/Makefile create mode 100644 app/gson/test/test4/main.cpp create mode 100644 app/gson/test/test4/stdafx.cpp create mode 100644 app/gson/test/test4/stdafx.h create mode 100644 app/gson/test/test4/struct.stub create mode 100755 app/gson/test/test4/valgrind.sh diff --git a/app/gson/test/test4/Makefile b/app/gson/test/test4/Makefile new file mode 100644 index 000000000..f3f60643b --- /dev/null +++ b/app/gson/test/test4/Makefile @@ -0,0 +1,5 @@ +include ../Makefile.in +ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) + EXTLIBS += -L/usr/local/lib -liconv +endif +PROG = test diff --git a/app/gson/test/test4/main.cpp b/app/gson/test/test4/main.cpp new file mode 100644 index 000000000..180eb5845 --- /dev/null +++ b/app/gson/test/test4/main.cpp @@ -0,0 +1,93 @@ +#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include "struct.h" // 由 gson 工具根据 struct.stub 转换而成 +#include "gson.h" // 由 gson 工具根据 struct.stub 生成 + +// 序列化过程 +static void serialize(void) +{ + user u; + + u.name = "zsxxsz"; + u.domain = "263.net"; + u.age = 11; + u.male = true; + + std::vector names; + names.push_back("zsx11"); + names.push_back("zsx12"); + names.push_back("zsx13"); + names.push_back("zsx14"); + + u.names["zsx1"] = names; + + names.clear(); + + names.push_back("zsx21"); + names.push_back("zsx22"); + names.push_back("zsx23"); + names.push_back("zsx24"); + + u.names["zsx2"] = names; + + acl::json json; + + // 将 user 对象转换为 json 对象 + acl::json_node& node = acl::gson(json, u); + + printf(">> serialize:\r\n"); + printf("json: %s\r\n", node.to_string().c_str()); + printf("\r\n"); +} + +// 反序列化过程 +static void deserialize(void) +{ + const char *s = "{\"name\": \"zsxxsz\", \"domain\": \"263.net\", \"age\": 11, \"male\": true, \"names\": [{\"zsx1\": [\"zsx11\", \"zsx12\", \"zsx13\", \"zsx14\"]}, {\"zsx2\": [\"zsx21\", \"zsx22\", \"zsx23\", \"zsx24\"]}]}"; + + acl::json json; + json.update(s); + user u; + + printf(">> deserialize:\r\n"); + + // 将 json 对象转换为 user 对象 + std::pair ret = acl::gson(json.get_root(), u); + + // 如果转换失败,则打印转换失败原因 + if (ret.first == false) + { + printf("error: %s\r\n", ret.second.c_str()); + return; + } + + printf("name: %s, domain: %s, age: %d, male: %s\r\n", u.name.c_str(), + u.domain.c_str(), u.age, u.male ? "yes" : "no"); + + std::map >::const_iterator + cit = u.names.find("zsx2"); + if (cit != u.names.end()) + { + printf("zsx2:"); + for (std::vector::const_iterator cit2 + = cit->second.begin(); + cit2 != cit->second.end(); ++cit2) + { + printf(" %s", (*cit2).c_str()); + } + + printf("\r\n"); + } +} + +int main(void) +{ + serialize(); + deserialize(); + return 0; +} diff --git a/app/gson/test/test4/stdafx.cpp b/app/gson/test/test4/stdafx.cpp new file mode 100644 index 000000000..86cfa1350 --- /dev/null +++ b/app/gson/test/test4/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : ֻ׼ļԴļ +// wizard.pch ΪԤͷ +// stdafx.obj ԤϢ + +#include "stdafx.h" + +// TODO: STDAFX.H +//κĸͷļڴļ diff --git a/app/gson/test/test4/stdafx.h b/app/gson/test/test4/stdafx.h new file mode 100644 index 000000000..eb2ba8f57 --- /dev/null +++ b/app/gson/test/test4/stdafx.h @@ -0,0 +1,15 @@ +// stdafx.h : ׼ϵͳļİļ +// dzõĵĿضİļ +// + +#pragma once + + +//#include +//#include + +// TODO: ڴ˴óҪĸͷļ + +#include "lib_acl.h" +#include "acl_cpp/lib_acl.hpp" +#include "lib_protocol.h" diff --git a/app/gson/test/test4/struct.stub b/app/gson/test/test4/struct.stub new file mode 100644 index 000000000..c5e1eda4b --- /dev/null +++ b/app/gson/test/test4/struct.stub @@ -0,0 +1,12 @@ +#pragma once +#include + +struct user +{ + std::string name; + std::string domain; + int age; + bool male; + + std::map > names; +}; diff --git a/app/gson/test/test4/valgrind.sh b/app/gson/test/test4/valgrind.sh new file mode 100755 index 000000000..0b1bb2988 --- /dev/null +++ b/app/gson/test/test4/valgrind.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +valgrind --tool=memcheck --leak-check=yes -v ./test