1. Introduction
-
json
It is a data exchange format, usingText formats that are completely independent of programming languagesTo store and represent data -
json
Data Type: Object, array, string, number-
Object:use
{}
Indicates an object in a sequential way -
Array:use
[]
Contains an array -
String:use
""
Indicates a string - number: Including plastic shaping and floating point type, use directly
-
Object:use
jsoncpp
Library for implementationjson
FormatSerialization and deserialization, complete the function of organizing multiple data objects into format strings to parse multiple data objects
It is mainly done with three classes and its corresponding small number of member functions.:
// Json data object class -> for intermediate data storageclass Json::Value { // Value overloads [] and =, so all assignments and acquisitions can be implemented through [] Value &operator=(const Value &other); // Complete val["name"] = "SnowK" in a simple way; Value& operator[](const std::string& key); Value& operator[](const char* key); // Remove elements Value removeMember(const char* key); // val["Score"][0] const Value& operator[](ArrayIndex index) const; // Add array element val["Score"].append(88); Value& append(const Value& value); // Get the number of array elements val["Score"].size(); ArrayIndex size() const; // Turn string name = val["name"].asString(); std::string asString() const; // Turn to char* char *name = val["name"].asCString(); const char* asCString() const; // Turn int int age = val["age"].asInt(); int asInt() const; // Turn to float float asFloat() const; // Turn to bool bool asBool() const; }; //Json serialization class, it is easier to use this in the lower versionclass JSON_API Writer { virtual std::string write(const Value& root) = 0; } class JSON_API FastWriter : public Writer { virtual std::string write(const Value& root); } class JSON_API StyledWriter : public Writer { virtual std::string write(const Value& root); } //json serialization class, recommended for higher versions, if you use lower versions of interfaces, there may be warnings.class JSON_API StreamWriter { virtual int write(Value const& root, std::ostream* sout) = 0; } class JSON_API StreamWriterBuilder : public StreamWriter::Factory { virtual StreamWriter* newStreamWriter() const; } // json deserialization class, the lower version is easier to useclass JSON_API Reader { bool parse(const std::string& document, Value& root, bool collectComments = true); } // json deserialization class, more recommended in higher versionclass JSON_API CharReader { virtual bool parse(char const* beginDoc, char const* endDoc, Value* root, std::string* errs) = 0; } class JSON_API CharReaderBuilder : public CharReader::Factory { virtual CharReader* newCharReader() const; }
3. Use
int main() { char name[] = "SnowK"; int age = 18; float score[3] = {100, 99, 98}; Json::Value stu; stu["Name"] = name; stu["Age"] = age; stu["Score"].append(score[0]); stu["Score"].append(score[1]); stu["Score"].append(score[2]); std::string str; if(Serialize(stu, str) == false) { return -1; } std::cout << str << std::endl; std::cout << "-------------------------------" << std::endl; Json::Value val; if(UnSerialize(str, val) == false) { return -1; } std::cout << val["Name"].asString() << std::endl; std::cout << val["Age"].asInt() << std::endl; for (int i = 0; i < val["Score"].size(); i++) { std::cout << val["Score"][i].asInt() << std::endl; } return 0; }
2. Serialization
bool Serialize(const Json::Value &val, std::string &dest) { // Json::StreamWriterBuilder produces Json::StreamWriter Json::StreamWriterBuilder swb; std::unique_ptr<Json::StreamWriter> sw(()); // Serialize via write() of Json::StreamWrite std::stringstream ss; if (sw->write(val, &ss) != 0) { std::cout << "Json serialization failed" << std::endl; return false; } dest = (); return true; }
3. Deserialization
bool UnSerialize(const std::string &src, Json::Value &val) { Json::CharReaderBuilder crb; std::unique_ptr<Json::CharReader> cr(()); std::string err; if (cr->parse(src.c_str(), src.c_str() + (), &val, &err) == false) { std::cout << "json deserialization failed: " << err << std::endl; return false; } return true; }
Summarize
This is all about this article about the C++ third-party library jsoncpp. For more related content of the C++ third-party library jsoncpp, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!