NettyHttpResultMsg.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-11-03 15:16:14
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-05-13 10:11:19
  8. */
  9. #pragma once
  10. #include <iostream>
  11. #include <rapidjson/document.h>
  12. #include <rapidjson/rapidjson.h>
  13. #include <rapidjson/stringbuffer.h>
  14. #include <rapidjson/writer.h>
  15. #include <vector>
  16. #include "Util/logger.h"
  17. #include "Util/NoticeCenter.h"
  18. #include "Poller/EventPoller.h"
  19. #include "Util/SqlPool.h"
  20. #include "Network/TcpClient.h"
  21. #include "Poller/Timer.h"
  22. #include "Notices.h"
  23. using namespace std;
  24. template <class T1, class T2>
  25. class NettyHttpResultMsg
  26. {
  27. public:
  28. std::string code;
  29. std::string msg;
  30. vector<T1> datas;
  31. vector<T2> extendDatas;
  32. bool notOk;
  33. bool ok;
  34. public:
  35. NettyHttpResultMsg(){}
  36. ~NettyHttpResultMsg(){}
  37. /**
  38. * @description: json转码为对象
  39. * @param {const std::string& json} json数据
  40. * @return {*}
  41. */
  42. bool jsonToObject(const std::string& json)
  43. {
  44. rapidjson::Document doc;
  45. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  46. return false;
  47. }
  48. // get members
  49. const auto end = doc.MemberEnd();
  50. if (end == doc.FindMember("code") || !doc["code"].IsString()) {
  51. return false;
  52. } else {
  53. code = doc["code"].GetString();
  54. }
  55. if (end == doc.FindMember("notOk") || !doc["notOk"].IsBool()) {
  56. return false;
  57. } else {
  58. notOk = doc["notOk"].GetBool();
  59. }
  60. if (end == doc.FindMember("ok") || !doc["ok"].IsBool()) {
  61. return false;
  62. } else {
  63. ok = doc["ok"].GetBool();
  64. }
  65. if (end != doc.FindMember("data")) {
  66. if(doc["data"].IsObject()){
  67. T1 data;
  68. const rapidjson::Value& object = doc["data"];
  69. if(!(data.jsonToObject(object))){
  70. return false;
  71. }
  72. datas.push_back(data);
  73. }else if(doc["data"].IsArray()){
  74. const rapidjson::Value& objs = doc["data"];
  75. for (size_t i = 0; i < objs.Size(); i++) {
  76. const rapidjson::Value& obj = objs[i];
  77. T1 data;
  78. if(!(data.jsonToObject(obj))){
  79. return false;
  80. }
  81. datas.push_back(data);
  82. }
  83. }
  84. }
  85. if (end != doc.FindMember("extendData")) {
  86. if(doc["extendData"].IsObject()){
  87. T2 extendData;
  88. const rapidjson::Value& object = doc["extendData"];
  89. if(!(extendData.jsonToObject(object))){
  90. return false;
  91. }
  92. extendDatas.push_back(extendData);
  93. }else if(doc["extendData"].IsArray()){
  94. const rapidjson::Value& objs = doc["extendData"];
  95. for (size_t i = 0; i < objs.Size(); ++i) {
  96. const rapidjson::Value& obj = objs[i];
  97. T2 extendData;
  98. if(!(extendData.jsonToObject(obj))){
  99. return false;
  100. }
  101. extendDatas.push_back(extendData);
  102. }
  103. }
  104. }
  105. return true;
  106. }
  107. int32_t ClearTable(std::string tableName)
  108. {
  109. return OK;
  110. }
  111. };