1 ROOT_PATH=$ shell (pwd) 2包括=- $ (ROOT_PATH)/lib/包括 3自由=- l $ (ROOT_PATH)/LIB/LIB 4 5 my_json: my_json.o udp_data.o 6 g++ - o $ @ $ ^ $ -ljsoncpp(自由) 7% . o: % . cpp 8 g + + - c <美元(包括) 9 .PHONY:清洁 10清洁: 11 rm - f *。o my_json
1 # include2使用名称空间性病; 3 # include“json/json.h” 4 # include <字符串> 5 6类my_json 7 { 8公开: 9 my_json (); 10 ~ my_json (); 11静态int序列化(字符串_out_str Json::价值_out_val); 12个静态int反序列化(Json:价值_val,字符串_in_str); 13 14个}; ~
1 #包括“my_json.h” 2 3 my_json:: my_json () 4 {} 5 6 my_json:: ~ my_json () 7 {} 8 9 int my_json::反序列化(Json:价值_val,字符串_in_str) 10 { Json: 11:读者_read; 12如果(_read.parse (_in_str _val,假)) {13 14日返回0; 15} 16日返回1; 17} 18 int my_json::序列化(字符串_out_str, Json::价值_out_val) 19日{ 20 # ifdef _DEBUG_ 21 Json: FastWriter _write; 22 _out_str=_write.write (_out_val); 其他23 # 24 Json: StyledWriter _write; 25 _out_str=_write.write (_out_val); 26 # endif 27日返回0; 28日}
1 # include2使用名称空间性病; 3 # include <字符串> 4 5类udp_data 6 { 7公开: 8 udp_data (); 9 ~ udp_data (); 10个空白to_string(字符串名称字符串味精,字符串学校,字符串cmd,撑舌鳎_out_str); 11空白to_value(字符串_out_name,字符串out_msg,字符串out_school,撑舌鳎out_cmd,字符串in_str); 12保护: 13弦_name; 14个字符串_msg; 15个字符串_school; 16个字符串_cmd; 17};
1 #包括“udp_data.h” 2 # include“my_json.h” 3 udp_data:: udp_data () 4:_cmd(“无”) 5 {} 6 7 udp_data:: ~ udp_data () {8} 9 10空udp_data:: to_string (std:: string的名字,std:: string味精、std:: string同步信道ool, std:: string cmd, std:: string _out_str) {11 12 _name=名称; 13 _msg=味精; 14 _school=学校; 15 _cmd=cmd; 16 Json:价值根源; 17根(“_name”)=_name; 18根(“_msg”)=_msg; 19根(“_school”)=_school; 20根(“_cmd”)=_cmd; 21 my_json:序列化(_out_str、根); 22} 23 24空白udp_data:: to_value (std:: string out_name, std:: string out_msg, std::生态ng out_school, std:: string out_cmd, std:: string in_str) 25 { 26 Json:价值val; 27 my_json:反序列化(val, in_str); 28 out_name=瓦尔(“_name”) .asString (); 29 out_msg=瓦尔(“_msg”) .asString (); 30 out_school=瓦尔(“_school”) .asString (); 31日out_cmd=瓦尔(“_cmd”) .asString (); 32 _name=out_name; 33 _msg=out_msg; 34 _school=out_school; 35 _cmd=out_cmd; 36} 37 38 39 int main () 40 { 41//字符串out_str; 42 udp_data _data; 43//字符串名称=" xiaozhi”; 44味精=//字符串“hello world”; 45//字符串学校=癤PU”; 46//字符串cmd=懊挥小? 47个//_data.to_string(名称、味精、学校,cmd, out_str); 48//cout
UDP畅聊系统数据的序列化与反序列化