tjson.c 10.5 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

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

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

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

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

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

X
Xiaoyu Wang 已提交
50
int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number) {
X
Xiaoyu Wang 已提交
51 52 53 54 55 56
  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 已提交
57 58
}

X
Xiaoyu Wang 已提交
59
int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean) {
X
Xiaoyu Wang 已提交
60 61 62 63 64 65
  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 已提交
66 67
}

X
Xiaoyu Wang 已提交
68
int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal) {
X
Xiaoyu Wang 已提交
69 70 71 72 73 74
  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 已提交
75 76
}

X
Xiaoyu Wang 已提交
77 78 79 80 81 82 83
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 已提交
84

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

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

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

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

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 已提交
125 126
int32_t tjsonAddArray(SJson* pJson, const char* pName, FToJson func, const void* pArray, int32_t itemSize,
                      int32_t num) {
X
Xiaoyu Wang 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  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;
}

X
Xiaoyu Wang 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
int32_t tjsonAddTArray(SJson* pJson, const char* pName, FToJson func, const SArray* pArray) {
  int32_t num = taosArrayGetSize(pArray);
  if (num > 0) {
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
    if (NULL == pJsonArray) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    for (int32_t i = 0; i < num; ++i) {
      int32_t code = tjsonAddItem(pJsonArray, func, taosArrayGet(pArray, i));
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
    }
  }
  return TSDB_CODE_SUCCESS;
}

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

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

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

wafwerar's avatar
wafwerar 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
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 已提交
181 182 183
int32_t tjsonGetStringValue(const SJson* pJson, const char* pName, char* pVal) {
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
  if (NULL == p) {
X
Xiaoyu Wang 已提交
184
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
185 186 187 188 189 190 191 192
  }
  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) {
X
Xiaoyu Wang 已提交
193
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
194 195 196 197 198 199 200 201
  }
  *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) {
X
Xiaoyu Wang 已提交
202
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
203
  }
wafwerar's avatar
wafwerar 已提交
204
#ifdef WINDOWS
S
Shengliang Guan 已提交
205
  sscanf(p, "%" PRId64, pVal);
wafwerar's avatar
wafwerar 已提交
206
#else
wafwerar's avatar
wafwerar 已提交
207
  *pVal = taosStr2Int64(p, NULL, 10);
wafwerar's avatar
wafwerar 已提交
208
#endif
L
fix  
Liu Jicong 已提交
209
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}

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) {
X
Xiaoyu Wang 已提交
236
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
237
  }
wafwerar's avatar
wafwerar 已提交
238
#ifdef WINDOWS
S
Shengliang Guan 已提交
239
  sscanf(p, "%" PRIu64, pVal);
wafwerar's avatar
wafwerar 已提交
240
#else
wafwerar's avatar
wafwerar 已提交
241
  *pVal = taosStr2UInt64(p, NULL, 10);
wafwerar's avatar
wafwerar 已提交
242
#endif
L
fix  
Liu Jicong 已提交
243
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
244 245
}

X
Xiaoyu Wang 已提交
246 247
int32_t tjsonGetUIntValue(const SJson* pJson, const char* pName, uint32_t* pVal) {
  uint64_t val = 0;
L
fix  
Liu Jicong 已提交
248
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
X
Xiaoyu Wang 已提交
249 250 251 252
  *pVal = val;
  return code;
}

X
Xiaoyu Wang 已提交
253 254
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
  uint64_t val = 0;
L
fix  
Liu Jicong 已提交
255
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
X
Xiaoyu Wang 已提交
256 257 258 259 260 261
  *pVal = val;
  return code;
}

int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
X
Xiaoyu Wang 已提交
262 263 264
  if (NULL == pObject) {
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
265
  if (!cJSON_IsBool(pObject)) {
X
Xiaoyu Wang 已提交
266 267 268 269 270 271 272 273
    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);
X
Xiaoyu Wang 已提交
274 275 276
  if (NULL == pObject) {
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
277 278 279 280 281 282 283
  if (!cJSON_IsNumber(pObject)) {
    return TSDB_CODE_FAILED;
  }
  *pVal = cJSON_GetNumberValue(pObject);
  return TSDB_CODE_SUCCESS;
}

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

S
Shengliang Guan 已提交
286
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
X
Xiaoyu Wang 已提交
287 288 289 290

int32_t tjsonToObject(const SJson* pJson, const char* pName, FToObject func, void* pObj) {
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
  if (NULL == pJsonObj) {
X
Xiaoyu Wang 已提交
291
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
292 293 294 295
  }
  return func(pJsonObj, pObj);
}

X
Xiaoyu Wang 已提交
296 297 298 299 300 301 302
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) {
X
Xiaoyu Wang 已提交
303
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
304
  }
wafwerar's avatar
wafwerar 已提交
305
  *pObj = taosMemoryCalloc(1, objSize);
X
Xiaoyu Wang 已提交
306 307 308 309 310 311
  if (NULL == *pObj) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  return func(pJsonObj, *pObj);
}

X
Xiaoyu Wang 已提交
312 313
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 已提交
314
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
X
Xiaoyu Wang 已提交
315 316 317 318 319 320 321 322 323
  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;
}

X
Xiaoyu Wang 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FToObject func, SArray** pArray, int32_t itemSize) {
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
  int32_t      size = tjsonGetArraySize(jArray);
  if (size > 0) {
    *pArray = taosArrayInit(size, itemSize);
    if (NULL == *pArray) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    taosArraySetSize(*pArray, size);
    for (int32_t i = 0; i < size; ++i) {
      int32_t code = func(tjsonGetArrayItem(jArray, i), taosArrayGet(*pArray, i));
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
    }
  }
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
343
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
wmmhello's avatar
wmmhello 已提交
344

X
Xiaoyu Wang 已提交
345 346
bool tjsonValidateJson(const char* jIn) {
  if (!jIn) {
wmmhello's avatar
wmmhello 已提交
347 348 349 350
    return false;
  }

  // set json real data
X
Xiaoyu Wang 已提交
351 352
  cJSON* root = cJSON_Parse(jIn);
  if (root == NULL) {
wmmhello's avatar
wmmhello 已提交
353 354 355
    return false;
  }

X
Xiaoyu Wang 已提交
356
  if (!cJSON_IsObject(root)) {
wmmhello's avatar
wmmhello 已提交
357 358 359
    return false;
  }
  int size = cJSON_GetArraySize(root);
X
Xiaoyu Wang 已提交
360
  for (int i = 0; i < size; i++) {
wmmhello's avatar
wmmhello 已提交
361 362 363 364 365 366 367
    cJSON* item = cJSON_GetArrayItem(root, i);
    if (!item) {
      return false;
    }

    char* jsonKey = item->string;
    if (!jsonKey) return false;
wmmhello's avatar
wmmhello 已提交
368 369
    for (size_t j = 0; j < strlen(jsonKey); ++j) {
      if (isprint(jsonKey[j]) == 0) return false;
wmmhello's avatar
wmmhello 已提交
370 371 372 373 374 375 376
    }

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

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