CrowdednessConfig.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-11-18 09:32:16
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2021-11-29 09:14:48
  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. using namespace std;
  17. class Crowdedness
  18. {
  19. public:
  20. int timeType;
  21. int cdLevel1Min;
  22. int cdLevel1Max;
  23. int cdLevel2Min;
  24. int cdLevel2Max;
  25. int cdLevel3Min;
  26. int cdLevel3Max;
  27. public:
  28. Crowdedness() {}
  29. ~Crowdedness() {}
  30. /**
  31. * @description: json转换为object
  32. * @param {Value&} object
  33. * @return {*}
  34. */
  35. bool jsonToObject(const rapidjson::Value& object){
  36. const auto end = object.MemberEnd();
  37. if(end == object.FindMember("timeType") || !object["timeType"].IsString()){
  38. return false;
  39. }else{
  40. timeType = atoi(object["timeType"].GetString());
  41. }
  42. if(end == object.FindMember("cdLevel1Min") || !object["cdLevel1Min"].IsString()){
  43. return false;
  44. }else{
  45. cdLevel1Min = atoi(object["cdLevel1Min"].GetString());
  46. }
  47. if(end == object.FindMember("cdLevel1Max") || !object["cdLevel1Max"].IsString()){
  48. return false;
  49. }else{
  50. cdLevel1Max = atoi(object["cdLevel1Max"].GetString());
  51. }
  52. if(end == object.FindMember("cdLevel2Min") || !object["cdLevel2Min"].IsString()){
  53. return false;
  54. }else{
  55. cdLevel2Min = atoi(object["cdLevel2Min"].GetString());
  56. }
  57. if(end == object.FindMember("cdLevel2Max") || !object["cdLevel2Max"].IsString()){
  58. return false;
  59. }else{
  60. cdLevel2Max = atoi(object["cdLevel2Max"].GetString());
  61. }
  62. if(end == object.FindMember("cdLevel3Min") || !object["cdLevel3Min"].IsString()){
  63. return false;
  64. }else{
  65. cdLevel3Min = atoi(object["cdLevel3Min"].GetString());
  66. }
  67. if(end == object.FindMember("cdLevel3Max") || !object["cdLevel3Max"].IsString()){
  68. return false;
  69. }else{
  70. cdLevel3Max = atoi(object["cdLevel3Max"].GetString());
  71. }
  72. return true;
  73. }
  74. };
  75. class CrowdednessConfig
  76. {
  77. public:
  78. vector<Crowdedness> CrowdednessVector;
  79. public:
  80. CrowdednessConfig() {}
  81. ~CrowdednessConfig() {}
  82. /**
  83. * @description: json转换为object
  84. * @param {string&} json
  85. * @return {*}
  86. */
  87. bool jsonToObject(const std::string& json)
  88. {
  89. rapidjson::Document doc;
  90. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  91. return false;
  92. }
  93. if(doc.IsArray())
  94. {
  95. const rapidjson::Value& objs = doc;
  96. for(int i = 0; i < (int)(objs.Size()); i++){
  97. Crowdedness crowdedness;
  98. if(!crowdedness.jsonToObject(objs[i])){
  99. return false;
  100. }
  101. CrowdednessVector.push_back(crowdedness);
  102. }
  103. }else{
  104. return false;
  105. }
  106. return true;
  107. }
  108. };