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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

75.4
/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() {
35,055,336✔
31
  SJson* pJson = cJSON_CreateArray();
35,055,336✔
32
  if (pJson == NULL) {
35,057,972✔
33
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
34
  }
35
  return pJson;
35,060,548✔
36
}
37

38
void tjsonDelete(SJson* pJson) {
737,844,951✔
39
  if (pJson != NULL) {
737,844,951✔
40
    cJSON_Delete((cJSON*)pJson);
737,846,697✔
41
  }
42
}
737,842,879✔
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) {
421,058,501✔
51
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
421,058,501✔
52
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
229✔
53
  }
54

55
  return TSDB_CODE_SUCCESS;
421,066,086✔
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✔
60
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
22,265✔
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;
×
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;
47✔
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,
944,706,972✔
130
                      int32_t num) {
131
  if (num > 0) {
944,706,972✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
878,805,133✔
133
    if (NULL == pJsonArray) {
878,805,023✔
134
      return terrno;
1,816✔
135
    }
136
    for (size_t i = 0; i < num; ++i) {
1,840,945,367✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
962,143,726✔
138
      if (TSDB_CODE_SUCCESS != code) {
962,142,160✔
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
944,703,480✔
144
}
145

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

163
char* tjsonToString(const SJson* pJson) {
34,370,702✔
164
  char* p = cJSON_Print((cJSON*)pJson);
34,370,702✔
165
  if (!p) {
34,373,339✔
UNCOV
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
34,373,339✔
169
}
170

171
char* tjsonToUnformattedString(const SJson* pJson) {
688,759,999✔
172
  char* p = cJSON_PrintUnformatted((cJSON*)pJson);
688,759,999✔
173
  if (!p) {
688,761,920✔
174
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
175
  }
176
  return p;
688,760,145✔
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) {
60✔
182
  *pName = ((cJSON*)pJson)->string;
60✔
183
  if (NULL == *pName) {
60✔
184
    return TSDB_CODE_FAILED;
×
185
  }
186
  return TSDB_CODE_SUCCESS;
60✔
187
}
188

189
int32_t tjsonGetObjectValueString(const SJson* pJson, char** pValueString) {
60✔
190
  *pValueString = ((cJSON*)pJson)->valuestring;
60✔
191
  if (NULL == *pValueString) {
60✔
192
    return TSDB_CODE_FAILED;
×
193
  }
194
  return TSDB_CODE_SUCCESS;
60✔
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) {
397,956,708✔
202
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
397,956,708✔
203
  if (NULL == p) {
398,027,916✔
204
    return TSDB_CODE_SUCCESS;
×
205
  }
206
  strcpy(pVal, p);
398,027,916✔
207
  return TSDB_CODE_SUCCESS;
398,027,916✔
208
}
209

210
int32_t tjsonGetStringValue2(const SJson* pJson, const char* pName, char* pVal, int32_t maxLen) {
4,298✔
211
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
4,298✔
212
  if (NULL == p) {
4,298✔
213
    return TSDB_CODE_SUCCESS;
×
214
  }
215
  int32_t len = strlen(p);
4,298✔
216
  if (len >= maxLen - 1) {
4,298✔
217
    return TSDB_CODE_OUT_OF_MEMORY;
×
218
  }
219
  if (pVal == NULL) {
4,298✔
NEW
220
    return TSDB_CODE_INVALID_PARA;
×
221
  } else {
222
    strcpy(pVal, p);
4,298✔
223
  }
224
  return TSDB_CODE_SUCCESS;
4,298✔
225
}
226

227
const char* tjsonGetStringPointer(const SJson* pJson, const char* pName) {
638✔
228
  return cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
638✔
229
}
230

231
int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal) {
9,125,768✔
232
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
9,125,768✔
233
  if (NULL == p) {
9,125,114✔
234
    return TSDB_CODE_SUCCESS;
1,630,600✔
235
  }
236
  *pVal = taosStrdup(p);
7,494,514✔
237
  return TSDB_CODE_SUCCESS;
7,495,168✔
238
}
239

240
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
1,302,074,335✔
241
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
1,302,074,335✔
242
  if (NULL == p) {
1,302,651,982✔
243
    return TSDB_CODE_SUCCESS;
10,881,842✔
244
  }
245
#ifdef WINDOWS
246
  sscanf(p, "%" PRId64, pVal);
247
#else
248
  *pVal = taosStr2Int64(p, NULL, 10);
1,291,770,140✔
249
#endif
250
  return TSDB_CODE_SUCCESS;
1,291,637,240✔
251
}
252

253
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
454,497,065✔
254
  int64_t val = 0;
454,497,065✔
255
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
454,590,524✔
256
  *pVal = val;
454,552,147✔
257
  return code;
454,562,498✔
258
}
259

260
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
176,787,785✔
261
  int64_t val = 0;
176,787,785✔
262
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
176,793,907✔
263
  *pVal = val;
176,791,027✔
264
  return code;
176,796,500✔
265
}
266

267
int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal) {
66,173,590✔
268
  int64_t val = 0;
66,173,590✔
269
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
66,174,010✔
270
  *pVal = val;
66,177,108✔
271
  return code;
66,177,436✔
272
}
273

274
int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pVal) {
356,661,020✔
275
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
356,661,020✔
276
  if (NULL == p) {
356,650,806✔
277
    return TSDB_CODE_SUCCESS;
×
278
  }
279
#ifdef WINDOWS
280
  sscanf(p, "%" PRIu64, pVal);
281
#else
282
  *pVal = taosStr2UInt64(p, NULL, 10);
356,650,806✔
283
#endif
284
  return TSDB_CODE_SUCCESS;
356,658,295✔
285
}
286

287
int32_t tjsonGetUIntValue(const SJson* pJson, const char* pName, uint32_t* pVal) {
1,723,608✔
288
  uint64_t val = 0;
1,723,608✔
289
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
1,723,608✔
290
  *pVal = val;
1,723,608✔
291
  return code;
1,723,608✔
292
}
293

294
int32_t tjsonGetUSmallIntValue(const SJson* pJson, const char* pName, uint16_t* pVal) {
627,228✔
295
  uint64_t val = 0;
627,228✔
296
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
627,228✔
297
  *pVal = val;
627,228✔
298
  return code;
627,228✔
299
}
300

301
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
315,981,536✔
302
  uint64_t val = 0;
315,981,536✔
303
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
315,971,696✔
304
  *pVal = val;
316,014,043✔
305
  return code;
316,017,206✔
306
}
307

308
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
413,035,309✔
309
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
413,035,309✔
310
  if (NULL == pObject) {
413,035,879✔
311
    return TSDB_CODE_SUCCESS;
×
312
  }
313
  if (!cJSON_IsBool(pObject)) {
413,035,879✔
314
    return TSDB_CODE_FAILED;
×
315
  }
316
  *pVal = cJSON_IsTrue(pObject) ? true : false;
413,029,745✔
317
  return TSDB_CODE_SUCCESS;
413,045,750✔
318
}
319

320
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
20,724,240✔
321
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
20,724,240✔
322
  if (NULL == pObject) {
20,723,831✔
323
    return TSDB_CODE_SUCCESS;
926,170✔
324
  }
325
  if (!cJSON_IsNumber(pObject)) {
19,797,661✔
326
    return TSDB_CODE_FAILED;
×
327
  }
328
  *pVal = cJSON_GetNumberValue(pObject);
19,797,661✔
329
  return TSDB_CODE_SUCCESS;
19,797,772✔
330
}
331

332
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
55,127,979✔
333

334
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
106,979,236✔
335

336
int32_t tjsonToObject(const SJson* pJson, const char* pName, FJsonToObject func, void* pObj) {
263,177,313✔
337
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
263,177,313✔
338
  if (NULL == pJsonObj) {
263,187,490✔
339
    return TSDB_CODE_SUCCESS;
3,236,791✔
340
  }
341
  return func(pJsonObj, pObj);
259,950,699✔
342
}
343

344
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FJsonToObject func, void** pObj, int32_t objSize) {
225,512✔
345
  if (objSize <= 0) {
225,512✔
346
    return TSDB_CODE_SUCCESS;
97,497✔
347
  }
348

349
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
128,015✔
350
  if (NULL == pJsonObj) {
128,015✔
351
    return TSDB_CODE_SUCCESS;
×
352
  }
353
  *pObj = taosMemoryCalloc(1, objSize);
128,015✔
354
  if (NULL == *pObj) {
128,015✔
355
    return terrno;
×
356
  }
357
  return func(pJsonObj, *pObj);
128,015✔
358
}
359

360
int32_t tjsonToArray(const SJson* pJson, const char* pName, FJsonToObject func, void* pArray, int32_t itemSize) {
4,758,726✔
361
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
4,758,726✔
362
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
4,758,726✔
363
  for (int32_t i = 0; i < size; ++i) {
9,304,007✔
364
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
4,545,281✔
365
    if (TSDB_CODE_SUCCESS != code) {
4,545,281✔
366
      return code;
×
367
    }
368
  }
369
  return TSDB_CODE_SUCCESS;
4,758,726✔
370
}
371

372
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FJsonToObject func, SArray** pArray, int32_t itemSize) {
3,894,908✔
373
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
3,894,908✔
374
  int32_t      size = tjsonGetArraySize(jArray);
3,894,908✔
375
  if (size > 0) {
3,894,908✔
376
    *pArray = taosArrayInit_s(itemSize, size);
2,135,670✔
377
    if (NULL == *pArray) {
2,135,670✔
378
      return terrno;
×
379
    }
380
    for (int32_t i = 0; i < size; ++i) {
6,026,063✔
381
      int32_t code = func(tjsonGetArrayItem(jArray, i), taosArrayGet(*pArray, i));
3,890,393✔
382
      if (TSDB_CODE_SUCCESS != code) {
3,890,393✔
383
        return code;
×
384
      }
385
    }
386
  }
387
  return TSDB_CODE_SUCCESS;
3,894,908✔
388
}
389

390
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
12,842,186✔
391

392
bool tjsonValidateJson(const char* jIn) {
×
393
  if (!jIn) {
×
394
    return false;
×
395
  }
396

397
  // set json real data
398
  cJSON* root = cJSON_Parse(jIn);
×
399
  if (root == NULL) {
×
400
    return false;
×
401
  }
402

403
  if (!cJSON_IsObject(root)) {
×
404
    return false;
×
405
  }
406
  int size = cJSON_GetArraySize(root);
×
407
  for (int i = 0; i < size; i++) {
×
408
    cJSON* item = cJSON_GetArrayItem(root, i);
×
409
    if (!item) {
×
410
      return false;
×
411
    }
412

413
    char* jsonKey = item->string;
×
414
    if (!jsonKey) return false;
×
415
    for (size_t j = 0; j < strlen(jsonKey); ++j) {
×
416
      if (isprint(jsonKey[j]) == 0) return false;
×
417
    }
418

419
    if (item->type == cJSON_Object || item->type == cJSON_Array) {
×
420
      return false;
×
421
    }
422
  }
423
  return true;
×
424
}
425

426
const char* tjsonGetError() { return cJSON_GetErrorPtr(); }
×
427

428
void tjsonDeleteItemFromObject(const SJson* pJson, const char* pName) {
×
429
  cJSON_DeleteItemFromObject((cJSON*)pJson, pName);
×
430
}
×
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