HepuHB.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #include <iostream>
  3. #include <rapidjson/document.h>
  4. #include <rapidjson/rapidjson.h>
  5. #include <rapidjson/stringbuffer.h>
  6. #include <rapidjson/writer.h>
  7. using namespace std;
  8. class HepuHB
  9. {
  10. private:
  11. public:
  12. const string cmd = "userOnlineHeart";
  13. string token;
  14. HepuHB(){};
  15. ~HepuHB(){};
  16. void objectToJson(string& str){
  17. rapidjson::StringBuffer strBuf;
  18. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  19. this->objectToJson(writer);
  20. str = strBuf.GetString();
  21. }
  22. void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
  23. writer.StartObject();
  24. writer.Key("cmd");
  25. writer.String(cmd.c_str());
  26. writer.Key("param");
  27. writer.StartObject();
  28. writer.Key("token");
  29. writer.String(token.c_str());
  30. writer.EndObject();
  31. writer.EndObject();
  32. }
  33. bool jsonToObject(std::string& json){
  34. rapidjson::Document doc;
  35. doc.Parse(json.c_str());
  36. if(!doc.IsObject()){
  37. return false;
  38. }
  39. const auto end = doc.MemberEnd();
  40. if(end == doc.FindMember("cmd") || !doc["cmd"].IsString()){
  41. return false;
  42. }else{
  43. if(cmd != doc["cmd"].GetString())
  44. return false;
  45. }
  46. if(end == doc.FindMember("param") || !doc["param"].IsObject()) {
  47. return false;
  48. }else{
  49. if(end == doc.FindMember("token") || !doc["token"].IsInt()){
  50. return false;
  51. }else{
  52. token = doc["token"].GetString();
  53. }
  54. }
  55. return true;
  56. }
  57. };