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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 hits per line

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

74.49
/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() {
27,271,972✔
31
  SJson* pJson = cJSON_CreateArray();
27,271,972✔
32
  if (pJson == NULL) {
27,281,804✔
33
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
34
  }
35
  return pJson;
27,284,562✔
36
}
37

38
void tjsonDelete(SJson* pJson) {
672,020,137✔
39
  if (pJson != NULL) {
672,020,137✔
40
    cJSON_Delete((cJSON*)pJson);
672,021,660✔
41
  }
42
}
672,019,442✔
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) {
373,419,709✔
51
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
373,419,709✔
52
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
750✔
53
  }
54

55
  return TSDB_CODE_SUCCESS;
373,420,750✔
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;
19,626✔
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;
120✔
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

UNCOV
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,
811,548,649✔
130
                      int32_t num) {
131
  if (num > 0) {
811,548,649✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
751,969,766✔
133
    if (NULL == pJsonArray) {
751,968,446✔
134
      return terrno;
×
135
    }
136
    for (size_t i = 0; i < num; ++i) {
1,585,040,133✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
833,071,011✔
138
      if (TSDB_CODE_SUCCESS != code) {
833,071,687✔
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
811,548,005✔
144
}
145

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

163
char* tjsonToString(const SJson* pJson) {
32,366,071✔
164
  char* p = cJSON_Print((cJSON*)pJson);
32,366,071✔
165
  if (!p) {
32,371,055✔
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
32,370,794✔
169
}
170

171
char* tjsonToUnformattedString(const SJson* pJson) {
630,633,017✔
172
  char* p = cJSON_PrintUnformatted((cJSON*)pJson);
630,633,017✔
173
  if (!p) {
630,635,973✔
174
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
175
  }
176
  return p;
630,635,040✔
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) {
66✔
182
  *pName = ((cJSON*)pJson)->string;
66✔
183
  if (NULL == *pName) {
66✔
184
    return TSDB_CODE_FAILED;
×
185
  }
186
  return TSDB_CODE_SUCCESS;
66✔
187
}
188

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

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

223
int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal) {
7,096,264✔
224
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
7,096,264✔
225
  if (NULL == p) {
7,096,264✔
226
    return TSDB_CODE_SUCCESS;
1,190,032✔
227
  }
228
  *pVal = taosStrdup(p);
5,906,232✔
229
  return TSDB_CODE_SUCCESS;
5,906,232✔
230
}
231

232
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
1,094,477,986✔
233
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
1,094,477,986✔
234
  if (NULL == p) {
1,094,978,297✔
235
    return TSDB_CODE_SUCCESS;
7,506,247✔
236
  }
237
#ifdef WINDOWS
238
  sscanf(p, "%" PRId64, pVal);
239
#else
240
  *pVal = taosStr2Int64(p, NULL, 10);
1,087,472,050✔
241
#endif
242
  return TSDB_CODE_SUCCESS;
1,087,771,268✔
243
}
244

245
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
360,771,417✔
246
  int64_t val = 0;
360,771,417✔
247
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
360,776,082✔
248
  *pVal = val;
360,826,918✔
249
  return code;
360,830,178✔
250
}
251

252
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
203,007,275✔
253
  int64_t val = 0;
203,007,275✔
254
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
203,012,937✔
255
  *pVal = val;
203,041,804✔
256
  return code;
203,043,496✔
257
}
258

259
int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal) {
54,323,782✔
260
  int64_t val = 0;
54,323,782✔
261
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
54,323,518✔
262
  *pVal = val;
54,327,056✔
263
  return code;
54,327,320✔
264
}
265

266
int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pVal) {
290,819,974✔
267
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
290,819,974✔
268
  if (NULL == p) {
290,824,074✔
269
    return TSDB_CODE_SUCCESS;
×
270
  }
271
#ifdef WINDOWS
272
  sscanf(p, "%" PRIu64, pVal);
273
#else
274
  *pVal = taosStr2UInt64(p, NULL, 10);
290,824,074✔
275
#endif
276
  return TSDB_CODE_SUCCESS;
290,845,366✔
277
}
278

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

286
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
256,693,846✔
287
  uint64_t val = 0;
256,693,846✔
288
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
256,696,930✔
289
  *pVal = val;
256,724,851✔
290
  return code;
256,726,889✔
291
}
292

293
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
304,261,838✔
294
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
304,261,838✔
295
  if (NULL == pObject) {
304,279,268✔
296
    return TSDB_CODE_SUCCESS;
×
297
  }
298
  if (!cJSON_IsBool(pObject)) {
304,279,268✔
299
    return TSDB_CODE_FAILED;
×
300
  }
301
  *pVal = cJSON_IsTrue(pObject) ? true : false;
304,277,514✔
302
  return TSDB_CODE_SUCCESS;
304,272,749✔
303
}
304

305
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
19,368,169✔
306
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
19,368,169✔
307
  if (NULL == pObject) {
19,368,508✔
308
    return TSDB_CODE_SUCCESS;
883,839✔
309
  }
310
  if (!cJSON_IsNumber(pObject)) {
18,484,669✔
311
    return TSDB_CODE_FAILED;
×
312
  }
313
  *pVal = cJSON_GetNumberValue(pObject);
18,484,497✔
314
  return TSDB_CODE_SUCCESS;
18,485,176✔
315
}
316

317
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
42,641,267✔
318

319
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
91,910,859✔
320

321
int32_t tjsonToObject(const SJson* pJson, const char* pName, FJsonToObject func, void* pObj) {
218,203,984✔
322
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
218,203,984✔
323
  if (NULL == pJsonObj) {
218,204,024✔
324
    return TSDB_CODE_SUCCESS;
2,707,232✔
325
  }
326
  return func(pJsonObj, pObj);
215,496,792✔
327
}
328

329
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FJsonToObject func, void** pObj, int32_t objSize) {
184,606✔
330
  if (objSize <= 0) {
184,606✔
331
    return TSDB_CODE_SUCCESS;
92,303✔
332
  }
333

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

345
int32_t tjsonToArray(const SJson* pJson, const char* pName, FJsonToObject func, void* pArray, int32_t itemSize) {
4,000,338✔
346
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
4,000,338✔
347
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
4,000,357✔
348
  for (int32_t i = 0; i < size; ++i) {
7,797,490✔
349
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
3,797,133✔
350
    if (TSDB_CODE_SUCCESS != code) {
3,797,133✔
351
      return code;
×
352
    }
353
  }
354
  return TSDB_CODE_SUCCESS;
4,000,357✔
355
}
356

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

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