mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 02:47:56 +08:00
add one gson sample: test4
This commit is contained in:
parent
576a1f6e44
commit
b52ba569ec
5
app/gson/test/test4/Makefile
Normal file
5
app/gson/test/test4/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
include ../Makefile.in
|
||||
ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD)
|
||||
EXTLIBS += -L/usr/local/lib -liconv
|
||||
endif
|
||||
PROG = test
|
93
app/gson/test/test4/main.cpp
Normal file
93
app/gson/test/test4/main.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
#include "stdafx.h"
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#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<std::string> 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<bool, std::string> 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<std::string, std::vector<std::string> >::const_iterator
|
||||
cit = u.names.find("zsx2");
|
||||
if (cit != u.names.end())
|
||||
{
|
||||
printf("zsx2:");
|
||||
for (std::vector<std::string>::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;
|
||||
}
|
8
app/gson/test/test4/stdafx.cpp
Normal file
8
app/gson/test/test4/stdafx.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
// stdafx.cpp : 只包括标准包含文件的源文件
|
||||
// wizard.pch 将成为预编译头
|
||||
// stdafx.obj 将包含预编译类型信息
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
// TODO: 在 STDAFX.H 中
|
||||
//引用任何所需的附加头文件,而不是在此文件中引用
|
15
app/gson/test/test4/stdafx.h
Normal file
15
app/gson/test/test4/stdafx.h
Normal file
@ -0,0 +1,15 @@
|
||||
// stdafx.h : 标准系统包含文件的包含文件,
|
||||
// 或是常用但不常更改的项目特定的包含文件
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
//#include <iostream>
|
||||
//#include <tchar.h>
|
||||
|
||||
// TODO: 在此处引用程序要求的附加头文件
|
||||
|
||||
#include "lib_acl.h"
|
||||
#include "acl_cpp/lib_acl.hpp"
|
||||
#include "lib_protocol.h"
|
12
app/gson/test/test4/struct.stub
Normal file
12
app/gson/test/test4/struct.stub
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
struct user
|
||||
{
|
||||
std::string name;
|
||||
std::string domain;
|
||||
int age;
|
||||
bool male;
|
||||
|
||||
std::map<std::string, std::vector<std::string> > names;
|
||||
};
|
3
app/gson/test/test4/valgrind.sh
Executable file
3
app/gson/test/test4/valgrind.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
valgrind --tool=memcheck --leak-check=yes -v ./test
|
Loading…
Reference in New Issue
Block a user