SendBird.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. doc.Parse(json.c_str());
  60. if(!doc.IsObject()){
  61. return false;
  62. }
  63. // get members
  64. const auto end = doc.MemberEnd();
  65. // json_type
  66. if(end == doc.FindMember("birdImageBase64") || !doc["birdImageBase64"].IsString()) {
  67. return false;
  68. }else{
  69. birdImageBase64 = doc["birdImageBase64"].GetString();
  70. }
  71. if (end == doc.FindMember("birdNum") || !doc["birdNum"].IsInt()) {
  72. return false;
  73. } else {
  74. birdNum = doc["birdNum"].GetInt();
  75. }
  76. if(end == doc.FindMember("birdType") || !doc["birdType"].IsString()) {
  77. return false;
  78. }else{
  79. birdType = doc["birdType"].GetString();
  80. }
  81. if (end == doc.FindMember("degree") || !doc["degree"].IsInt()) {
  82. return false;
  83. } else {
  84. degree = doc["degree"].GetInt();
  85. }
  86. if(end == doc.FindMember("deviceUuid") || !doc["deviceUuid"].IsString()) {
  87. return false;
  88. }else{
  89. deviceUuid = doc["deviceUuid"].GetString();
  90. }
  91. if(end == doc.FindMember("heightRange") || !doc["heightRange"].IsString()) {
  92. return false;
  93. }else{
  94. heightRange = doc["heightRange"].GetString();
  95. }
  96. if(end == doc.FindMember("repeStatus") || !doc["repeStatus"].IsInt()) {
  97. return false;
  98. }else{
  99. repeStatus = doc["repeStatus"].GetInt();
  100. }
  101. if(end == doc.FindMember("warningTime") || !doc["warningTime"].IsString()) {
  102. return false;
  103. }else{
  104. warningTime = doc["warningTime"].GetString();
  105. }
  106. return true;
  107. }
  108. };