NettyHttpResultMsg.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-11-03 15:16:14
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-09-28 14:21:17
  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. doc.Parse(json.c_str());
  46. if(!doc.IsObject()){
  47. return false;
  48. }
  49. // get members
  50. const auto end = doc.MemberEnd();
  51. if (end == doc.FindMember("code") || !doc["code"].IsString()) {
  52. return false;
  53. } else {
  54. code = doc["code"].GetString();
  55. }
  56. if (end == doc.FindMember("notOk") || !doc["notOk"].IsBool()) {
  57. return false;
  58. } else {
  59. notOk = doc["notOk"].GetBool();
  60. }
  61. if (end == doc.FindMember("ok") || !doc["ok"].IsBool()) {
  62. return false;
  63. } else {
  64. ok = doc["ok"].GetBool();
  65. }
  66. if (end != doc.FindMember("data")) {
  67. if(doc["data"].IsObject()){
  68. T1 data;
  69. const rapidjson::Value& object = doc["data"];
  70. if(!(data.jsonToObject(object))){
  71. return false;
  72. }
  73. datas.push_back(data);
  74. }else if(doc["data"].IsArray()){
  75. const rapidjson::Value& objs = doc["data"];
  76. for (size_t i = 0; i < objs.Size(); i++) {
  77. const rapidjson::Value& obj = objs[i];
  78. T1 data;
  79. if(!(data.jsonToObject(obj))){
  80. return false;
  81. }
  82. datas.push_back(data);
  83. }
  84. }
  85. }
  86. if (end != doc.FindMember("extendData")) {
  87. if(doc["extendData"].IsObject()){
  88. T2 extendData;
  89. const rapidjson::Value& object = doc["extendData"];
  90. if(!(extendData.jsonToObject(object))){
  91. return false;
  92. }
  93. extendDatas.push_back(extendData);
  94. }else if(doc["extendData"].IsArray()){
  95. const rapidjson::Value& objs = doc["extendData"];
  96. for (size_t i = 0; i < objs.Size(); ++i) {
  97. const rapidjson::Value& obj = objs[i];
  98. T2 extendData;
  99. if(!(extendData.jsonToObject(obj))){
  100. return false;
  101. }
  102. extendDatas.push_back(extendData);
  103. }
  104. }
  105. }
  106. return true;
  107. }
  108. int32_t ClearTable(std::string tableName)
  109. {
  110. return OK;
  111. }
  112. };