Based on C ++ rapidjson json serialization and deserialization string

Parse json string to encapsulate often seen in our development process, especially in the socket communication above, encountered during a project json string parsing, and the company did not have a good library package, so they own based on open source a library package, took a use case, if you feel you can, I go to download the source code to compile a library out (direct compiler, without any dependence), 

Download: upload CSDN resources are reviewing them, if we need to contact QQ: 497725486, etc. approved, the link labeled

jsonObjDefine.h

#pragma once
#include "../LibJosnSerial/JsonBase.h"
#include <string>
using namespace JsonBaseHelper;
using namespace std;
class Person : public JsonBase
{
public:
	string name;
	string sex;
	int age;
	int ident;
	void ToWrite(Writer<StringBuffer> &writer)
	{
		RapidjsonWriteBegin(writer);
		RapidjsonWriteString(name);
		RapidjsonWriteString(sex);
		RapidjsonWriteInt(age);
		RapidjsonWriteInt(ident);
		RapidjsonWriteEnd();
	}
	void ParseJson(const Value& val)
	{
		RapidjsonParseBegin(val);
		RapidjsonParseToString(name);
		RapidjsonParseToString(sex);
		RapidjsonParseToInt(age);
		RapidjsonParseToInt(ident);
		RapidjsonParseEnd();
	}
};

class MacPropery : public JsonBase
{
public:
	int count;
	float perMachine;
	int machineCount;
	string operationTime;
	void ToWrite(Writer<StringBuffer> &writer)
	{
		RapidjsonWriteBegin(writer);
		RapidjsonWriteInt(count);
		RapidjsonWriteDouble(perMachine);
		RapidjsonWriteInt(machineCount);
		RapidjsonWriteString(operationTime);
		RapidjsonWriteEnd();
	};
	void ParseJson(const Value& val)
	{
		RapidjsonParseBegin(val);
		RapidjsonParseToInt(count);
		RapidjsonParseToDouble(perMachine);
		RapidjsonParseToInt(machineCount);
		RapidjsonParseToString(operationTime);
		RapidjsonParseEnd();
	};
};

class PrintMacInfo : public JsonBase
{
public:
	int code;
	string message;
	JsonArray<MacPropery> data;
	void ToWrite(Writer<StringBuffer> &writer)
	{
		RapidjsonWriteBegin(writer);
		RapidjsonWriteInt(code);
		RapidjsonWriteString(message);
		RapidjsonWriteClass(data);
		RapidjsonWriteEnd();
	};
	void ParseJson(const Value& val)
	{
		RapidjsonParseBegin(val);
		RapidjsonParseToInt(code);
		RapidjsonParseToString(message);
		RapidjsonParseToClass(data);
		RapidjsonParseEnd();
	};
};

  Test demo

#include <iostream> 
#include "JsonObjDefine.h" // json object header file defines 
#include <iostream> 
library based rapidjson // package 
#pragma comment (lib, ".. \\ LibJosnSerial \\ Bin \\ LibJosnSerial. lib ") 
int main () 
{ 
    STD :: COUT <<" basis json string conversion \ n-"; 
	the Person per; 
	per.age = 99; 
	per.ident = 123; 
	per.name =" test "; 
	per.sex = "male"; 
	string strPerJson = JsonBase :: SerializeJson (& per); 
	std :: cout << "the Person -> Json string:" << std :: endl << std :: endl << strPerJson; 
	std :: << endl cout; 
	std :: cout << "Json string -> the Person" << std :: endl; 
	the Person PERRET;
	JsonBase::DeserializeJson(&perRet, strPerJson);
	cout << "age: " << perRet.age << endl;
	cout << "ident: " << perRet.ident << endl;
	cout << "name: " << perRet.name << endl;
	cout << "sex: " << perRet.sex << endl;
	std::cout << endl;

	std::cout << "有嵌套json字符串转换\n";
	PrintMacInfo printMac;
	printMac.code = 0;
	printMac.message = "测试json字符串";
	MacPropery mac_1;
	mac_1.count = 6031;
	mac_1.perMachine = 861.57;
	mac_1.machineCount = 7;
	mac_1.operationTime = "2012";
	printMac.data.objList.push_back(mac_1);
	MacPropery mac_2;
	mac_2.count = 20754;
	mac_2.perMachine = 2964.86;
	mac_2.machineCount = 7;
	mac_2.operationTime = "2018";
	printMac.data.objList.push_back(mac_2);
	string strJson = JsonBase::SerializeJson(&printMac);
	std::cout << "PrintMacInfo -> Json字符串:"<< std::endl << strJson<<std::endl;
	std::cout << endl;
	std::cout << "Json字符串 -> PrintMacInfo" << endl;
	PrintMacInfo printMacRet;
	JsonBase::DeserializeJson(&printMacRet, strJson);
	cout << "code:"<< printMacRet.code << endl;
	cout << "message" << printMacRet.message << endl;
	int index = 1;
	for each(auto it in printMacRet.data.objList)
	{
		cout << "data_MacPro_"<<index<<": "<< it.count << endl;
		cout << "data_MacPro_"<<index<<": "<< it.machineCount << endl;
		cout << "data_MacPro_"<<index<<": "<< it.perMachine << endl;
		cout << "data_MacPro_"<<index<<": "<< it.operationTime << endl;
		cout << endl;
		index++;
	}
	getchar();
}

  Printout:

Ordinary string format

{
"name": "测试",
"sex": "男性",
"age": 99,
"ident": 123
}

Nested string format json

{
"code": 0,
"message": "测试json字符串",
"data": [{
"count": 6031,
"perMachine": 861.5700073242188,
"machineCount": 7,
"operationTime ": "2012 "
},
{
"count ": 20754,
"perMachine ": 2964.860107421875,
"machineCount ": 7,
"operationTime ": "2018 "
}
]
}

 

Guess you like

Origin www.cnblogs.com/hul201610101100/p/11278105.html