findDevMsg.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. doc.Parse(json.c_str());
  32. if(!doc.IsObject()){
  33. return false;
  34. }
  35. // get members
  36. const auto end = doc.MemberEnd();
  37. // json_type
  38. // if(end == doc.FindMember("requestId") || !doc["requestId"].IsString()) {
  39. // return false;
  40. // }else{
  41. // requestId = doc["requestId"].GetString();
  42. // }
  43. if(end == doc.FindMember("ServerIP") || !doc["ServerIP"].IsString()) {
  44. return false;
  45. }else{
  46. ServerIP = doc["ServerIP"].GetString();
  47. }
  48. if(end == doc.FindMember("ServerPort") || !doc["ServerPort"].IsInt()) {
  49. return false;
  50. }else{
  51. ServerPort = doc["ServerPort"].GetInt();
  52. }
  53. if(end == doc.FindMember("DockerAddress") || !doc["DockerAddress"].IsString()) {
  54. return false;
  55. }else{
  56. DockerAddress = doc["DockerAddress"].GetString();
  57. }
  58. if(end == doc.FindMember("DockerPort") || !doc["DockerPort"].IsInt()) {
  59. return false;
  60. }else{
  61. DockerPort = doc["DockerPort"].GetInt();
  62. }
  63. if(end != doc.FindMember("attr1") && doc["attr1"].IsString()) {
  64. attr1 = doc["attr1"].GetString();
  65. }
  66. if(end != doc.FindMember("attr2") && doc["attr2"].IsString()) {
  67. attr1 = doc["attr2"].GetString();
  68. }
  69. if(end != doc.FindMember("attr3") && doc["attr3"].IsString()) {
  70. attr3 = doc["attr3"].GetString();
  71. }
  72. return true;
  73. }
  74. };
  75. #endif