12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #pragma once
- #include <iostream>
- #include <rapidjson/document.h>
- #include <rapidjson/rapidjson.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/writer.h>
- using namespace std;
- class HepuLogin
- {
- private:
- public:
- const string cmd = "userLogin";
- string token;
- string username;
- string password;
- HepuLogin(){};
- ~HepuLogin(){};
-
- 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.Key("username");
- writer.String(username.c_str());
- writer.Key("password");
- writer.String(password.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"].IsString()) {
- return false;
- }else{
- token = doc["token"].GetString();
- }
- if(end == doc.FindMember("username") || !doc["username"].IsString()) {
- return false;
- }else{
- username = doc["username"].GetString();
- }
- if(end == doc.FindMember("password") || !doc["password"].IsString()) {
- return false;
- }else{
- password = doc["password"].GetString();
- }
- }
-
- return true;
- }
- };
|