• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#define _DEFAULT_SOURCE
17

18
#include "tjson.h"
19
#include "cJSON.h"
20
#include "taoserror.h"
21

22
SJson* tjsonCreateObject() {
2,147,483,647✔
23
  SJson* pJson = cJSON_CreateObject();
2,147,483,647✔
24
  if (pJson == NULL) {
2,147,483,647✔
25
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
26
  }
27
  return pJson;
2,147,483,647✔
28
}
29

30
SJson* tjsonCreateArray() {
38,936,823✔
31
  SJson* pJson = cJSON_CreateArray();
38,936,823✔
32
  if (pJson == NULL) {
38,944,077✔
33
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
34
  }
35
  return pJson;
38,940,323✔
36
}
37

38
void tjsonDelete(SJson* pJson) {
682,296,605✔
39
  if (pJson != NULL) {
682,296,605✔
40
    cJSON_Delete((cJSON*)pJson);
682,297,475✔
41
  }
42
}
682,298,580✔
43

44
int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number) {
2,147,483,647✔
45
  char tmp[40] = {0};
2,147,483,647✔
46
  snprintf(tmp, sizeof(tmp), "%" PRId64, number);
2,147,483,647✔
47
  return tjsonAddStringToObject(pJson, pName, tmp);
2,147,483,647✔
48
}
49

50
int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number) {
465,515,126✔
51
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
465,515,126✔
52
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
2,191✔
53
  }
54

55
  return TSDB_CODE_SUCCESS;
465,514,306✔
56
}
57

58
int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean) {
2,147,483,647✔
59
  if (NULL == cJSON_AddBoolToObject((cJSON*)pJson, pName, boolean)) {
2,147,483,647✔
UNCOV
60
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
61
  }
62

63
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
64
}
65

66
int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal) {
2,147,483,647✔
67
  if (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal)) {
2,147,483,647✔
68
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
4,116,585✔
69
  }
70

71
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
72
}
73

74
SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) {
2,147,483,647✔
75
  SJson* ret = (SJson*)cJSON_AddArrayToObject((cJSON*)pJson, pName);
2,147,483,647✔
76
  if (ret == NULL) {
2,147,483,647✔
77
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
78
  }
79
  return ret;
2,147,483,647✔
80
}
81

82
int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem) {
2,147,483,647✔
83
  if (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem)) {
2,147,483,647✔
84
    return TSDB_CODE_SUCCESS;
2,147,483,647✔
85
  }
86

87
  return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
88
}
89

90
int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem) {
2,147,483,647✔
91
  if (cJSON_AddItemToArray((cJSON*)pJson, pItem)) {
2,147,483,647✔
92
    return TSDB_CODE_SUCCESS;
2,147,483,647✔
93
  }
94

95
  return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
96
}
97

98
int32_t tjsonAddObject(SJson* pJson, const char* pName, FToJson func, const void* pObj) {
2,147,483,647✔
99
  if (NULL == pObj) {
2,147,483,647✔
100
    return TSDB_CODE_SUCCESS;
2,147,483,647✔
101
  }
102

103
  SJson* pJobj = tjsonCreateObject();
2,147,483,647✔
104
  if (NULL == pJobj) {
2,147,483,647✔
105
    return terrno;
×
106
  }
107
  int32_t rc = func(pObj, pJobj);
2,147,483,647✔
108
  if (rc != TSDB_CODE_SUCCESS) {
2,147,483,647✔
109
    tjsonDelete(pJobj);
×
110
    return rc;
×
111
  }
112
  return tjsonAddItemToObject(pJson, pName, pJobj);
2,147,483,647✔
113
}
114

115
int32_t tjsonAddItem(SJson* pJson, FToJson func, const void* pObj) {
2,147,483,647✔
116
  SJson* pJobj = tjsonCreateObject();
2,147,483,647✔
117
  if (pJobj == NULL) {
2,147,483,647✔
118
    return terrno;
×
119
  }
120

121
  int32_t rc = func(pObj, pJobj);
2,147,483,647✔
122
  if (rc != TSDB_CODE_SUCCESS) {
2,147,483,647✔
123
    tjsonDelete(pJobj);
×
124
    return rc;
×
125
  }
126
  return tjsonAddItemToArray(pJson, pJobj);
2,147,483,647✔
127
}
128

129
int32_t tjsonAddArray(SJson* pJson, const char* pName, FToJson func, const void* pArray, int32_t itemSize,
940,235,821✔
130
                      int32_t num) {
131
  if (num > 0) {
940,235,821✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
868,404,366✔
133
    if (NULL == pJsonArray) {
868,407,932✔
134
      return terrno;
822✔
135
    }
136
    for (size_t i = 0; i < num; ++i) {
1,822,064,455✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
953,659,427✔
138
      if (TSDB_CODE_SUCCESS != code) {
953,657,345✔
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
940,236,483✔
144
}
145

146
int32_t tjsonAddTArray(SJson* pJson, const char* pName, FToJson func, const SArray* pArray) {
4,804,634✔
147
  int32_t num = taosArrayGetSize(pArray);
4,804,634✔
148
  if (num > 0) {
4,804,634✔
149
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
828,805✔
150
    if (NULL == pJsonArray) {
828,805✔
151
      return terrno;
×
152
    }
153
    for (int32_t i = 0; i < num; ++i) {
2,230,667✔
154
      int32_t code = tjsonAddItem(pJsonArray, func, taosArrayGet(pArray, i));
1,401,862✔
155
      if (TSDB_CODE_SUCCESS != code) {
1,401,862✔
156
        return code;
×
157
      }
158
    }
159
  }
160
  return TSDB_CODE_SUCCESS;
4,804,634✔
161
}
162

163
char* tjsonToString(const SJson* pJson) {
37,809,326✔
164
  char* p = cJSON_Print((cJSON*)pJson);
37,809,326✔
165
  if (!p) {
37,811,688✔
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
37,810,780✔
169
}
170

171
char* tjsonToUnformattedString(const SJson* pJson) {
625,845,063✔
172
  char* p = cJSON_PrintUnformatted((cJSON*)pJson);
625,845,063✔
173
  if (!p) {
625,847,437✔
174
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
175
  }
176
  return p;
625,847,437✔
177
}
178

179
SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName) { return cJSON_GetObjectItem(pJson, pName); }
2,147,483,647✔
180

181
int32_t tjsonGetObjectName(const SJson* pJson, char** pName) {
1,132✔
182
  *pName = ((cJSON*)pJson)->string;
1,132✔
183
  if (NULL == *pName) {
1,132✔
184
    return TSDB_CODE_FAILED;
×
185
  }
186
  return TSDB_CODE_SUCCESS;
1,132✔
187
}
188

189
int32_t tjsonGetObjectValueString(const SJson* pJson, char** pValueString) {
1,132✔
190
  *pValueString = ((cJSON*)pJson)->valuestring;
1,132✔
191
  if (NULL == *pValueString) {
1,132✔
192
    return TSDB_CODE_FAILED;
×
193
  }
194
  return TSDB_CODE_SUCCESS;
1,132✔
195
}
196

197
void tjsonGetObjectValueBigInt(const SJson* pJson, int64_t* pVal) { *pVal = (int64_t)((cJSON*)pJson)->valuedouble; }
×
198

199
void tjsonGetObjectValueDouble(const SJson* pJson, double* pVal) { *pVal = ((cJSON*)pJson)->valuedouble; }
×
200

201
int32_t tjsonGetStringValue(const SJson* pJson, const char* pName, char* pVal) {
×
202
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
×
203
  if (NULL == p) {
×
204
    return TSDB_CODE_SUCCESS;
×
205
  }
206
  TAOS_STRCPY(pVal, p);
×
207
  return TSDB_CODE_SUCCESS;
×
208
}
209
int32_t tjsonGetStringValue1(const SJson* pJson, const char* pName, char* pVal, int32_t cap) {
596,134,746✔
210
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
596,134,746✔
211
  if (NULL == p) {
596,156,465✔
212
    return TSDB_CODE_SUCCESS;
×
213
  }
214
  int32_t len = strlen(p);
596,156,465✔
215
  if (len >= cap) {
596,156,465✔
216
    return TSDB_CODE_OUT_OF_MEMORY;
×
217
  }
218
  TAOS_STRCPY(pVal, p);
596,156,465✔
219
  return TSDB_CODE_SUCCESS;
596,156,465✔
220
}
221

222
int32_t tjsonGetStringValue2(const SJson* pJson, const char* pName, char* pVal, int32_t maxLen) {
5,002✔
223
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
5,002✔
224
  if (NULL == p) {
5,002✔
225
    return TSDB_CODE_SUCCESS;
×
226
  }
227
  int32_t len = strlen(p);
5,002✔
228
  if (len >= maxLen - 1) {
5,002✔
229
    return TSDB_CODE_OUT_OF_MEMORY;
×
230
  }
231
  if (pVal == NULL) {
5,002✔
232
    return TSDB_CODE_INVALID_PARA;
×
233
  } else {
234
    strcpy(pVal, p);
5,002✔
235
  }
236
  return TSDB_CODE_SUCCESS;
5,002✔
237
}
238

239
const char* tjsonGetStringPointer(const SJson* pJson, const char* pName) {
737✔
240
  return cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
737✔
241
}
242

243
int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal) {
12,332,808✔
244
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
12,332,808✔
245
  if (NULL == p) {
12,333,130✔
246
    return TSDB_CODE_SUCCESS;
2,067,555✔
247
  }
248
  *pVal = taosStrdup(p);
10,265,575✔
249
  return TSDB_CODE_SUCCESS;
10,265,867✔
250
}
251

252
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
1,842,575,849✔
253
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
1,842,575,849✔
254
  if (NULL == p) {
1,843,436,529✔
255
    return TSDB_CODE_SUCCESS;
14,954,229✔
256
  }
257
#ifdef WINDOWS
258
  sscanf(p, "%" PRId64, pVal);
259
#else
260
  *pVal = taosStr2Int64(p, NULL, 10);
1,828,482,300✔
261
#endif
262
  return TSDB_CODE_SUCCESS;
1,828,425,906✔
263
}
264

265
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
677,241,707✔
266
  int64_t val = 0;
677,241,707✔
267
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
677,258,656✔
268
  *pVal = val;
677,263,754✔
269
  return code;
677,269,330✔
270
}
271

272
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
264,069,280✔
273
  int64_t val = 0;
264,069,280✔
274
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
264,072,969✔
275
  *pVal = val;
264,076,375✔
276
  return code;
264,079,086✔
277
}
278

279
int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal) {
102,916,061✔
280
  int64_t val = 0;
102,916,061✔
281
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
102,917,521✔
282
  *pVal = val;
102,925,029✔
283
  return code;
102,924,654✔
284
}
285

286
int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pVal) {
531,190,274✔
287
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
531,190,274✔
288
  if (NULL == p) {
531,246,981✔
289
    return TSDB_CODE_SUCCESS;
×
290
  }
291
#ifdef WINDOWS
292
  sscanf(p, "%" PRIu64, pVal);
293
#else
294
  *pVal = taosStr2UInt64(p, NULL, 10);
531,246,981✔
295
#endif
296
  return TSDB_CODE_SUCCESS;
531,240,849✔
297
}
298

299
int32_t tjsonGetUIntValue(const SJson* pJson, const char* pName, uint32_t* pVal) {
2,571,706✔
300
  uint64_t val = 0;
2,571,706✔
301
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
2,571,706✔
302
  *pVal = val;
2,571,706✔
303
  return code;
2,571,706✔
304
}
305

306
int32_t tjsonGetUSmallIntValue(const SJson* pJson, const char* pName, uint16_t* pVal) {
1,108,630✔
307
  uint64_t val = 0;
1,108,630✔
308
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
1,108,630✔
309
  *pVal = val;
1,108,630✔
310
  return code;
1,108,630✔
311
}
312

313
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
463,972,225✔
314
  uint64_t val = 0;
463,972,225✔
315
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
463,978,419✔
316
  *pVal = val;
463,988,902✔
317
  return code;
463,992,542✔
318
}
319

320
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
614,777,498✔
321
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
614,777,498✔
322
  if (NULL == pObject) {
614,778,806✔
323
    return TSDB_CODE_SUCCESS;
×
324
  }
325
  if (!cJSON_IsBool(pObject)) {
614,778,806✔
326
    return TSDB_CODE_FAILED;
×
327
  }
328
  *pVal = cJSON_IsTrue(pObject) ? true : false;
614,776,202✔
329
  return TSDB_CODE_SUCCESS;
614,778,363✔
330
}
331

332
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
23,303,088✔
333
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
23,303,088✔
334
  if (NULL == pObject) {
23,304,178✔
335
    return TSDB_CODE_SUCCESS;
1,012,004✔
336
  }
337
  if (!cJSON_IsNumber(pObject)) {
22,292,174✔
338
    return TSDB_CODE_FAILED;
×
339
  }
340
  *pVal = cJSON_GetNumberValue(pObject);
22,291,618✔
341
  return TSDB_CODE_SUCCESS;
22,291,362✔
342
}
343

344
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
79,664,779✔
345

346
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
157,266,576✔
347

348
int32_t tjsonToObject(const SJson* pJson, const char* pName, FJsonToObject func, void* pObj) {
391,176,149✔
349
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
391,176,149✔
350
  if (NULL == pJsonObj) {
391,185,269✔
351
    return TSDB_CODE_SUCCESS;
5,005,537✔
352
  }
353
  return func(pJsonObj, pObj);
386,179,732✔
354
}
355

356
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FJsonToObject func, void** pObj, int32_t objSize) {
1,533,778✔
357
  if (objSize <= 0) {
1,533,778✔
358
    return TSDB_CODE_SUCCESS;
925,453✔
359
  }
360

361
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
608,325✔
362
  if (NULL == pJsonObj) {
608,325✔
363
    return TSDB_CODE_SUCCESS;
×
364
  }
365
  *pObj = taosMemoryCalloc(1, objSize);
608,325✔
366
  if (NULL == *pObj) {
608,325✔
367
    return terrno;
×
368
  }
369
  return func(pJsonObj, *pObj);
608,325✔
370
}
371

372
int32_t tjsonToArray(const SJson* pJson, const char* pName, FJsonToObject func, void* pArray, int32_t itemSize) {
9,513,459✔
373
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
9,513,459✔
374
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
9,513,751✔
375
  for (int32_t i = 0; i < size; ++i) {
19,704,180✔
376
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
10,190,650✔
377
    if (TSDB_CODE_SUCCESS != code) {
10,190,429✔
378
      return code;
×
379
    }
380
  }
381
  return TSDB_CODE_SUCCESS;
9,513,530✔
382
}
383

384
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FJsonToObject func, SArray** pArray, int32_t itemSize) {
7,305,302✔
385
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
7,305,302✔
386
  int32_t      size = tjsonGetArraySize(jArray);
7,305,302✔
387
  if (size > 0) {
7,305,302✔
388
    *pArray = taosArrayInit_s(itemSize, size);
3,293,364✔
389
    if (NULL == *pArray) {
3,293,364✔
390
      return terrno;
×
391
    }
392
    for (int32_t i = 0; i < size; ++i) {
9,014,638✔
393
      int32_t code = func(tjsonGetArrayItem(jArray, i), taosArrayGet(*pArray, i));
5,721,274✔
394
      if (TSDB_CODE_SUCCESS != code) {
5,721,274✔
395
        return code;
×
396
      }
397
    }
398
  }
399
  return TSDB_CODE_SUCCESS;
7,305,302✔
400
}
401

402
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
16,440,867✔
403

404
bool tjsonValidateJson(const char* jIn) {
×
405
  if (!jIn) {
×
406
    return false;
×
407
  }
408

409
  // set json real data
410
  cJSON* root = cJSON_Parse(jIn);
×
411
  if (root == NULL) {
×
412
    return false;
×
413
  }
414

415
  if (!cJSON_IsObject(root)) {
×
416
    return false;
×
417
  }
418
  int size = cJSON_GetArraySize(root);
×
419
  for (int i = 0; i < size; i++) {
×
420
    cJSON* item = cJSON_GetArrayItem(root, i);
×
421
    if (!item) {
×
422
      return false;
×
423
    }
424

425
    char* jsonKey = item->string;
×
426
    if (!jsonKey) return false;
×
427
    for (size_t j = 0; j < strlen(jsonKey); ++j) {
×
428
      if (isprint(jsonKey[j]) == 0) return false;
×
429
    }
430

431
    if (item->type == cJSON_Object || item->type == cJSON_Array) {
×
432
      return false;
×
433
    }
434
  }
435
  return true;
×
436
}
437

438
const char* tjsonGetError() { return cJSON_GetErrorPtr(); }
×
439

440
void tjsonDeleteItemFromObject(const SJson* pJson, const char* pName) {
×
441
  cJSON_DeleteItemFromObject((cJSON*)pJson, pName);
×
442
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc