From b52ba569eca7878f7a5b38c39691f49720979817 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Mon, 17 Oct 2016 22:23:21 +0800 Subject: [PATCH] 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