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

taosdata / TDengine / #4976

06 Mar 2026 09:48AM UTC coverage: 68.446% (+0.08%) from 68.37%
#4976

push

travis-ci

web-flow
feat(TDgpt): support multiple input data columns for anomaly detection. (#34606)

0 of 93 new or added lines in 9 files covered. (0.0%)

5718 existing lines in 144 files now uncovered.

211146 of 308486 relevant lines covered (68.45%)

136170362.0 hits per line

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

74.6
/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() {
35,237,211✔
31
  SJson* pJson = cJSON_CreateArray();
35,237,211✔
32
  if (pJson == NULL) {
35,249,376✔
33
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
34
  }
35
  return pJson;
35,249,840✔
36
}
37

38
void tjsonDelete(SJson* pJson) {
742,208,528✔
39
  if (pJson != NULL) {
742,208,528✔
40
    cJSON_Delete((cJSON*)pJson);
742,209,919✔
41
  }
42
}
742,204,297✔
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) {
431,901,219✔
51
  if (NULL == cJSON_AddNumberToObject((cJSON*)pJson, pName, number)) {
431,901,219✔
UNCOV
52
    return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
53
  }
54

55
  return TSDB_CODE_SUCCESS;
431,906,619✔
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;
33,698✔
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✔
UNCOV
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;
×
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,
959,648,776✔
130
                      int32_t num) {
131
  if (num > 0) {
959,648,776✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
893,327,844✔
133
    if (NULL == pJsonArray) {
893,329,021✔
134
      return terrno;
3,244✔
135
    }
136
    for (size_t i = 0; i < num; ++i) {
1,869,484,359✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
976,157,091✔
138
      if (TSDB_CODE_SUCCESS != code) {
976,158,582✔
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
959,648,200✔
144
}
145

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

163
char* tjsonToString(const SJson* pJson) {
34,406,936✔
164
  char* p = cJSON_Print((cJSON*)pJson);
34,406,936✔
165
  if (!p) {
34,411,420✔
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
34,411,466✔
169
}
170

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

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

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

223
int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal) {
9,147,380✔
224
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
9,147,380✔
225
  if (NULL == p) {
9,147,062✔
226
    return TSDB_CODE_SUCCESS;
1,635,250✔
227
  }
228
  *pVal = taosStrdup(p);
7,511,812✔
229
  return TSDB_CODE_SUCCESS;
7,511,076✔
230
}
231

232
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
1,326,699,191✔
233
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
1,326,699,191✔
234
  if (NULL == p) {
1,327,446,827✔
235
    return TSDB_CODE_SUCCESS;
11,019,359✔
236
  }
237
#ifdef WINDOWS
238
  sscanf(p, "%" PRId64, pVal);
239
#else
240
  *pVal = taosStr2Int64(p, NULL, 10);
1,316,427,468✔
241
#endif
242
  return TSDB_CODE_SUCCESS;
1,316,820,499✔
243
}
244

245
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
465,439,312✔
246
  int64_t val = 0;
465,439,312✔
247
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
465,453,715✔
248
  *pVal = val;
465,521,892✔
249
  return code;
465,530,815✔
250
}
251

252
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
181,495,195✔
253
  int64_t val = 0;
181,495,195✔
254
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
181,499,836✔
255
  *pVal = val;
181,528,093✔
256
  return code;
181,529,798✔
257
}
258

259
int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal) {
67,524,191✔
260
  int64_t val = 0;
67,524,191✔
261
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
67,523,970✔
262
  *pVal = val;
67,532,718✔
263
  return code;
67,533,036✔
264
}
265

266
int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pVal) {
365,731,052✔
267
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
365,731,052✔
268
  if (NULL == p) {
365,730,984✔
269
    return TSDB_CODE_SUCCESS;
×
270
  }
271
#ifdef WINDOWS
272
  sscanf(p, "%" PRIu64, pVal);
273
#else
274
  *pVal = taosStr2UInt64(p, NULL, 10);
365,730,984✔
275
#endif
276
  return TSDB_CODE_SUCCESS;
365,758,478✔
277
}
278

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

286
int32_t tjsonGetUSmallIntValue(const SJson* pJson, const char* pName, uint16_t* pVal) {
653,540✔
287
  uint64_t val = 0;
653,540✔
288
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
653,540✔
289
  *pVal = val;
653,540✔
290
  return code;
653,540✔
291
}
292

293
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
324,009,243✔
294
  uint64_t val = 0;
324,009,243✔
295
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
324,014,972✔
296
  *pVal = val;
324,056,369✔
297
  return code;
324,059,005✔
298
}
299

300
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
423,305,577✔
301
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
423,305,577✔
302
  if (NULL == pObject) {
423,351,765✔
303
    return TSDB_CODE_SUCCESS;
×
304
  }
305
  if (!cJSON_IsBool(pObject)) {
423,351,765✔
306
    return TSDB_CODE_FAILED;
×
307
  }
308
  *pVal = cJSON_IsTrue(pObject) ? true : false;
423,356,542✔
309
  return TSDB_CODE_SUCCESS;
423,339,400✔
310
}
311

312
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
20,922,708✔
313
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
20,922,708✔
314
  if (NULL == pObject) {
20,923,482✔
315
    return TSDB_CODE_SUCCESS;
937,936✔
316
  }
317
  if (!cJSON_IsNumber(pObject)) {
19,985,546✔
318
    return TSDB_CODE_FAILED;
×
319
  }
320
  *pVal = cJSON_GetNumberValue(pObject);
19,985,489✔
321
  return TSDB_CODE_SUCCESS;
19,985,581✔
322
}
323

324
int32_t tjsonGetArraySize(const SJson* pJson) { return cJSON_GetArraySize(pJson); }
56,070,279✔
325

326
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
109,416,935✔
327

328
int32_t tjsonToObject(const SJson* pJson, const char* pName, FJsonToObject func, void* pObj) {
269,603,393✔
329
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
269,603,393✔
330
  if (NULL == pJsonObj) {
269,629,253✔
331
    return TSDB_CODE_SUCCESS;
3,303,274✔
332
  }
333
  return func(pJsonObj, pObj);
266,325,979✔
334
}
335

336
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FJsonToObject func, void** pObj, int32_t objSize) {
226,132✔
337
  if (objSize <= 0) {
226,132✔
338
    return TSDB_CODE_SUCCESS;
97,904✔
339
  }
340

341
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
128,228✔
342
  if (NULL == pJsonObj) {
128,228✔
343
    return TSDB_CODE_SUCCESS;
×
344
  }
345
  *pObj = taosMemoryCalloc(1, objSize);
128,228✔
346
  if (NULL == *pObj) {
128,228✔
347
    return terrno;
×
348
  }
349
  return func(pJsonObj, *pObj);
128,228✔
350
}
351

352
int32_t tjsonToArray(const SJson* pJson, const char* pName, FJsonToObject func, void* pArray, int32_t itemSize) {
4,906,066✔
353
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
4,906,066✔
354
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
4,906,422✔
355
  for (int32_t i = 0; i < size; ++i) {
9,598,816✔
356
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
4,692,394✔
357
    if (TSDB_CODE_SUCCESS != code) {
4,692,394✔
358
      return code;
×
359
    }
360
  }
361
  return TSDB_CODE_SUCCESS;
4,906,422✔
362
}
363

364
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FJsonToObject func, SArray** pArray, int32_t itemSize) {
3,916,245✔
365
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
3,916,245✔
366
  int32_t      size = tjsonGetArraySize(jArray);
3,916,245✔
367
  if (size > 0) {
3,916,245✔
368
    *pArray = taosArrayInit_s(itemSize, size);
2,149,053✔
369
    if (NULL == *pArray) {
2,149,053✔
370
      return terrno;
×
371
    }
372
    for (int32_t i = 0; i < size; ++i) {
6,063,087✔
373
      int32_t code = func(tjsonGetArrayItem(jArray, i), taosArrayGet(*pArray, i));
3,914,034✔
374
      if (TSDB_CODE_SUCCESS != code) {
3,914,034✔
375
        return code;
×
376
      }
377
    }
378
  }
379
  return TSDB_CODE_SUCCESS;
3,916,245✔
380
}
381

382
SJson* tjsonParse(const char* pStr) { return cJSON_Parse(pStr); }
12,913,958✔
383

384
bool tjsonValidateJson(const char* jIn) {
×
385
  if (!jIn) {
×
386
    return false;
×
387
  }
388

389
  // set json real data
390
  cJSON* root = cJSON_Parse(jIn);
×
391
  if (root == NULL) {
×
392
    return false;
×
393
  }
394

395
  if (!cJSON_IsObject(root)) {
×
396
    return false;
×
397
  }
398
  int size = cJSON_GetArraySize(root);
×
399
  for (int i = 0; i < size; i++) {
×
400
    cJSON* item = cJSON_GetArrayItem(root, i);
×
401
    if (!item) {
×
402
      return false;
×
403
    }
404

405
    char* jsonKey = item->string;
×
406
    if (!jsonKey) return false;
×
407
    for (size_t j = 0; j < strlen(jsonKey); ++j) {
×
408
      if (isprint(jsonKey[j]) == 0) return false;
×
409
    }
410

411
    if (item->type == cJSON_Object || item->type == cJSON_Array) {
×
412
      return false;
×
413
    }
414
  }
415
  return true;
×
416
}
417

418
const char* tjsonGetError() { return cJSON_GetErrorPtr(); }
×
419

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