123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #pragma once
- #include <iostream>
- #include <rapidjson/document.h>
- #include <rapidjson/rapidjson.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/writer.h>
- using namespace std;
- class SendDevice
- {
- private:
- public:
- /* data */
- SendDevice(){};
- ~SendDevice(){};
- int deviceId;
- string commandCode;
- int status;
- int workStatus;
- int powerStatus;
- int batteryStatus;
- int num;
- int oxyPressure;
- int gasPressure;
- string remark;
- string attr1;
- string attr2;
- string attr3;
- string msgTime;
- void objectToJson(std::string& str){
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
- this->objectToJson(writer);
- str = strBuf.GetString();
- }
- void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
- writer.StartObject();
- writer.Key("deviceId");
- writer.Int(deviceId);
- writer.Key("commandCode");
- writer.String(commandCode.c_str());
- writer.Key("status");
- writer.Int(status);
- writer.Key("workStatus");
- writer.Int(workStatus);
- writer.Key("powerStatus");
- writer.Int(powerStatus);
- writer.Key("batteryStatus");
- writer.Int(batteryStatus);
- writer.Key("num");
- writer.Int(num);
-
- writer.Key("oxyPressure");
- writer.Int(oxyPressure);
- writer.Key("gasPressure");
- writer.Int(gasPressure);
- writer.Key("gasPressure");
- writer.Int(gasPressure);
- writer.Key("remark");
- writer.String(remark.c_str());
- writer.Key("attr1");
- writer.String(attr1.c_str());
- writer.Key("attr2");
- writer.String(attr2.c_str());
- writer.Key("attr3");
- writer.String(attr3.c_str());
- writer.Key("msgTime");
- writer.String(msgTime.c_str());
- writer.EndObject();
- }
-
- bool jsonToObject(const std::string& json){
- rapidjson::Document doc;
- if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
- return false;
- }
- // get members
- const auto end = doc.MemberEnd();
- // json_type
- if(end == doc.FindMember("deviceId") || !doc["deviceId"].IsInt()) {
- return false;
- }else{
- deviceId = doc["deviceId"].GetInt();
- }
- if(end == doc.FindMember("commandCode") || !doc["commandCode"].IsString()) {
- return false;
- }else{
- commandCode = doc["commandCode"].GetString();
- }
- if(end == doc.FindMember("status") || !doc["status"].IsInt()) {
- return false;
- }else{
- status = doc["status"].GetInt();
- }
- if(end == doc.FindMember("workStatus") || !doc["workStatus"].IsInt()) {
- return false;
- }else{
- workStatus = doc["workStatus"].GetInt();
- }
- if(end == doc.FindMember("powerStatus") || !doc["powerStatus"].IsInt()) {
- return false;
- }else{
- powerStatus = doc["powerStatus"].GetInt();
- }
- if(end == doc.FindMember("batteryStatus") || !doc["batteryStatus"].IsInt()) {
- return false;
- }else{
- batteryStatus = doc["batteryStatus"].GetInt();
- }
- if(end == doc.FindMember("num") || !doc["num"].IsInt()) {
- return false;
- }else{
- num = doc["num"].GetInt();
- }
- if(end == doc.FindMember("oxyPressure") || !doc["oxyPressure"].IsInt()) {
- return false;
- }else{
- oxyPressure = doc["oxyPressure"].GetInt();
- }
- if(end == doc.FindMember("gasPressure") || !doc["gasPressure"].IsInt()) {
- return false;
- }else{
- gasPressure = doc["gasPressure"].GetInt();
- }
-
- if(end == doc.FindMember("remark") || !doc["remark"].IsString()) {
- return false;
- }else{
- remark = doc["remark"].GetString();
- }
- if(end == doc.FindMember("attr1") || !doc["attr1"].IsString()) {
- return false;
- }else{
- attr1 = doc["attr1"].GetString();
- }
- if(end == doc.FindMember("attr2") || !doc["attr2"].IsString()) {
- return false;
- }else{
- attr2 = doc["attr2"].GetString();
- }
- if(end == doc.FindMember("attr3") || !doc["attr3"].IsString()) {
- return false;
- }else{
- attr3 = doc["attr3"].GetString();
- }
- if(end == doc.FindMember("msgTime") || !doc["msgTime"].IsString()) {
- return false;
- }else{
- msgTime = doc["msgTime"].GetString();
- }
- return true;
- }
- };
|