publicFunExport.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <iostream>
  2. #include "publicFunExport.h"
  3. #include <memory>
  4. #include <math.h>
  5. #include <cstring>
  6. #include "cJSON.h"
  7. int str2int(const string str, int& ret) {
  8. if (str.size() == 0) return 1;
  9. ret = 0;
  10. int symbol = 0;
  11. for (int i = 0; i < str.size(); i++) {
  12. if (i == 0 && str[i] == '-') {
  13. symbol = 1;
  14. continue;
  15. }
  16. if (i == 0 && str[i] == '+') continue;
  17. if (i == 0) {
  18. while (str[i] == '0' && i < str.size()) {
  19. ++i;
  20. }
  21. if (i == str.size()) return 0;
  22. }
  23. if (str[i] < '0' || str[i] >'9') return 2;
  24. ret += (int)(str[i] - '0') * pow((double)10, (double)(str.size() - i - 1));
  25. }
  26. if (symbol) ret -= 2 * ret;
  27. return 0;
  28. }
  29. int str2int(const string str) {
  30. int ret;
  31. str2int(str, ret);
  32. return ret;
  33. }
  34. string int2str(const int num) {
  35. char cNum[12] = { 0 };
  36. sprintf(cNum, "%d", num);
  37. return string(cNum);
  38. }
  39. std::pair<bool, std::string> generateJsonStr(std::map<std::string, std::string>& objectArr)
  40. {
  41. auto dstJson = std::shared_ptr<cJSON>(cJSON_CreateObject(), [](cJSON* p) {
  42. if(p)
  43. cJSON_Delete(p);
  44. });
  45. for(auto it = objectArr.begin(); it != objectArr.end(); it++)
  46. {
  47. if (it->first.empty())
  48. return std::make_pair(false, "param is empty");
  49. cJSON_AddStringToObject(dstJson.get(), it->first.c_str(), it->second.c_str());
  50. }
  51. auto dstPrintStr = std::shared_ptr<char>(cJSON_PrintUnformatted(dstJson.get()), [](char* p) {
  52. if (p)
  53. free(p);
  54. });
  55. return std::make_pair(true, dstPrintStr.get());
  56. }
  57. std::vector<std::string> parseJsonStr(const std::string jsonStr,const std::vector<std::string>& keys) {
  58. std::vector<std::string> values;
  59. std::vector<std::string>::const_iterator it = keys.begin();
  60. auto cJsonPtr = std::shared_ptr<cJSON>(cJSON_Parse(jsonStr.c_str()), [](cJSON* json) {
  61. if (json) cJSON_Delete(json);
  62. });
  63. while (it != keys.end()) {
  64. auto ret = cJSON_GetObjectItem(cJsonPtr.get(),(*it).c_str());
  65. switch (ret->type)
  66. {
  67. case cJSON_False:
  68. values.push_back(STR_FALSE);
  69. break;
  70. case cJSON_True:
  71. values.push_back(STR_TRUE);
  72. break;
  73. case cJSON_NULL:
  74. values.push_back(STR_NULL);
  75. break;
  76. case cJSON_Number:
  77. values.push_back(int2str(ret->valueint));
  78. break;
  79. case cJSON_String:
  80. values.push_back(ret->valuestring);
  81. break;
  82. default:
  83. values.push_back("error type");
  84. break;
  85. }
  86. it++;
  87. }
  88. return values;
  89. }
  90. RVCJson::RVCJson():pJson(NULL) {}
  91. RVCJson::RVCJson(bool notNewArray):pJson(NULL) {
  92. if (notNewArray) {
  93. pJson = cJSON_CreateObject();
  94. }
  95. else {
  96. pJson = cJSON_CreateArray();
  97. }
  98. }
  99. RVCJson::~RVCJson() {
  100. }
  101. void RVCJson::SetJson(const char* jsonStr) {
  102. if (pJson)
  103. cJSON_Delete(pJson);
  104. pJson = cJSON_Parse(jsonStr);
  105. }
  106. void RVCJson::SetJson(RVCJson* rvcJson) {
  107. if (pJson) cJSON_Delete(pJson);
  108. if (!rvcJson) return;
  109. pJson = cJSON_Parse(cJSON_PrintUnformatted(rvcJson->pJson));
  110. }
  111. void RVCJson::AddNullToObject(const char*key) {
  112. cJSON_AddNullToObject(pJson, key);
  113. }
  114. void RVCJson::AddBoolToObject(const char*key, bool value)
  115. {
  116. if (value) {
  117. cJSON_AddTrueToObject(pJson, key);
  118. }
  119. else {
  120. cJSON_AddFalseToObject(pJson, key);
  121. }
  122. }
  123. void RVCJson::AddNumberToObject(const char*key, double value)
  124. {
  125. cJSON_AddNumberToObject(pJson, key, value);
  126. }
  127. void RVCJson::AddStringToObject(const char*key, char*value)
  128. {
  129. cJSON_AddStringToObject(pJson, key, value);
  130. }
  131. void RVCJson::AddItemToObject(const char*key, RVCJson* rvcJson) {
  132. if (!key) return;
  133. if (!rvcJson) return;
  134. if (!pJson) return;
  135. RVCJson tmpJson;
  136. tmpJson.SetJson(rvcJson);
  137. cJSON_AddItemToObject(pJson, key, tmpJson.pJson);
  138. }
  139. void RVCJson::AddItemToArray(RVCJson* rvcJson) {
  140. if (!pJson) return;
  141. if (!rvcJson) return;
  142. if (pJson->type != cJSON_Array) return;
  143. RVCJson tmpJson;
  144. tmpJson.SetJson(rvcJson);
  145. cJSON_AddItemToArray(pJson,tmpJson.pJson);
  146. }
  147. RVCJson* RVCJson::Get() {
  148. return this;
  149. }
  150. char*RVCJson::GetJsonStr() {
  151. char* ret = cJSON_PrintUnformatted(pJson);
  152. if (ret == NULL) return NULL;
  153. char* retStr = new char[strlen(ret) + 1];
  154. memset(retStr,0, strlen(ret) + 1);
  155. memcpy(retStr, ret, strlen(ret));
  156. free(ret);
  157. return retStr;
  158. }
  159. bool RVCJson::GetBoolValue(const char*key) {
  160. cJSON* ret = cJSON_GetObjectItem(pJson, key);
  161. if (ret != NULL) {
  162. if (ret->type == cJSON_True) return true;
  163. }
  164. return false;
  165. }
  166. double RVCJson::GetNumberValue(const char*key) {
  167. cJSON* ret = cJSON_GetObjectItem(pJson, key);
  168. if (ret) {
  169. if (ret->type == cJSON_Number) return ret->valuedouble;
  170. }
  171. return 0.0;
  172. }
  173. int RVCJson::GetNumberIntValue(const char*key) {
  174. cJSON* ret = cJSON_GetObjectItem(pJson, key);
  175. if (ret) {
  176. if (ret->type == cJSON_Number) return ret->valueint;
  177. }
  178. return 0.0;
  179. }
  180. char* RVCJson::GetStringValue(const char*key) {
  181. cJSON* ret = cJSON_GetObjectItem(pJson, key);
  182. if (ret) {
  183. if (ret->type == cJSON_String) return ret->valuestring;
  184. if (ret->type == cJSON_NULL) return NULL;
  185. }
  186. return NULL;
  187. }
  188. RVCJson* RVCJson::GetJsonValue(const char*key){
  189. cJSON* ret = cJSON_GetObjectItem(pJson, key);
  190. RVCJson *rvcJson = new RVCJson();
  191. rvcJson->SetJson(cJSON_PrintUnformatted(ret));
  192. return rvcJson;
  193. }
  194. int RVCJson::GetArraySize() {
  195. if (!pJson) return 0;
  196. return cJSON_GetArraySize(pJson);
  197. }
  198. RVCJson* RVCJson::GetArrayValue(int index) {
  199. if (!pJson) return NULL;
  200. cJSON* ret = cJSON_GetArrayItem(pJson,index);
  201. RVCJson* rvcJson = new RVCJson();
  202. rvcJson->SetJson(cJSON_PrintUnformatted(ret));
  203. return rvcJson;
  204. }
  205. void RVCJson::Destory() {
  206. if (pJson) {
  207. cJSON_Delete(pJson);
  208. pJson = NULL;
  209. }
  210. }