123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #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;
- doc.Parse(json.c_str());
- if(!doc.IsObject()){
- 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;
- }
- };
|