comm目录下的另外几个文件
使用了开源的库jsoncpp,可下载。
(1)my_json.h
1 #include<iostream> 2 #include<unistd.h> 3 #include<string> 4 #include "json/json.h" 5 6 class my_json 7 { 8 public: 9 my_json(); 10 ~my_json(); 11 static int serialize(Json::Value& _val,std::string& _out_str); 12 static int deserialize(Json::Value& _val,std::string& _in_str); 13 private: 14 Json::Value val; 15 std::string str; 16 };
(2)my_json.cpp
1 #include "my_json.h" 2 3 my_json::my_json() 4 {} 5 my_json::~my_json() 6 {} 7 int my_json::serialize(Json::Value& _in_val,std::string& _out_str) 8 { 9 #ifdef DEBUG 10 Json::StyledWriter _write; 11 _out_str=_write.write(_in_val); 12 #else 13 Json::FastWriter _write; 14 _out_str=_write.write(_in_val); 15 #endif 16 return 0; 17 } 18 int my_json::deserialize(Json::Value& _out_val,std::string& _in_str) 19 { 20 Json::Reader _read; 21 if(_read.parse(_in_str,_out_val,false)) 22 return 0; 23 return 1; 24 }
(3)udp_data.h
1 #include<iostream> 2 #include<string> 3 #include "json/json.h" 4 #include "my_json.h" 5 class udp_data 6 { 7 public: 8 // udp_data(std::string _nick_name,std::string _msg,std::string _school,std::string _cmd); 9 udp_data(); 10 ~udp_data(); 11 void to_string(std::string _in_nick_name,std::string _in_msg,std::string _in_school,std::string _in_cmd,std::string &_out_string); 12 //void to_value(std::string& _out_nick_name,std::string& _out_msg,std::string& _out_school,std::string& _out_cmd,std::string& _in_string); 13 void to_value(std::string& _in_string); 14 private: 15 std::string nick_name; 16 std::string msg; 17 std::string school; 18 std::string cmd; 19 };
(4)udp_data.cpp
1 #include "udp_data.h" 2 3 //udp_data::udp_data(std::string _nick_name,std::string _msg,std::string _school,std::string _cmd) 4 // :nick_name(_nick_name) 5 // ,msg(_msg) 6 // ,school(_school) 7 // ,cmd(_cmd) 8 //{} 9 10 udp_data::udp_data() 11 {} 12 13 udp_data::~udp_data() 14 {} 15 void udp_data::to_string(std::string _in_nick_name,std::string _in_msg,std::string _in_school,std::string _in_cmd,std::string& _out_string) 16 { 17 nick_name=_in_nick_name; 18 msg=_in_msg; 19 school=_in_school; 20 cmd=_in_cmd; 21 22 Json::Value root; 23 root["nick_name"]=nick_name; 24 root["msg"]=msg; 25 root["school"]=school; 26 root["cmd"]=cmd; 27 28 my_json::serialize(root,_out_string); 29 } 30 //void udp_data::to_string(std::string& _out_string) 31 //{ 32 // Json::Value root; 33 // root["nick_name"]=nick_name; 34 // root["msg"]=msg; 35 // root["school"]=school; 36 // root["cmd"]=cmd; 37 // my_json::serialize(root,_out_string); 38 //} 39 40 //void udp_data::to_value(std::string& _out_nick_name,std::string& _out_msg,std::string& _out_school,std::string& _out_cmd,std::string& _in_string) 41 //{ 42 // Json::Value val; 43 // my_json::deserialize(_in_string,val); 44 // 45 // _out_nick_name=val["nick_name"].asString(); 46 // _out_msg=val["msg"].asString(); 47 // _out_school=val["school"].asString(); 48 // _out_cmd=val["cmd"].asString(); 49 // 50 // nick_name=_out_nick_name; 51 // msg=_out_msg; 52 // school=_out_school; 53 // cmd=_out_cmd; 54 //} 55 56 void udp_data::to_value(std::string& _in_string) 57 { 58 Json::Value val; 59 my_json::deserialize(val,_in_string); 60 61 nick_name=val["nick_name"].asString(); 62 msg=val["msg"].asString(); 63 school=val["school"].asString(); 64 cmd=val["cmd"].asString(); 65 66 std::cout<<"nick_name:"<<nick_name<<std::endl; 67 std::cout<<"msg:"<<msg<<std::endl; 68 std::cout<<"school:"<<school<<std::endl; 69 std::cout<<"cmd:"<<cmd<<std::endl; 70 } 71 72 //void udp_data::to_value(std::string& _in_string) 73 //{ 74 // Json::Value val; 75 // my_json::deserialize(_in_string,val); 76 //} 77 int main() 78 { 79 udp_data _data; 80 // std::string out_string; 81 // _data.to_string("luu","beautiful","XPU","dancing",out_string); 82 // std::cout<<out_string; 83 84 // std::string in_string="{luu beautiful XPU dancing}"; 85 std::string in_string="{\"cmd\":\"dancing\",\"msg\":\"beautiful\",\"nick_name\":\"luu\",\"school\":\"XPU\"}"; 86 _data.to_value(in_string); 87 return 0; 88 }
//makefile
1 ROOT_PATH=$(shell pwd) 2 INCLUDE=-I$(ROOT_PATH)/lib/include 3 LIB=-L$(ROOT_PATH)/lib 4 5 udp_data:udp_data.o my_json.o 6 g++ -o [email protected] $^ $(LIB) -ljsoncpp 7 8 %.o:%.cpp 9 g++ -c $< $(INCLUDE) -D_DEBUG_ 10 11 .PHONY:clean 12 clean: 13 rm -f udp_data *.o 14
输出结果:
(1)序列化
(2)反序列化
时间: 2024-11-24 16:52:44