tjson.c 9.2 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
X
Xiaoyu Wang 已提交
17 18
#include "tjson.h"
#include "cJSON.h"
S
Shengliang Guan 已提交
19
#include "taoserror.h"
X
Xiaoyu Wang 已提交
20

X
Xiaoyu Wang 已提交
21 22 23 24 25 26 27
SJson* tjsonCreateObject() {
  SJson* pJson = cJSON_CreateObject();
  if (pJson == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
  }
  return pJson;
}
X
Xiaoyu Wang 已提交
28

X
Xiaoyu Wang 已提交
29 30 31 32 33 34 35 36
SJson* tjsonCreateArray() {
  SJson* pJson = cJSON_CreateArray();
  if (pJson == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
  }
  return pJson;
}

S
monitor  
Shengliang Guan 已提交
37 38 39 40 41
void tjsonDelete(SJson* pJson) {
  if (pJson != NULL) {
    cJSON_Delete((cJSON*)pJson);
  }
}
X
Xiaoyu Wang 已提交
42 43 44

int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number) {
  char tmp[40] = {0};
X
Xiaoyu Wang 已提交
45
  snprintf(tmp, sizeof(tmp), "%" PRId64, number);
X
Xiaoyu Wang 已提交
46 47 48
  return tjsonAddStringToObject(pJson, pName, tmp);
}

X
Xiaoyu Wang 已提交
49
int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number) {
X
Xiaoyu Wang 已提交
50 51 52 53 54 55
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_FAILED;
  }

  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
56 57
}

X
Xiaoyu Wang 已提交
58
int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean) {
X
Xiaoyu Wang 已提交
59 60 61 62 63 64
  if (NULL == cJSON_AddBoolToObject((cJSON*)pJson, pName, boolean)) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_FAILED;
  }

  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
65 66
}

X
Xiaoyu Wang 已提交
67
int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal) {
X
Xiaoyu Wang 已提交
68 69 70 71 72 73
  if (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal)) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_FAILED;
  }

  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
74 75
}

X
Xiaoyu Wang 已提交
76 77 78 79 80 81 82
SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) {
  SJson* ret = (SJson*)cJSON_AddArrayToObject((cJSON*)pJson, pName);
  if (ret == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
  }
  return ret;
}
X
Xiaoyu Wang 已提交
83

S
Shengliang Guan 已提交
84
int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem) {
X
Xiaoyu Wang 已提交
85 86 87 88 89 90
  if (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem)) {
    return TSDB_CODE_SUCCESS;
  }

  terrno = TSDB_CODE_OUT_OF_MEMORY;
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
91 92 93
}

int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem) {
X
Xiaoyu Wang 已提交
94 95 96 97 98 99
  if (cJSON_AddItemToArray((cJSON*)pJson, pItem)) {
    return TSDB_CODE_SUCCESS;
  }

  terrno = TSDB_CODE_OUT_OF_MEMORY;
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

int32_t tjsonAddObject(SJson* pJson, const char* pName, FToJson func, const void* pObj) {
  if (NULL == pObj) {
    return TSDB_CODE_SUCCESS;
  }

  SJson* pJobj = tjsonCreateObject();
  if (NULL == pJobj || TSDB_CODE_SUCCESS != func(pObj, pJobj)) {
    tjsonDelete(pJobj);
    return TSDB_CODE_FAILED;
  }
  return tjsonAddItemToObject(pJson, pName, pJobj);
}

int32_t tjsonAddItem(SJson* pJson, FToJson func, const void* pObj) {
  SJson* pJobj = tjsonCreateObject();
  if (NULL == pJobj || TSDB_CODE_SUCCESS != func(pObj, pJobj)) {
    tjsonDelete(pJobj);
    return TSDB_CODE_FAILED;
  }
  return tjsonAddItemToArray(pJson, pJobj);
}

L
fix  
Liu Jicong 已提交
124 125
int32_t tjsonAddArray(SJson* pJson, const char* pName, FToJson func, const void* pArray, int32_t itemSize,
                      int32_t num) {
X
Xiaoyu Wang 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  if (num > 0) {
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
    if (NULL == pJsonArray) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    for (size_t i = 0; i < num; ++i) {
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
    }
  }
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
141
char* tjsonToString(const SJson* pJson) { return cJSON_Print((cJSON*)pJson); }
X
Xiaoyu Wang 已提交
142

S
Shengliang Guan 已提交
143
char* tjsonToUnformattedString(const SJson* pJson) { return cJSON_PrintUnformatted((cJSON*)pJson); }
X
Xiaoyu Wang 已提交
144

S
Shengliang Guan 已提交
145
SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName) { return cJSON_GetObjectItem(pJson, pName); }
X
Xiaoyu Wang 已提交
146

wafwerar's avatar
wafwerar 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
int32_t tjsonGetObjectName(const SJson* pJson, char** pName) {
  *pName = ((cJSON*)pJson)->string;
  if (NULL == *pName) {
    return TSDB_CODE_FAILED;
  }
  return TSDB_CODE_SUCCESS;
}

int32_t tjsonGetObjectValueString(const SJson* pJson, char** pValueString) {
  *pValueString = ((cJSON*)pJson)->valuestring;
  if (NULL == *pValueString) {
    return TSDB_CODE_FAILED;
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
int32_t tjsonGetStringValue(const SJson* pJson, const char* pName, char* pVal) {
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
  if (NULL == p) {
    return TSDB_CODE_FAILED;
  }
  strcpy(pVal, p);
  return TSDB_CODE_SUCCESS;
}

int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal) {
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
  if (NULL == p) {
    return TSDB_CODE_FAILED;
  }
  *pVal = strdup(p);
  return TSDB_CODE_SUCCESS;
}

int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
  if (NULL == p) {
    return TSDB_CODE_FAILED;
  }
X
Xiaoyu Wang 已提交
186

X
Xiaoyu Wang 已提交
187
  *pVal = strtol(p, NULL, 10);
L
fix  
Liu Jicong 已提交
188
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
  int64_t val = 0;
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
  *pVal = val;
  return code;
}

int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
  int64_t val = 0;
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
  *pVal = val;
  return code;
}

int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal) {
  int64_t val = 0;
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
  *pVal = val;
  return code;
}

int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pVal) {
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
  if (NULL == p) {
    return TSDB_CODE_FAILED;
  }
X
Xiaoyu Wang 已提交
217

X
Xiaoyu Wang 已提交
218
  *pVal = strtoul(p, NULL, 10);
L
fix  
Liu Jicong 已提交
219
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
220 221
}

X
Xiaoyu Wang 已提交
222 223
int32_t tjsonGetUIntValue(const SJson* pJson, const char* pName, uint32_t* pVal) {
  uint64_t val = 0;
L
fix  
Liu Jicong 已提交
224
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
X
Xiaoyu Wang 已提交
225 226 227 228
  *pVal = val;
  return code;
}

X
Xiaoyu Wang 已提交
229 230
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
  uint64_t val = 0;
L
fix  
Liu Jicong 已提交
231
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
X
Xiaoyu Wang 已提交
232 233 234 235 236 237
  *pVal = val;
  return code;
}

int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
X
Xiaoyu Wang 已提交
238
  if (!cJSON_IsBool(pObject)) {
X
Xiaoyu Wang 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    return TSDB_CODE_FAILED;
  }
  *pVal = cJSON_IsTrue(pObject) ? true : false;
  return TSDB_CODE_SUCCESS;
}

int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
  if (!cJSON_IsNumber(pObject)) {
    return TSDB_CODE_FAILED;
  }
  *pVal = cJSON_GetNumberValue(pObject);
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
254
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
X
Xiaoyu Wang 已提交
255

S
Shengliang Guan 已提交
256
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
X
Xiaoyu Wang 已提交
257 258 259 260 261 262 263 264 265

int32_t tjsonToObject(const SJson* pJson, const char* pName, FToObject func, void* pObj) {
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
  if (NULL == pJsonObj) {
    return TSDB_CODE_FAILED;
  }
  return func(pJsonObj, pObj);
}

X
Xiaoyu Wang 已提交
266 267 268 269 270 271 272 273 274
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FToObject func, void** pObj, int32_t objSize) {
  if (objSize <= 0) {
    return TSDB_CODE_SUCCESS;
  }

  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
  if (NULL == pJsonObj) {
    return TSDB_CODE_FAILED;
  }
wafwerar's avatar
wafwerar 已提交
275
  *pObj = taosMemoryCalloc(1, objSize);
X
Xiaoyu Wang 已提交
276 277 278 279 280 281
  if (NULL == *pObj) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  return func(pJsonObj, *pObj);
}

X
Xiaoyu Wang 已提交
282 283
int32_t tjsonToArray(const SJson* pJson, const char* pName, FToObject func, void* pArray, int32_t itemSize) {
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
L
fix  
Liu Jicong 已提交
284
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
X
Xiaoyu Wang 已提交
285 286 287 288 289 290 291 292 293
  for (int32_t i = 0; i < size; ++i) {
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
  }
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
294
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
wmmhello's avatar
wmmhello 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

bool tjsonValidateJson(const char *jIn) {
  if (!jIn){
    return false;
  }

  // set json real data
  cJSON *root = cJSON_Parse(jIn);
  if (root == NULL){
    return false;
  }

  if(!cJSON_IsObject(root)){
    return false;
  }
  int size = cJSON_GetArraySize(root);
  for(int i = 0; i < size; i++) {
    cJSON* item = cJSON_GetArrayItem(root, i);
    if (!item) {
      return false;
    }

    char* jsonKey = item->string;
    if (!jsonKey) return false;
wmmhello's avatar
wmmhello 已提交
319 320
    for (size_t j = 0; j < strlen(jsonKey); ++j) {
      if (isprint(jsonKey[j]) == 0) return false;
wmmhello's avatar
wmmhello 已提交
321 322 323 324 325 326 327
    }

    if (item->type == cJSON_Object || item->type == cJSON_Array) {
      return false;
    }
  }
  return true;
wafwerar's avatar
wafwerar 已提交
328 329 330
}

const char* tjsonGetError() { return cJSON_GetErrorPtr(); }