Serialization and deserialization of UDP chat system data
Serialization and deserialization of basic data in comm
Based on the jsoncpp library, written by C++, used to provide serialization and deserialization of network data
Several classes used
Json::Value can represent all types, int,float,string, etc.
Json::Reader parses json strings into Value, deserializes, and uses the Parse function
Json::Writer converts Value to json string and serializes
Its two subclasses: Json::FastWriter and Json::StyledWriter two different display styles
Makefile
1 ROOT_PATH=$ (shell pwd) 2 INCLUDE=-I$ (ROOT_PATH) / lib/include 3 LIB=-L$ (ROOT_PATH) / lib/lib 4 5 my_json:my_json.o udp_data.o 6 rm +-o $@ $^ $(LIB)-ljsoncpp 7%. O:%.cpp 8 g my_json:my_json.o udp_data.o +-c $< $(INCLUDE) 9. PHONY: clean 10 clean: 11 rm-f * .o my_json
Writing serialization and deserialization in my_json
My_json.h
1 # include 2 using namespace std; 3 # include "json/json.h" 4 # include 56 class my_json 7 {8 public: 9 my_json (); 10 ~ my_json (); 11 static int serialize (string& _ out_str,Json::Value& _ out_val); 12 static int deserialize (Json::Value& _ val,string& _ in_str); 13 14}; ~
My_json.cpp
1 # include "my_json.h" 2 3 my_json::my_json () 4 {} 5 6 my_json::~my_json () 7 {} 8 9 int my_json::deserialize (Json::Value& _ val,string& _ in_str) 10 {11 Json::Reader _ read; 12 if (_ read.parse (_ in_str,_val,false)) 13 {14 return 0; 15} 16 return-1 17} 18 int my_json::serialize (string& _ out_str,Json::Value& _ out_val) 19 {20 # ifdef _ DEBUG_ 21 Json::FastWriter _ write; 22 _ out_str=_write.write (_ out_val); 23 # else 24 Json::StyledWriter _ write; 25 _ out_str=_write.write (_ out_val); 26 # endif 27 return 0; 28}
Test serialization and deserialization in udp_data
Udp_data.h
1 # include 2 using namespace std; 3 # include 4 5 class udp_data 6 {7 public: 8 udp_data (); 9 ~ udp_data (); 10 void to_string (string& name,string& msg,string& school,string& cmd,string& _ out_str); 11 void to_value (string& _ out_name,string& out_msg,string& out_school,strin g & out_cmd,string& in_str) 12 protected: 13 string _ name; 14 string _ msg; 15 string _ school; 16 string _ cmd; 17}
Udp_data.cpp
1 # include "udp_data.h" 2 # include "my_json.h" 3 udp_data::udp_data () 4: _ cmd ("None") 5 {} 6 7 udp_data::~udp_data () 8 {} 9 10 void udp_data::to_string (std::string& name,std::string& msg,std::string& sch ool,std::string& cmd,std::string& _ out_str) 11 {12 _ name=name 13 _ msg=msg; 14 _ school=school; 15 _ cmd=cmd; 16 Json::Value root; 17 root ["_ name"] = _ name; 18 root ["_ msg"] = _ msg; 19 root ["_ school"] = _ school; 20 root ["_ cmd"] = _ cmd; 21 my_json::serialize (_ out_str,root) 22} 23 24 void udp_data::to_value (std::string& out_name,std::string& out_msg,std::stri ng& out_school,std::string& out_cmd,std::string& in_str) 25 {26 Json::Valueval; 27 my_json::deserialize (val,in_str); 28 out_name=val ["_ name"] .asString (); 29 out_msg=val ["_ msg"] .asString () 30 out_school=val ["_ school"] .asString (); 31 out_cmd=val ["_ 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 / / string out_str; 42 udp_data _ data; 43 / string name= "xiaozhi"; 44 / / string msg= "hello world"; 45 / / string school= "XPU" 46 / / string cmd= "None"; 47 / / _ data.to_string (name,msg,school,cmd,out_str); 48 / / cout