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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

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%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

76.72
/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,635,205✔
31
  SJson* pJson = cJSON_CreateArray();
43,635,205✔
32
  if (pJson == NULL) {
43,640,965✔
33
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
34
  }
35
  return pJson;
43,643,863✔
36
}
37

38
void tjsonDelete(SJson* pJson) {
756,304,246✔
39
  if (pJson != NULL) {
756,304,246✔
40
    cJSON_Delete((cJSON*)pJson);
756,305,156✔
41
  }
42
}
756,305,564✔
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) {
511,012,526✔
51
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
511,012,526✔
UNCOV
52
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
53
  }
54

55
  return TSDB_CODE_SUCCESS;
511,015,101✔
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;
496✔
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;
13,248✔
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;
2,892✔
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,028,433,471✔
130
                      int32_t num) {
131
  if (num > 0) {
1,028,433,471✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
948,438,820✔
133
    if (NULL == pJsonArray) {
948,437,979✔
134
      return terrno;
1,739✔
135
    }
136
    for (size_t i = 0; i < num; ++i) {
1,992,726,341✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
1,044,290,972✔
138
      if (TSDB_CODE_SUCCESS != code) {
1,044,290,101✔
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
1,028,430,020✔
144
}
145

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

163
char* tjsonToString(const SJson* pJson) {
42,011,153✔
164
  char* p = cJSON_Print((cJSON*)pJson);
42,011,153✔
165
  if (!p) {
42,012,313✔
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
42,011,726✔
169
}
170

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

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

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

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

243
int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal) {
14,937,842✔
244
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
14,937,842✔
245
  if (NULL == p) {
14,937,842✔
246
    return TSDB_CODE_SUCCESS;
2,572,641✔
247
  }
248
  *pVal = taosStrdup(p);
12,365,201✔
249
  return TSDB_CODE_SUCCESS;
12,365,201✔
250
}
251

252
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
2,141,938,587✔
253
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
2,141,938,587✔
254
  if (NULL == p) {
2,142,781,740✔
255
    return TSDB_CODE_SUCCESS;
18,088,201✔
256
  }
257
#ifdef WINDOWS
258
  sscanf(p, "%" PRId64, pVal);
259
#else
260
  *pVal = taosStr2Int64(p, NULL, 10);
2,124,693,539✔
261
#endif
262
  return TSDB_CODE_SUCCESS;
2,124,503,595✔
263
}
264

265
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
799,337,498✔
266
  int64_t val = 0;
799,337,498✔
267
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
799,360,601✔
268
  *pVal = val;
799,356,137✔
269
  return code;
799,360,231✔
270
}
271

272
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
307,369,515✔
273
  int64_t val = 0;
307,369,515✔
274
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
307,373,619✔
275
  *pVal = val;
307,372,123✔
276
  return code;
307,374,295✔
277
}
278

279
int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal) {
120,765,860✔
280
  int64_t val = 0;
120,765,860✔
281
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
120,765,860✔
282
  *pVal = val;
120,768,825✔
283
  return code;
120,768,825✔
284
}
285

286
int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pVal) {
624,063,628✔
287
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
624,063,628✔
288
  if (NULL == p) {
624,073,031✔
289
    return TSDB_CODE_SUCCESS;
×
290
  }
291
#ifdef WINDOWS
292
  sscanf(p, "%" PRIu64, pVal);
293
#else
294
  *pVal = taosStr2UInt64(p, NULL, 10);
624,073,031✔
295
#endif
296
  return TSDB_CODE_SUCCESS;
624,066,521✔
297
}
298

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

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

313
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
545,207,955✔
314
  uint64_t val = 0;
545,207,955✔
315
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
545,215,197✔
316
  *pVal = val;
545,232,619✔
317
  return code;
545,234,002✔
318
}
319

320
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
720,057,452✔
321
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
720,057,452✔
322
  if (NULL == pObject) {
720,065,769✔
323
    return TSDB_CODE_SUCCESS;
×
324
  }
325
  if (!cJSON_IsBool(pObject)) {
720,065,769✔
326
    return TSDB_CODE_FAILED;
×
327
  }
328
  *pVal = cJSON_IsTrue(pObject) ? true : false;
720,052,938✔
329
  return TSDB_CODE_SUCCESS;
720,054,653✔
330
}
331

332
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
25,857,508✔
333
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
25,857,508✔
334
  if (NULL == pObject) {
25,859,613✔
335
    return TSDB_CODE_SUCCESS;
1,161,086✔
336
  }
337
  if (!cJSON_IsNumber(pObject)) {
24,698,527✔
338
    return TSDB_CODE_FAILED;
×
339
  }
340
  *pVal = cJSON_GetNumberValue(pObject);
24,698,880✔
341
  return TSDB_CODE_SUCCESS;
24,698,764✔
342
}
343

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

346
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
93,207,949✔
347

348
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
183,616,440✔
349

350
int32_t tjsonToObject(const SJson* pJson, const char* pName, FJsonToObject func, void* pObj) {
457,261,940✔
351
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
457,261,940✔
352
  if (NULL == pJsonObj) {
457,282,848✔
353
    return TSDB_CODE_SUCCESS;
5,884,374✔
354
  }
355
  return func(pJsonObj, pObj);
451,398,474✔
356
}
357

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

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

374
int32_t tjsonToArray(const SJson* pJson, const char* pName, FJsonToObject func, void* pArray, int32_t itemSize) {
11,161,414✔
375
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
11,161,414✔
376
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
11,162,234✔
377
  for (int32_t i = 0; i < size; ++i) {
23,686,903✔
378
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
12,524,669✔
379
    if (TSDB_CODE_SUCCESS != code) {
12,524,669✔
380
      return code;
×
381
    }
382
  }
383
  return TSDB_CODE_SUCCESS;
11,162,234✔
384
}
385

386
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FJsonToObject func, SArray** pArray, int32_t itemSize) {
9,037,003✔
387
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
9,037,003✔
388
  int32_t      size = tjsonGetArraySize(jArray);
9,037,003✔
389
  if (size > 0) {
9,037,003✔
390
    *pArray = taosArrayInit_s(itemSize, size);
4,173,177✔
391
    if (NULL == *pArray) {
4,173,177✔
392
      return terrno;
×
393
    }
394
    for (int32_t i = 0; i < size; ++i) {
11,271,725✔
395
      int32_t code = func(tjsonGetArrayItem(jArray, i), taosArrayGet(*pArray, i));
7,098,705✔
396
      if (TSDB_CODE_SUCCESS != code) {
7,098,548✔
397
        return code;
×
398
      }
399
    }
400
  }
401
  return TSDB_CODE_SUCCESS;
9,036,846✔
402
}
403

404
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
18,810,854✔
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) {
624✔
443
  cJSON_DeleteItemFromObject((cJSON*)pJson, pName);
624✔
444
}
624✔
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