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

taosdata / TDengine / #4829

30 Oct 2025 09:25AM UTC coverage: 49.734% (-11.3%) from 61.071%
#4829

push

travis-ci

web-flow
Merge pull request #33435 from taosdata/3.0

merge 3.0

123072 of 323930 branches covered (37.99%)

Branch coverage included in aggregate %.

7 of 25 new or added lines in 3 files covered. (28.0%)

35232 existing lines in 327 files now uncovered.

172062 of 269495 relevant lines covered (63.85%)

70709785.06 hits per line

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

59.22
/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() {
5,563,960✔
31
  SJson* pJson = cJSON_CreateArray();
5,563,960✔
32
  if (pJson == NULL) {
5,565,150!
33
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
34
  }
35
  return pJson;
5,564,154✔
36
}
37

38
void tjsonDelete(SJson* pJson) {
153,026,278✔
39
  if (pJson != NULL) {
153,026,278✔
40
    cJSON_Delete((cJSON*)pJson);
153,026,292✔
41
  }
42
}
153,025,434✔
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) {
88,718,285✔
51
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
88,718,285✔
52
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
342✔
53
  }
54

55
  return TSDB_CODE_SUCCESS;
88,718,982✔
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;
123,510✔
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;
45✔
69
  }
70

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

74
SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) {
873,737,605✔
75
  SJson* ret = (SJson*)cJSON_AddArrayToObject((cJSON*)pJson, pName);
873,737,605✔
76
  if (ret == NULL) {
873,737,100!
77
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
78
  }
79
  return ret;
873,736,709✔
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) {
1,967,911,455✔
91
  if (cJSON_AddItemToArray((cJSON*)pJson, pItem)) {
1,967,911,455✔
92
    return TSDB_CODE_SUCCESS;
1,967,911,570✔
93
  }
94

95
  return terrno = TSDB_CODE_OUT_OF_MEMORY;
11✔
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;
1,287,256,135✔
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) {
1,957,866,077✔
116
  SJson* pJobj = tjsonCreateObject();
1,957,866,077✔
117
  if (pJobj == NULL) {
1,957,867,304!
118
    return terrno;
×
119
  }
120

121
  int32_t rc = func(pObj, pJobj);
1,957,867,304✔
122
  if (rc != TSDB_CODE_SUCCESS) {
1,957,867,260!
123
    tjsonDelete(pJobj);
×
124
    return rc;
×
125
  }
126
  return tjsonAddItemToArray(pJson, pJobj);
1,957,867,260✔
127
}
128

129
int32_t tjsonAddArray(SJson* pJson, const char* pName, FToJson func, const void* pArray, int32_t itemSize,
201,119,789✔
130
                      int32_t num) {
131
  if (num > 0) {
201,119,789✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
185,786,828✔
133
    if (NULL == pJsonArray) {
185,786,026✔
134
      return terrno;
596✔
135
    }
136
    for (size_t i = 0; i < num; ++i) {
376,786,345✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
191,001,058✔
138
      if (TSDB_CODE_SUCCESS != code) {
191,000,915!
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
201,118,248✔
144
}
145

146
int32_t tjsonAddTArray(SJson* pJson, const char* pName, FToJson func, const SArray* pArray) {
419,218✔
147
  int32_t num = taosArrayGetSize(pArray);
419,218✔
148
  if (num > 0) {
419,218!
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;
419,218✔
161
}
162

163
char* tjsonToString(const SJson* pJson) {
6,345,649✔
164
  char* p = cJSON_Print((cJSON*)pJson);
6,345,649✔
165
  if (!p) {
6,346,240!
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
6,346,358✔
169
}
170

171
char* tjsonToUnformattedString(const SJson* pJson) {
144,991,591✔
172
  char* p = cJSON_PrintUnformatted((cJSON*)pJson);
144,991,591✔
173
  if (!p) {
144,992,404!
174
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
175
  }
176
  return p;
144,992,404✔
177
}
178

179
SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName) { return cJSON_GetObjectItem(pJson, pName); }
405,614,981✔
180

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

189
int32_t tjsonGetObjectValueString(const SJson* pJson, char** pValueString) {
124✔
190
  *pValueString = ((cJSON*)pJson)->valuestring;
124✔
191
  if (NULL == *pValueString) {
124!
192
    return TSDB_CODE_FAILED;
×
193
  }
194
  return TSDB_CODE_SUCCESS;
124✔
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) {
55,446,774✔
202
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
55,446,774✔
203
  if (NULL == p) {
55,451,457!
204
    return TSDB_CODE_SUCCESS;
×
205
  }
206
  strcpy(pVal, p);
55,451,457!
207
  return TSDB_CODE_SUCCESS;
55,451,457✔
208
}
209

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

223
int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal) {
220,176✔
224
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
220,176✔
225
  if (NULL == p) {
220,176!
226
    return TSDB_CODE_SUCCESS;
×
227
  }
228
  *pVal = taosStrdup(p);
220,176!
229
  return TSDB_CODE_SUCCESS;
220,176✔
230
}
231

232
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
182,891,295✔
233
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
182,891,295✔
234
  if (NULL == p) {
183,044,426✔
235
    return TSDB_CODE_SUCCESS;
1,198,250✔
236
  }
237
#ifdef WINDOWS
238
  sscanf(p, "%" PRId64, pVal);
239
#else
240
  *pVal = taosStr2Int64(p, NULL, 10);
181,846,176✔
241
#endif
242
  return TSDB_CODE_SUCCESS;
181,826,725✔
243
}
244

245
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
62,131,860✔
246
  int64_t val = 0;
62,131,860✔
247
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
62,131,557✔
248
  *pVal = val;
62,121,564✔
249
  return code;
62,122,362✔
250
}
251

252
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
35,972,157✔
253
  int64_t val = 0;
35,972,157✔
254
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
35,972,339✔
255
  *pVal = val;
35,961,673✔
256
  return code;
35,961,673✔
257
}
258

259
int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal) {
8,099,963✔
260
  int64_t val = 0;
8,099,963✔
261
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
8,099,963✔
262
  *pVal = val;
8,099,877✔
263
  return code;
8,099,877✔
264
}
265

266
int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pVal) {
50,112,577✔
267
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
50,112,577✔
268
  if (NULL == p) {
50,112,416!
269
    return TSDB_CODE_SUCCESS;
×
270
  }
271
#ifdef WINDOWS
272
  sscanf(p, "%" PRIu64, pVal);
273
#else
274
  *pVal = taosStr2UInt64(p, NULL, 10);
50,112,416✔
275
#endif
276
  return TSDB_CODE_SUCCESS;
50,109,767✔
277
}
278

UNCOV
279
int32_t tjsonGetUIntValue(const SJson* pJson, const char* pName, uint32_t* pVal) {
×
UNCOV
280
  uint64_t val = 0;
×
UNCOV
281
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
×
UNCOV
282
  *pVal = val;
×
UNCOV
283
  return code;
×
284
}
285

286
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
44,362,643✔
287
  uint64_t val = 0;
44,362,643✔
288
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
44,363,845✔
289
  *pVal = val;
44,363,756✔
290
  return code;
44,363,756✔
291
}
292

293
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
53,643,950✔
294
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
53,643,950✔
295
  if (NULL == pObject) {
53,650,591!
296
    return TSDB_CODE_SUCCESS;
×
297
  }
298
  if (!cJSON_IsBool(pObject)) {
53,650,591!
299
    return TSDB_CODE_FAILED;
×
300
  }
301
  *pVal = cJSON_IsTrue(pObject) ? true : false;
53,648,966✔
302
  return TSDB_CODE_SUCCESS;
53,645,014✔
303
}
304

305
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
3,350,917✔
306
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
3,350,917✔
307
  if (NULL == pObject) {
3,350,917✔
308
    return TSDB_CODE_SUCCESS;
178,373✔
309
  }
310
  if (!cJSON_IsNumber(pObject)) {
3,172,544!
311
    return TSDB_CODE_FAILED;
×
312
  }
313
  *pVal = cJSON_GetNumberValue(pObject);
3,172,544✔
314
  return TSDB_CODE_SUCCESS;
3,172,544✔
315
}
316

317
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
7,263,035✔
318

319
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
15,302,051✔
320

321
int32_t tjsonToObject(const SJson* pJson, const char* pName, FToObject func, void* pObj) {
37,999,980✔
322
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
37,999,980✔
323
  if (NULL == pJsonObj) {
38,004,469✔
324
    return TSDB_CODE_SUCCESS;
506,844✔
325
  }
326
  return func(pJsonObj, pObj);
37,497,625✔
327
}
328

329
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FToObject func, void** pObj, int32_t objSize) {
23,742✔
330
  if (objSize <= 0) {
23,742✔
331
    return TSDB_CODE_SUCCESS;
11,871✔
332
  }
333

334
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
11,871✔
335
  if (NULL == pJsonObj) {
11,871!
336
    return TSDB_CODE_SUCCESS;
×
337
  }
338
  *pObj = taosMemoryCalloc(1, objSize);
11,871!
339
  if (NULL == *pObj) {
11,871!
340
    return terrno;
×
341
  }
342
  return func(pJsonObj, *pObj);
11,871✔
343
}
344

345
int32_t tjsonToArray(const SJson* pJson, const char* pName, FToObject func, void* pArray, int32_t itemSize) {
723,939✔
346
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
723,939✔
347
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
723,939✔
348
  for (int32_t i = 0; i < size; ++i) {
1,394,408✔
349
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
670,469✔
350
    if (TSDB_CODE_SUCCESS != code) {
670,469!
351
      return code;
×
352
    }
353
  }
354
  return TSDB_CODE_SUCCESS;
723,939✔
355
}
356

357
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FToObject func, SArray** pArray, int32_t itemSize) {
11,871✔
358
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
11,871✔
359
  int32_t      size = tjsonGetArraySize(jArray);
11,871✔
360
  if (size > 0) {
11,871!
361
    *pArray = taosArrayInit_s(itemSize, size);
×
362
    if (NULL == *pArray) {
×
363
      return terrno;
×
364
    }
365
    for (int32_t i = 0; i < size; ++i) {
×
366
      int32_t code = func(tjsonGetArrayItem(jArray, i), taosArrayGet(*pArray, i));
×
367
      if (TSDB_CODE_SUCCESS != code) {
×
368
        return code;
×
369
      }
370
    }
371
  }
372
  return TSDB_CODE_SUCCESS;
11,871✔
373
}
374

375
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
1,878,514✔
376

377
bool tjsonValidateJson(const char* jIn) {
×
378
  if (!jIn) {
×
379
    return false;
×
380
  }
381

382
  // set json real data
383
  cJSON* root = cJSON_Parse(jIn);
×
384
  if (root == NULL) {
×
385
    return false;
×
386
  }
387

388
  if (!cJSON_IsObject(root)) {
×
389
    return false;
×
390
  }
391
  int size = cJSON_GetArraySize(root);
×
392
  for (int i = 0; i < size; i++) {
×
393
    cJSON* item = cJSON_GetArrayItem(root, i);
×
394
    if (!item) {
×
395
      return false;
×
396
    }
397

398
    char* jsonKey = item->string;
×
399
    if (!jsonKey) return false;
×
400
    for (size_t j = 0; j < strlen(jsonKey); ++j) {
×
401
      if (isprint(jsonKey[j]) == 0) return false;
×
402
    }
403

404
    if (item->type == cJSON_Object || item->type == cJSON_Array) {
×
405
      return false;
×
406
    }
407
  }
408
  return true;
×
409
}
410

411
const char* tjsonGetError() { return cJSON_GetErrorPtr(); }
×
412

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