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

taosdata / TDengine / #3558

17 Dec 2024 06:05AM UTC coverage: 59.778% (+1.6%) from 58.204%
#3558

push

travis-ci

web-flow
Merge pull request #29179 from taosdata/merge/mainto3.0

merge: form main to 3.0 branch

132787 of 287595 branches covered (46.17%)

Branch coverage included in aggregate %.

104 of 191 new or added lines in 5 files covered. (54.45%)

6085 existing lines in 168 files now uncovered.

209348 of 284746 relevant lines covered (73.52%)

8164844.48 hits per line

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

57.26
/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() {
8,564,722✔
23
  SJson* pJson = cJSON_CreateObject();
8,564,722✔
24
  if (pJson == NULL) {
8,566,846!
UNCOV
25
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
26
  }
27
  return pJson;
8,566,848✔
28
}
29

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

38
void tjsonDelete(SJson* pJson) {
140,969✔
39
  if (pJson != NULL) {
140,969!
40
    cJSON_Delete((cJSON*)pJson);
140,975✔
41
  }
42
}
140,976✔
43

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

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

55
  return TSDB_CODE_SUCCESS;
411,083✔
56
}
57

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

63
  return TSDB_CODE_SUCCESS;
4,606,824✔
64
}
65

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

71
  return TSDB_CODE_SUCCESS;
37,036,661✔
72
}
73

74
SJson* tjsonAddArrayToObject(SJson* pJson, const char* pName) {
550,941✔
75
  SJson* ret = (SJson*)cJSON_AddArrayToObject((cJSON*)pJson, pName);
550,941✔
76
  if (ret == NULL) {
550,943!
77
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
78
  }
79
  return ret;
550,943✔
80
}
81

82
int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem) {
6,187,922✔
83
  if (cJSON_AddItemToObject((cJSON*)pJson, pName, pItem)) {
6,187,922!
84
    return TSDB_CODE_SUCCESS;
6,188,529✔
85
  }
86

87
  return terrno = TSDB_CODE_OUT_OF_MEMORY;
×
88
}
89

90
int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem) {
2,341,245✔
91
  if (cJSON_AddItemToArray((cJSON*)pJson, pItem)) {
2,341,245!
92
    return TSDB_CODE_SUCCESS;
2,341,175✔
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) {
6,350,591✔
99
  if (NULL == pObj) {
6,350,591✔
100
    return TSDB_CODE_SUCCESS;
231,782✔
101
  }
102

103
  SJson* pJobj = tjsonCreateObject();
6,118,809✔
104
  if (NULL == pJobj) {
6,119,858!
105
    return terrno;
×
106
  }
107
  int32_t rc = func(pObj, pJobj);
6,119,858✔
108
  if (rc != TSDB_CODE_SUCCESS) {
6,118,790!
109
    tjsonDelete(pJobj);
×
110
    return rc;
×
111
  }
112
  return tjsonAddItemToObject(pJson, pName, pJobj);
6,118,790✔
113
}
114

115
int32_t tjsonAddItem(SJson* pJson, FToJson func, const void* pObj) {
2,213,413✔
116
  SJson* pJobj = tjsonCreateObject();
2,213,413✔
117
  if (pJobj == NULL) {
2,213,566!
118
    return terrno;
×
119
  }
120

121
  int32_t rc = func(pObj, pJobj);
2,213,566✔
122
  if (rc != TSDB_CODE_SUCCESS) {
2,213,447!
123
    tjsonDelete(pJobj);
×
124
    return rc;
×
125
  }
126
  return tjsonAddItemToArray(pJson, pJobj);
2,213,447✔
127
}
128

129
int32_t tjsonAddArray(SJson* pJson, const char* pName, FToJson func, const void* pArray, int32_t itemSize,
28,712✔
130
                      int32_t num) {
131
  if (num > 0) {
28,712✔
132
    SJson* pJsonArray = tjsonAddArrayToObject(pJson, pName);
24,973✔
133
    if (NULL == pJsonArray) {
24,972!
134
      return terrno;
×
135
    }
136
    for (size_t i = 0; i < num; ++i) {
111,816✔
137
      int32_t code = tjsonAddItem(pJsonArray, func, (const char*)pArray + itemSize * i);
86,850✔
138
      if (TSDB_CODE_SUCCESS != code) {
86,844!
139
        return code;
×
140
      }
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS;
28,705✔
144
}
145

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

163
char* tjsonToString(const SJson* pJson) {
85,349✔
164
  char* p = cJSON_Print((cJSON*)pJson);
85,349✔
165
  if (!p) {
85,352!
166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
167
  }
168
  return p;
85,355✔
169
}
170

171
char* tjsonToUnformattedString(const SJson* pJson) {
23,140✔
172
  char* p = cJSON_PrintUnformatted((cJSON*)pJson);
23,140✔
173
  if (!p) {
23,141!
174
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
175
  }
176
  return p;
23,142✔
177
}
178

179
SJson* tjsonGetObjectItem(const SJson* pJson, const char* pName) { return cJSON_GetObjectItem(pJson, pName); }
62,754,645✔
180

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

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

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

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

232
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal) {
28,411,912✔
233
  char* p = cJSON_GetStringValue(tjsonGetObjectItem((cJSON*)pJson, pName));
28,411,912✔
234
  if (NULL == p) {
28,431,343✔
235
    return TSDB_CODE_SUCCESS;
76,608✔
236
  }
237
#ifdef WINDOWS
238
  sscanf(p, "%" PRId64, pVal);
239
#else
240
  *pVal = taosStr2Int64(p, NULL, 10);
28,354,735✔
241
#endif
242
  return TSDB_CODE_SUCCESS;
28,344,743✔
243
}
244

245
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal) {
10,611,678✔
246
  int64_t val = 0;
10,611,678✔
247
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
10,611,678✔
248
  *pVal = val;
10,616,004✔
249
  return code;
10,616,004✔
250
}
251

252
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal) {
8,231,935✔
253
  int64_t val = 0;
8,231,935✔
254
  int32_t code = tjsonGetBigIntValue(pJson, pName, &val);
8,231,935✔
255
  *pVal = val;
8,239,004✔
256
  return code;
8,239,004✔
257
}
258

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

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

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

286
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal) {
8,471,042✔
287
  uint64_t val = 0;
8,471,042✔
288
  int32_t  code = tjsonGetUBigIntValue(pJson, pName, &val);
8,471,042✔
289
  *pVal = val;
8,466,932✔
290
  return code;
8,466,932✔
291
}
292

293
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal) {
5,826,519✔
294
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
5,826,519✔
295
  if (NULL == pObject) {
5,829,004✔
296
    return TSDB_CODE_SUCCESS;
16✔
297
  }
298
  if (!cJSON_IsBool(pObject)) {
5,828,988!
299
    return TSDB_CODE_FAILED;
×
300
  }
301
  *pVal = cJSON_IsTrue(pObject) ? true : false;
5,829,189✔
302
  return TSDB_CODE_SUCCESS;
5,828,823✔
303
}
304

305
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal) {
49,028✔
306
  const SJson* pObject = tjsonGetObjectItem(pJson, pName);
49,028✔
307
  if (NULL == pObject) {
49,025✔
308
    return TSDB_CODE_SUCCESS;
1,196✔
309
  }
310
  if (!cJSON_IsNumber(pObject)) {
47,829!
311
    return TSDB_CODE_FAILED;
×
312
  }
313
  *pVal = cJSON_GetNumberValue(pObject);
47,829✔
314
  return TSDB_CODE_SUCCESS;
47,830✔
315
}
316

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

319
SJson* tjsonGetArrayItem(const SJson* pJson, int32_t index) { return cJSON_GetArrayItem(pJson, index); }
3,272,240✔
320

321
int32_t tjsonToObject(const SJson* pJson, const char* pName, FToObject func, void* pObj) {
6,506,339✔
322
  SJson* pJsonObj = tjsonGetObjectItem(pJson, pName);
6,506,339✔
323
  if (NULL == pJsonObj) {
6,510,550✔
324
    return TSDB_CODE_SUCCESS;
16✔
325
  }
326
  return func(pJsonObj, pObj);
6,510,534✔
327
}
328

329
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FToObject func, void** pObj, int32_t objSize) {
15,528✔
330
  if (objSize <= 0) {
15,528✔
331
    return TSDB_CODE_SUCCESS;
4,201✔
332
  }
333

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

345
int32_t tjsonToArray(const SJson* pJson, const char* pName, FToObject func, void* pArray, int32_t itemSize) {
36,285✔
346
  const cJSON* jArray = tjsonGetObjectItem(pJson, pName);
36,285✔
347
  int32_t      size = (NULL == jArray ? 0 : tjsonGetArraySize(jArray));
36,284✔
348
  for (int32_t i = 0; i < size; ++i) {
497,671✔
349
    int32_t code = func(tjsonGetArrayItem(jArray, i), (char*)pArray + itemSize * i);
461,386✔
350
    if (TSDB_CODE_SUCCESS != code) {
461,385!
351
      return code;
×
352
    }
353
  }
354
  return TSDB_CODE_SUCCESS;
36,285✔
355
}
356

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

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