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

taosdata / TDengine / #5053

13 May 2026 12:00PM UTC coverage: 73.397% (+0.06%) from 73.338%
#5053

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

627 existing lines in 131 files now uncovered.

281694 of 383795 relevant lines covered (73.4%)

132505311.38 hits per line

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

76.34
/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() {
43,434,983✔
31
  SJson* pJson = cJSON_CreateArray();
43,434,983✔
32
  if (pJson == NULL) {
43,439,806✔
33
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
34
  }
35
  return pJson;
43,439,983✔
36
}
37

38
void tjsonDelete(SJson* pJson) {
749,313,429✔
39
  if (pJson != NULL) {
749,313,429✔
40
    cJSON_Delete((cJSON*)pJson);
749,314,934✔
41
  }
42
}
749,311,799✔
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) {
498,641,198✔
51
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
498,641,198✔
52
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
868✔
53
  }
54

55
  return TSDB_CODE_SUCCESS;
498,639,921✔
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;
2,865✔
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

UNCOV
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,
1,009,019,068✔
130
                      int32_t num) {
131
  if (num > 0) {
1,009,019,068✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
930,845,702✔
133
    if (NULL == pJsonArray) {
930,844,564✔
134
      return terrno;
1,461✔
135
    }
136
    for (size_t i = 0; i < num; ++i) {
1,956,841,501✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
1,025,999,251✔
138
      if (TSDB_CODE_SUCCESS != code) {
1,025,998,398✔
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
1,009,015,616✔
144
}
145

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

163
char* tjsonToString(const SJson* pJson) {
41,250,618✔
164
  char* p = cJSON_Print((cJSON*)pJson);
41,250,618✔
165
  if (!p) {
41,252,719✔
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
41,251,702✔
169
}
170

171
char* tjsonToUnformattedString(const SJson* pJson) {
687,124,587✔
172
  char* p = cJSON_PrintUnformatted((cJSON*)pJson);
687,124,587✔
173
  if (!p) {
687,125,439✔
174
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
175
  }
176
  return p;
687,125,439✔
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,148✔
182
  *pName = ((cJSON*)pJson)->string;
1,148✔
183
  if (NULL == *pName) {
1,148✔
184
    return TSDB_CODE_FAILED;
×
185
  }
186
  return TSDB_CODE_SUCCESS;
1,148✔
187
}
188

189
int32_t tjsonGetObjectValueString(const SJson* pJson, char** pValueString) {
1,148✔
190
  *pValueString = ((cJSON*)pJson)->valuestring;
1,148✔
191
  if (NULL == *pValueString) {
1,148✔
192
    return TSDB_CODE_FAILED;
×
193
  }
194
  return TSDB_CODE_SUCCESS;
1,148✔
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) {
652,572,962✔
210
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
652,572,962✔
211
  if (NULL == p) {
652,621,787✔
212
    return TSDB_CODE_SUCCESS;
×
213
  }
214
  int32_t len = strlen(p);
652,621,787✔
215
  if (len >= cap) {
652,621,787✔
216
    return TSDB_CODE_OUT_OF_MEMORY;
×
217
  }
218
  TAOS_STRCPY(pVal, p);
652,621,787✔
219
  return TSDB_CODE_SUCCESS;
652,621,787✔
220
}
221

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

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

243
int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal) {
14,366,887✔
244
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
14,366,887✔
245
  if (NULL == p) {
14,366,867✔
246
    return TSDB_CODE_SUCCESS;
2,477,067✔
247
  }
248
  *pVal = taosStrdup(p);
11,889,800✔
249
  return TSDB_CODE_SUCCESS;
11,890,187✔
250
}
251

252
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
2,043,486,526✔
253
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
2,043,486,526✔
254
  if (NULL == p) {
2,044,229,127✔
255
    return TSDB_CODE_SUCCESS;
17,369,678✔
256
  }
257
#ifdef WINDOWS
258
  sscanf(p, "%" PRId64, pVal);
259
#else
260
  *pVal = taosStr2Int64(p, NULL, 10);
2,026,859,449✔
261
#endif
262
  return TSDB_CODE_SUCCESS;
2,026,657,993✔
263
}
264

265
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
758,918,539✔
266
  int64_t val = 0;
758,918,539✔
267
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
758,923,172✔
268
  *pVal = val;
758,896,953✔
269
  return code;
758,899,551✔
270
}
271

272
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
287,966,452✔
273
  int64_t val = 0;
287,966,452✔
274
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
287,970,586✔
275
  *pVal = val;
287,963,118✔
276
  return code;
287,963,182✔
277
}
278

279
int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal) {
114,464,929✔
280
  int64_t val = 0;
114,464,929✔
281
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
114,464,554✔
282
  *pVal = val;
114,465,963✔
283
  return code;
114,466,233✔
284
}
285

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

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

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

313
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
515,330,453✔
314
  uint64_t val = 0;
515,330,453✔
315
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
515,335,885✔
316
  *pVal = val;
515,349,123✔
317
  return code;
515,350,359✔
318
}
319

320
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
678,829,695✔
321
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
678,829,695✔
322
  if (NULL == pObject) {
678,839,711✔
323
    return TSDB_CODE_SUCCESS;
×
324
  }
325
  if (!cJSON_IsBool(pObject)) {
678,839,711✔
326
    return TSDB_CODE_FAILED;
×
327
  }
328
  *pVal = cJSON_IsTrue(pObject) ? true : false;
678,830,976✔
329
  return TSDB_CODE_SUCCESS;
678,828,956✔
330
}
331

332
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
25,354,901✔
333
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
25,354,901✔
334
  if (NULL == pObject) {
25,355,194✔
335
    return TSDB_CODE_SUCCESS;
1,137,645✔
336
  }
337
  if (!cJSON_IsNumber(pObject)) {
24,217,549✔
338
    return TSDB_CODE_FAILED;
×
339
  }
340
  *pVal = cJSON_GetNumberValue(pObject);
24,217,541✔
341
  return TSDB_CODE_SUCCESS;
24,217,653✔
342
}
343

344
bool tjsonIsArray(const SJson* pJson) { return cJSON_IsArray(pJson); }
721,270✔
345

346
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
90,040,045✔
347

348
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
173,769,176✔
349

350
int32_t tjsonToObject(const SJson* pJson, const char* pName, FJsonToObject func, void* pObj) {
432,263,362✔
351
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
432,263,362✔
352
  if (NULL == pJsonObj) {
432,280,969✔
353
    return TSDB_CODE_SUCCESS;
5,573,469✔
354
  }
355
  return func(pJsonObj, pObj);
426,707,500✔
356
}
357

358
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FJsonToObject func, void** pObj, int32_t objSize) {
1,703,368✔
359
  if (objSize <= 0) {
1,703,368✔
360
    return TSDB_CODE_SUCCESS;
1,032,000✔
361
  }
362

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

374
int32_t tjsonToArray(const SJson* pJson, const char* pName, FJsonToObject func, void* pArray, int32_t itemSize) {
10,526,514✔
375
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
10,526,514✔
376
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
10,526,514✔
377
  for (int32_t i = 0; i < size; ++i) {
22,433,830✔
378
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
11,907,316✔
379
    if (TSDB_CODE_SUCCESS != code) {
11,907,316✔
380
      return code;
×
381
    }
382
  }
383
  return TSDB_CODE_SUCCESS;
10,526,514✔
384
}
385

386
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FJsonToObject func, SArray** pArray, int32_t itemSize) {
8,709,383✔
387
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
8,709,383✔
388
  int32_t      size = tjsonGetArraySize(jArray);
8,709,383✔
389
  if (size > 0) {
8,709,383✔
390
    *pArray = taosArrayInit_s(itemSize, size);
3,992,035✔
391
    if (NULL == *pArray) {
3,992,035✔
392
      return terrno;
×
393
    }
394
    for (int32_t i = 0; i < size; ++i) {
10,744,425✔
395
      int32_t code = func(tjsonGetArrayItem(jArray, i), taosArrayGet(*pArray, i));
6,752,390✔
396
      if (TSDB_CODE_SUCCESS != code) {
6,752,390✔
397
        return code;
×
398
      }
399
    }
400
  }
401
  return TSDB_CODE_SUCCESS;
8,709,383✔
402
}
403

404
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
18,180,798✔
405

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

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

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

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

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

440
const char* tjsonGetError() { return cJSON_GetErrorPtr(); }
×
441

442
void tjsonDeleteItemFromObject(const SJson* pJson, const char* pName) {
472✔
443
  cJSON_DeleteItemFromObject((cJSON*)pJson, pName);
472✔
444
}
472✔
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