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

taosdata / TDengine / #4696

29 Aug 2025 06:36AM UTC coverage: 58.25% (+0.2%) from 58.041%
#4696

push

travis-ci

web-flow
fix(gpt): fix race-condition in preparing tmp files (#32800)

133424 of 291873 branches covered (45.71%)

Branch coverage included in aggregate %.

5 of 34 new or added lines in 6 files covered. (14.71%)

444 existing lines in 69 files now uncovered.

201767 of 283561 relevant lines covered (71.15%)

17907122.76 hits per line

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

64.66
/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() {
115,762,356✔
23
  SJson* pJson = cJSON_CreateObject();
115,762,356✔
24
  if (pJson == NULL) {
116,326,576!
25
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
26
  }
27
  return pJson;
116,393,748✔
28
}
29

30
SJson* tjsonCreateArray() {
147,978✔
31
  SJson* pJson = cJSON_CreateArray();
147,978✔
32
  if (pJson == NULL) {
147,978!
33
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
34
  }
35
  return pJson;
147,980✔
36
}
37

38
void tjsonDelete(SJson* pJson) {
8,386,264✔
39
  if (pJson != NULL) {
8,386,264!
40
    cJSON_Delete((cJSON*)pJson);
8,404,049✔
41
  }
42
}
8,408,365✔
43

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

50
int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number) {
1,679,971✔
51
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
1,679,971!
52
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
53
  }
54

55
  return TSDB_CODE_SUCCESS;
1,679,994✔
56
}
57

58
int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean) {
68,051,208✔
59
  if (NULL == cJSON_AddBoolToObject((cJSON*)pJson, pName, boolean)) {
68,051,208!
60
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
61
  }
62

63
  return TSDB_CODE_SUCCESS;
68,233,779✔
64
}
65

66
int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal) {
310,481,660✔
67
  if (NULL == cJSON_AddStringToObject((cJSON*)pJson, pName, pVal)) {
310,481,660!
68
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
69
  }
70

71
  return TSDB_CODE_SUCCESS;
312,830,686✔
72
}
73

74
SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) {
18,138,112✔
75
  SJson* ret = (SJson*)cJSON_AddArrayToObject((cJSON*)pJson, pName);
18,138,112✔
76
  if (ret == NULL) {
18,206,889!
77
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
78
  }
79
  return ret;
18,211,755✔
80
}
81

82
int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem) {
86,988,055✔
83
  if (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem)) {
86,988,055!
84
    return TSDB_CODE_SUCCESS;
87,491,633✔
85
  }
86

87
  return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
88
}
89

90
int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem) {
21,375,195✔
91
  if (cJSON_AddItemToArray((cJSON*)pJson, pItem)) {
21,375,195!
92
    return TSDB_CODE_SUCCESS;
21,388,328✔
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) {
120,827,856✔
99
  if (NULL == pObj) {
120,827,856✔
100
    return TSDB_CODE_SUCCESS;
34,545,701✔
101
  }
102

103
  SJson* pJobj = tjsonCreateObject();
86,282,155✔
104
  if (NULL == pJobj) {
86,881,302!
105
    return terrno;
×
106
  }
107
  int32_t rc = func(pObj, pJobj);
86,881,302✔
108
  if (rc != TSDB_CODE_SUCCESS) {
86,958,494!
109
    tjsonDelete(pJobj);
×
110
    return rc;
×
111
  }
112
  return tjsonAddItemToObject(pJson, pName, pJobj);
86,958,494✔
113
}
114

115
int32_t tjsonAddItem(SJson* pJson, FToJson func, const void* pObj) {
20,924,000✔
116
  SJson* pJobj = tjsonCreateObject();
20,924,000✔
117
  if (pJobj == NULL) {
20,976,888!
118
    return terrno;
×
119
  }
120

121
  int32_t rc = func(pObj, pJobj);
20,976,888✔
122
  if (rc != TSDB_CODE_SUCCESS) {
20,979,172!
123
    tjsonDelete(pJobj);
×
124
    return rc;
×
125
  }
126
  return tjsonAddItemToArray(pJson, pJobj);
20,979,172✔
127
}
128

129
int32_t tjsonAddArray(SJson* pJson, const char* pName, FToJson func, const void* pArray, int32_t itemSize,
8,230,429✔
130
                      int32_t num) {
131
  if (num > 0) {
8,230,429✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
8,219,048✔
133
    if (NULL == pJsonArray) {
8,239,713!
UNCOV
134
      return terrno;
×
135
    }
136
    for (size_t i = 0; i < num; ++i) {
16,742,830✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
8,518,608✔
138
      if (TSDB_CODE_SUCCESS != code) {
8,502,925!
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
8,235,603✔
144
}
145

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

163
char* tjsonToString(const SJson* pJson) {
172,811✔
164
  char* p = cJSON_Print((cJSON*)pJson);
172,811✔
165
  if (!p) {
172,825!
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
172,829✔
169
}
170

171
char* tjsonToUnformattedString(const SJson* pJson) {
8,187,506✔
172
  char* p = cJSON_PrintUnformatted((cJSON*)pJson);
8,187,506✔
173
  if (!p) {
8,190,294!
174
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
175
  }
176
  return p;
8,202,917✔
177
}
178

179
SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName) { return cJSON_GetObjectItem(pJson, pName); }
18,570,695✔
180

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

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

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

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

232
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
7,853,153✔
233
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
7,853,153✔
234
  if (NULL == p) {
7,857,487✔
235
    return TSDB_CODE_SUCCESS;
82,300✔
236
  }
237
#ifdef WINDOWS
238
  sscanf(p, "%" PRId64, pVal);
239
#else
240
  *pVal = taosStr2Int64(p, NULL, 10);
7,775,187✔
241
#endif
242
  return TSDB_CODE_SUCCESS;
7,778,723✔
243
}
244

245
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
3,155,682✔
246
  int64_t val = 0;
3,155,682✔
247
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
3,155,682✔
248
  *pVal = val;
3,156,781✔
249
  return code;
3,156,781✔
250
}
251

252
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
1,508,164✔
253
  int64_t val = 0;
1,508,164✔
254
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
1,508,164✔
255
  *pVal = val;
1,509,896✔
256
  return code;
1,509,896✔
257
}
258

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

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

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

286
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
2,094,507✔
287
  uint64_t val = 0;
2,094,507✔
288
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
2,094,507✔
289
  *pVal = val;
2,095,350✔
290
  return code;
2,095,350✔
291
}
292

293
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
2,377,465✔
294
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
2,377,465✔
295
  if (NULL == pObject) {
2,378,149!
296
    return TSDB_CODE_SUCCESS;
×
297
  }
298
  if (!cJSON_IsBool(pObject)) {
2,378,149!
299
    return TSDB_CODE_FAILED;
×
300
  }
301
  *pVal = cJSON_IsTrue(pObject) ? true : false;
2,378,165✔
302
  return TSDB_CODE_SUCCESS;
2,378,181✔
303
}
304

305
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
71,596✔
306
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
71,596✔
307
  if (NULL == pObject) {
71,603✔
308
    return TSDB_CODE_SUCCESS;
1,894✔
309
  }
310
  if (!cJSON_IsNumber(pObject)) {
69,709!
311
    return TSDB_CODE_FAILED;
×
312
  }
313
  *pVal = cJSON_GetNumberValue(pObject);
69,707✔
314
  return TSDB_CODE_SUCCESS;
69,705✔
315
}
316

317
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
283,222✔
318

319
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
596,974✔
320

321
int32_t tjsonToObject(const SJson* pJson, const char* pName, FToObject func, void* pObj) {
1,751,678✔
322
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
1,751,678✔
323
  if (NULL == pJsonObj) {
1,751,227✔
324
    return TSDB_CODE_SUCCESS;
24,149✔
325
  }
326
  return func(pJsonObj, pObj);
1,727,078✔
327
}
328

329
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FToObject func, void** pObj, int32_t objSize) {
21,756✔
330
  if (objSize <= 0) {
21,756✔
331
    return TSDB_CODE_SUCCESS;
13,068✔
332
  }
333

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

345
int32_t tjsonToArray(const SJson* pJson, const char* pName, FToObject func, void* pArray, int32_t itemSize) {
38,095✔
346
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
38,095✔
347
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
38,094✔
348
  for (int32_t i = 0; i < size; ++i) {
109,171✔
349
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
71,075✔
350
    if (TSDB_CODE_SUCCESS != code) {
71,076!
351
      return code;
×
352
    }
353
  }
354
  return TSDB_CODE_SUCCESS;
38,096✔
355
}
356

357
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FToObject func, SArray** pArray, int32_t itemSize) {
8,676✔
358
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
8,676✔
359
  int32_t      size = tjsonGetArraySize(jArray);
8,676✔
360
  if (size > 0) {
8,676!
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;
8,676✔
373
}
374

375
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
62,973✔
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