1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #pragma once
- #include <iostream>
- #include <rapidjson/document.h>
- #include <rapidjson/rapidjson.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/writer.h>
- using namespace std;
- class HepuHB
- {
- private:
- public:
- const string cmd = "userOnlineHeart";
- string token;
- HepuHB(){};
- ~HepuHB(){};
- void objectToJson(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("cmd");
- writer.String(cmd.c_str());
- writer.Key("param");
- writer.StartObject();
- writer.Key("token");
- writer.String(token.c_str());
- writer.EndObject();
- writer.EndObject();
- }
- bool jsonToObject(std::string& json){
- rapidjson::Document doc;
- doc.Parse(json.c_str());
- if(!doc.IsObject()){
- return false;
- }
- const auto end = doc.MemberEnd();
- if(end == doc.FindMember("cmd") || !doc["cmd"].IsString()){
- return false;
- }else{
- if(cmd != doc["cmd"].GetString())
- return false;
- }
- if(end == doc.FindMember("param") || !doc["param"].IsObject()) {
- return false;
- }else{
- if(end == doc.FindMember("token") || !doc["token"].IsInt()){
- return false;
- }else{
- token = doc["token"].GetString();
- }
- }
- return true;
- }
- };
|