1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include "stdafx.h" #include <list> #include <vector> #include <map> #include <stdio.h> #include <iostream> #include <time.h> #include "struct.h" #include "gson.h"
static void print_msg(message& msg) { printf("=======================================================\r\n"); printf("type: %d\r\n", msg.type); printf("cmd: %s\r\n", msg.cmd.c_str());
printf("-------------------- user list ------------------------\r\n"); size_t i = 0; for (auto cit : msg.user_list) { printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", cit.username.c_str(), cit.domain.c_str(), cit.age, cit.male ? "true" : "false"); if (++i >= 10) break; }
printf("-------------------- user vector ----------------------\r\n"); i = 0; for (auto cit : msg.user_vector) { printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", cit.username.c_str(), cit.domain.c_str(), cit.age, cit.male ? "true" : "false"); if (++i >= 10) break; }
printf("------------------- user map --------------------------\r\n"); i = 0; for (auto cit : msg.user_map) { printf(">>key: %s, username: %s, domain: %s, age: %d, male: %s\r\n", cit.first.c_str(), cit.second.username.c_str(), cit.second.domain.c_str(), cit.second.age, cit.second.male ? "true" : "false"); if (++i >= 10) break; } printf("-------------------- user list ptr --------------------\r\n"); i = 0; for (auto cit : *msg.user_list_ptr) { printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", cit->username.c_str(), cit->domain.c_str(), cit->age, cit->male ? "true" : "false"); if (++i >= 10) break; }
printf("-------------------- user vector ptr ------------------\r\n"); i = 0; for (auto cit : *msg.user_vector_ptr) { printf(">>username: %s, domain: %s, age: %d, male: %s\r\n", cit->username.c_str(), cit->domain.c_str(), cit->age, cit->male ? "true" : "false"); if (++i >= 10) break; }
printf("------------------- user map ptr ----------------------\r\n"); i = 0; for (auto cit : *msg.user_map_ptr) { printf(">>key: %s, username: %s, domain: %s, age: %d, male: %s\r\n", cit.first.c_str(), cit.second->username.c_str(), cit.second->domain.c_str(), cit.second->age, cit.second->male ? "true" : "false"); if (++i >= 10) break; }
printf("-------------------------------------------------------\r\n"); }
static void test(void) {
message msg; msg.user_list_ptr = new std::list<user*>; msg.user_vector_ptr = new std::vector<user*>; msg.user_map_ptr = new std::map<acl::string, user*>;
msg.type = 1; msg.cmd = "add";
user u = {"zsx1", "263.net", 11, true}; msg.user_list.push_back(u); msg.user_list.emplace_back(u); msg.user_list.emplace_back("zsx1", "263.net", 13, false);
u = {"zsx2", "263.net", 11, true}; msg.user_vector.push_back(u); msg.user_vector.emplace_back(u); msg.user_vector.emplace_back("zsx2", "263.net4", 14, true);
u = {"zsx31", "263.net", 11, true}; msg.user_map[u.username] = u; msg.user_map["zsx32"] = {"zsx32", "263.net", 11, true };
msg.user_list_ptr->push_back(new user("zsx4", "263.net1", 11, true)); msg.user_list_ptr->push_back(new user("zsx4", "263.net2", 12, true));
msg.user_vector_ptr->push_back(new user("zsx5", "263.net1", 11, true)); msg.user_vector_ptr->push_back(new user("zsx5", "263.net2", 12, true));
(*msg.user_map_ptr)["zsx61"] = new user("zsx61:", "263.net1", 11, true); (*msg.user_map_ptr)["zsx62"] = new user("zsx62", "263.net2", 12, true);
acl::json json;
acl::json_node& node = acl::gson(json, msg); printf("serialize: %s\r\n", node.to_string().c_str());
message msg1; acl::json json1; json1.update(node.to_string());
printf("json1 to_string: %s\r\n", json1.to_string().c_str());
std::pair<bool, std::string> ret = acl::gson(json1.get_root(), msg1); if (ret.first == false) printf("error: %s\r\n", ret.second.c_str()); else print_msg(msg); }
int main(void) { test(); return 0; }
上述例子主要体现了 gson 的两个特点:1、允许有构造函数,2、支持 C++11 中的结构成员初始化。
|