12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #pragma once
- #include <iostream>
- using namespace std;
- class RecDeviceCommand
- {
- private:
- /* data */
- int deviceId;
- /**
- * 操作驱鸟设备的指令类型
- */
- std::string commandCode;
- public:
- RecDeviceCommand(){};
- ~RecDeviceCommand(){};
- void setDeviceId(int DeviceId){
- deviceId = DeviceId;
- }
- void setCommandCode(std::string CommandCode) {
- commandCode = CommandCode;
- }
- int getDeviceId(){
- return deviceId;
- }
- std::string getCommandCode(){
- return commandCode;
- }
- 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("deviceId");
- writer.String(commandCode.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")) {
- return false;
- } else {
- if(doc["deviceId"].IsInt())
- deviceId = doc["deviceId"].GetInt();
- else if(doc["deviceId"].IsString())
- deviceId = std::stoi(doc["deviceId"].GetString());
- }
- if(end == doc.FindMember("commandCode") || !doc["commandCode"].IsString()) {
- return false;
- }else{
- commandCode = doc["commandCode"].GetString();
- }
- return true;
- }
- bool jsonToObject(const rapidjson::Value& object){
- const auto end = object.MemberEnd();
- if(end == object.FindMember("deviceId")){
- return false;
- }
- else{
- if(object["deviceId"].IsInt()) deviceId = object["deviceId"].GetInt();
- else if(object["deviceId"].IsString()) deviceId = std::atoi(object["deviceId"].GetString());
- }
- if(end == object.FindMember("commandCode") || !object["commandCode"].IsString()){
- return false;
- }
- else{
- commandCode = object["commandCode"].GetString();
- }
- return true;
- }
- };
|