123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include <iostream>
- #include "publicFunExport.h"
- #include <memory>
- #include <math.h>
- #include <cstring>
- #include "cJSON.h"
- int str2int(const string str, int& ret) {
- if (str.size() == 0) return 1;
- ret = 0;
- int symbol = 0;
- for (int i = 0; i < str.size(); i++) {
- if (i == 0 && str[i] == '-') {
- symbol = 1;
- continue;
- }
- if (i == 0 && str[i] == '+') continue;
- if (i == 0) {
- while (str[i] == '0' && i < str.size()) {
- ++i;
- }
- if (i == str.size()) return 0;
- }
- if (str[i] < '0' || str[i] >'9') return 2;
- ret += (int)(str[i] - '0') * pow((double)10, (double)(str.size() - i - 1));
- }
- if (symbol) ret -= 2 * ret;
- return 0;
- }
- int str2int(const string str) {
- int ret;
- str2int(str, ret);
- return ret;
- }
- string int2str(const int num) {
- char cNum[12] = { 0 };
- sprintf(cNum, "%d", num);
- return string(cNum);
- }
- std::pair<bool, std::string> generateJsonStr(std::map<std::string, std::string>& objectArr)
- {
- auto dstJson = std::shared_ptr<cJSON>(cJSON_CreateObject(), [](cJSON* p) {
- if(p)
- cJSON_Delete(p);
- });
- for(auto it = objectArr.begin(); it != objectArr.end(); it++)
- {
- if (it->first.empty())
- return std::make_pair(false, "param is empty");
- cJSON_AddStringToObject(dstJson.get(), it->first.c_str(), it->second.c_str());
- }
- auto dstPrintStr = std::shared_ptr<char>(cJSON_PrintUnformatted(dstJson.get()), [](char* p) {
- if (p)
- free(p);
- });
- return std::make_pair(true, dstPrintStr.get());
- }
- std::vector<std::string> parseJsonStr(const std::string jsonStr,const std::vector<std::string>& keys) {
- std::vector<std::string> values;
- std::vector<std::string>::const_iterator it = keys.begin();
- auto cJsonPtr = std::shared_ptr<cJSON>(cJSON_Parse(jsonStr.c_str()), [](cJSON* json) {
- if (json) cJSON_Delete(json);
- });
- while (it != keys.end()) {
- auto ret = cJSON_GetObjectItem(cJsonPtr.get(),(*it).c_str());
- switch (ret->type)
- {
- case cJSON_False:
- values.push_back(STR_FALSE);
- break;
- case cJSON_True:
- values.push_back(STR_TRUE);
- break;
- case cJSON_NULL:
- values.push_back(STR_NULL);
- break;
- case cJSON_Number:
- values.push_back(int2str(ret->valueint));
- break;
- case cJSON_String:
- values.push_back(ret->valuestring);
- break;
- default:
- values.push_back("error type");
- break;
- }
- it++;
- }
- return values;
- }
- RVCJson::RVCJson():pJson(NULL) {}
- RVCJson::RVCJson(bool notNewArray):pJson(NULL) {
- if (notNewArray) {
- pJson = cJSON_CreateObject();
- }
- else {
- pJson = cJSON_CreateArray();
- }
-
- }
- RVCJson::~RVCJson() {
-
- }
- void RVCJson::SetJson(const char* jsonStr) {
- if (pJson)
- cJSON_Delete(pJson);
- pJson = cJSON_Parse(jsonStr);
- }
- void RVCJson::SetJson(RVCJson* rvcJson) {
- if (pJson) cJSON_Delete(pJson);
- if (!rvcJson) return;
- pJson = cJSON_Parse(cJSON_PrintUnformatted(rvcJson->pJson));
- }
- void RVCJson::AddNullToObject(const char*key) {
- cJSON_AddNullToObject(pJson, key);
- }
- void RVCJson::AddBoolToObject(const char*key, bool value)
- {
- if (value) {
- cJSON_AddTrueToObject(pJson, key);
- }
- else {
- cJSON_AddFalseToObject(pJson, key);
- }
- }
- void RVCJson::AddNumberToObject(const char*key, double value)
- {
- cJSON_AddNumberToObject(pJson, key, value);
- }
- void RVCJson::AddStringToObject(const char*key, char*value)
- {
- cJSON_AddStringToObject(pJson, key, value);
- }
- void RVCJson::AddItemToObject(const char*key, RVCJson* rvcJson) {
- if (!key) return;
- if (!rvcJson) return;
- if (!pJson) return;
- RVCJson tmpJson;
- tmpJson.SetJson(rvcJson);
- cJSON_AddItemToObject(pJson, key, tmpJson.pJson);
- }
- void RVCJson::AddItemToArray(RVCJson* rvcJson) {
- if (!pJson) return;
- if (!rvcJson) return;
- if (pJson->type != cJSON_Array) return;
- RVCJson tmpJson;
- tmpJson.SetJson(rvcJson);
- cJSON_AddItemToArray(pJson,tmpJson.pJson);
- }
- RVCJson* RVCJson::Get() {
- return this;
- }
- char*RVCJson::GetJsonStr() {
- char* ret = cJSON_PrintUnformatted(pJson);
- if (ret == NULL) return NULL;
- char* retStr = new char[strlen(ret) + 1];
- memset(retStr,0, strlen(ret) + 1);
- memcpy(retStr, ret, strlen(ret));
- free(ret);
- return retStr;
- }
- bool RVCJson::GetBoolValue(const char*key) {
- cJSON* ret = cJSON_GetObjectItem(pJson, key);
- if (ret != NULL) {
- if (ret->type == cJSON_True) return true;
- }
- return false;
- }
- double RVCJson::GetNumberValue(const char*key) {
-
- cJSON* ret = cJSON_GetObjectItem(pJson, key);
- if (ret) {
- if (ret->type == cJSON_Number) return ret->valuedouble;
- }
- return 0.0;
- }
- int RVCJson::GetNumberIntValue(const char*key) {
-
- cJSON* ret = cJSON_GetObjectItem(pJson, key);
- if (ret) {
- if (ret->type == cJSON_Number) return ret->valueint;
- }
- return 0.0;
- }
- char* RVCJson::GetStringValue(const char*key) {
- cJSON* ret = cJSON_GetObjectItem(pJson, key);
- if (ret) {
- if (ret->type == cJSON_String) return ret->valuestring;
- if (ret->type == cJSON_NULL) return NULL;
- }
- return NULL;
- }
- RVCJson* RVCJson::GetJsonValue(const char*key){
- cJSON* ret = cJSON_GetObjectItem(pJson, key);
- RVCJson *rvcJson = new RVCJson();
- rvcJson->SetJson(cJSON_PrintUnformatted(ret));
- return rvcJson;
- }
- int RVCJson::GetArraySize() {
- if (!pJson) return 0;
- return cJSON_GetArraySize(pJson);
- }
- RVCJson* RVCJson::GetArrayValue(int index) {
- if (!pJson) return NULL;
- cJSON* ret = cJSON_GetArrayItem(pJson,index);
- RVCJson* rvcJson = new RVCJson();
- rvcJson->SetJson(cJSON_PrintUnformatted(ret));
- return rvcJson;
- }
- void RVCJson::Destory() {
- if (pJson) {
- cJSON_Delete(pJson);
- pJson = NULL;
- }
- }
|