findDevMsg.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef __FINDDEVMSG_H_
  2. #define __FINDDEVMSG_H_
  3. #include <iostream>
  4. #include <vector>
  5. #include <rapidjson/document.h>
  6. #include <rapidjson/rapidjson.h>
  7. #include <rapidjson/stringbuffer.h>
  8. #include <rapidjson/writer.h>
  9. using namespace std;
  10. class findDevMsg
  11. {
  12. public:
  13. std::string requestId;
  14. std::string ServerIP;
  15. int ServerPort;
  16. std::string DockerAddress;
  17. int DockerPort;
  18. std::string attr1;
  19. std::string attr2;
  20. std::string attr3;
  21. public:
  22. findDevMsg(){}
  23. ~findDevMsg(){}
  24. /**
  25. * @description: json转换为object
  26. * @param {string&} json
  27. * @return {*}
  28. */
  29. bool JsonToObject(std::string& json){
  30. rapidjson::Document doc;
  31. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  32. return false;
  33. }
  34. // get members
  35. const auto end = doc.MemberEnd();
  36. // json_type
  37. // if(end == doc.FindMember("requestId") || !doc["requestId"].IsString()) {
  38. // return false;
  39. // }else{
  40. // requestId = doc["requestId"].GetString();
  41. // }
  42. if(end == doc.FindMember("ServerIP") || !doc["ServerIP"].IsString()) {
  43. return false;
  44. }else{
  45. ServerIP = doc["ServerIP"].GetString();
  46. }
  47. if(end == doc.FindMember("ServerPort") || !doc["ServerPort"].IsInt()) {
  48. return false;
  49. }else{
  50. ServerPort = doc["ServerPort"].GetInt();
  51. }
  52. if(end == doc.FindMember("DockerAddress") || !doc["DockerAddress"].IsString()) {
  53. return false;
  54. }else{
  55. DockerAddress = doc["DockerAddress"].GetString();
  56. }
  57. if(end == doc.FindMember("DockerPort") || !doc["DockerPort"].IsInt()) {
  58. return false;
  59. }else{
  60. DockerPort = doc["DockerPort"].GetInt();
  61. }
  62. if(end != doc.FindMember("attr1") && doc["attr1"].IsString()) {
  63. attr1 = doc["attr1"].GetString();
  64. }
  65. if(end != doc.FindMember("attr2") && doc["attr2"].IsString()) {
  66. attr1 = doc["attr2"].GetString();
  67. }
  68. if(end != doc.FindMember("attr3") && doc["attr3"].IsString()) {
  69. attr3 = doc["attr3"].GetString();
  70. }
  71. return true;
  72. }
  73. };
  74. #endif