SendBird.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 SendBird
  9. {
  10. private:
  11. public:
  12. string birdImageBase64;
  13. int birdNum;
  14. string birdType;
  15. int degree;
  16. string deviceUuid;
  17. string heightRange;
  18. string remark;
  19. int repeStatus;
  20. string warningTime;
  21. string attr4;
  22. string attr5;
  23. SendBird(){};
  24. ~SendBird(){};
  25. void objectToJson(std::string& str){
  26. rapidjson::StringBuffer strBuf;
  27. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  28. this->objectToJson(writer);
  29. str = strBuf.GetString();
  30. }
  31. void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
  32. writer.StartObject();
  33. writer.Key("birdImageBase64");
  34. writer.String(birdImageBase64.c_str());
  35. writer.Key("birdNum");
  36. writer.Int(birdNum);
  37. writer.Key("birdType");
  38. writer.String(birdType.c_str());
  39. writer.Key("degree");
  40. writer.Int(degree);
  41. writer.Key("deviceUuid");
  42. writer.String(deviceUuid.c_str());
  43. writer.Key("heightRange");
  44. writer.String(heightRange.c_str());
  45. writer.Key("remark");
  46. writer.String(remark.c_str());
  47. writer.Key("repeStatus");
  48. writer.Int(repeStatus);
  49. writer.Key("warningTime");
  50. writer.String(warningTime.c_str());
  51. writer.Key("attr4");
  52. writer.String(attr4.c_str());
  53. writer.Key("attr5");
  54. writer.String(attr5.c_str());
  55. writer.EndObject();
  56. }
  57. bool jsonToObject(std::string& json){
  58. rapidjson::Document doc;
  59. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  60. return false;
  61. }
  62. // get members
  63. const auto end = doc.MemberEnd();
  64. // json_type
  65. if(end == doc.FindMember("birdImageBase64") || !doc["birdImageBase64"].IsString()) {
  66. return false;
  67. }else{
  68. birdImageBase64 = doc["birdImageBase64"].GetString();
  69. }
  70. if (end == doc.FindMember("birdNum") || !doc["birdNum"].IsInt()) {
  71. return false;
  72. } else {
  73. birdNum = doc["birdNum"].GetInt();
  74. }
  75. if(end == doc.FindMember("birdType") || !doc["birdType"].IsString()) {
  76. return false;
  77. }else{
  78. birdType = doc["birdType"].GetString();
  79. }
  80. if (end == doc.FindMember("degree") || !doc["degree"].IsInt()) {
  81. return false;
  82. } else {
  83. degree = doc["degree"].GetInt();
  84. }
  85. if(end == doc.FindMember("deviceUuid") || !doc["deviceUuid"].IsString()) {
  86. return false;
  87. }else{
  88. deviceUuid = doc["deviceUuid"].GetString();
  89. }
  90. if(end == doc.FindMember("heightRange") || !doc["heightRange"].IsString()) {
  91. return false;
  92. }else{
  93. heightRange = doc["heightRange"].GetString();
  94. }
  95. if(end == doc.FindMember("repeStatus") || !doc["repeStatus"].IsInt()) {
  96. return false;
  97. }else{
  98. repeStatus = doc["repeStatus"].GetInt();
  99. }
  100. if(end == doc.FindMember("warningTime") || !doc["warningTime"].IsString()) {
  101. return false;
  102. }else{
  103. warningTime = doc["warningTime"].GetString();
  104. }
  105. return true;
  106. }
  107. };