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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

87.17
/source/client/src/clientSmlJson.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
#include <ctype.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20
#include "clientSml.h"
21

22
#define OTD_JSON_SUB_FIELDS_NUM 2
23

24
static inline int32_t smlParseMetricFromJSON(SSmlHandle *info, cJSON *metric, SSmlLineInfo *elements) {
431,973✔
25
  elements->measureLen = strlen(metric->valuestring);
431,973✔
26
  if (IS_INVALID_TABLE_LEN(elements->measureLen)) {
431,973✔
27
    uError("SML:0x%" PRIx64 " Metric length is 0 or large than 192", info->id);
1,190✔
28
    return TSDB_CODE_TSC_INVALID_TABLE_ID_LENGTH;
1,190✔
29
  }
30

31
  elements->measure = metric->valuestring;
430,796✔
32
  return TSDB_CODE_SUCCESS;
430,796✔
33
}
34

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
431,973✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
2,159,826✔
38
    cJSON *child = root->child;
1,727,853✔
39
    while (child != NULL) {
4,319,639✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
4,319,678✔
41
        *marks[i] = child;
1,727,892✔
42
        break;
1,727,892✔
43
      }
44
      child = child->next;
2,591,786✔
45
    }
46
    if (*marks[i] == NULL) {
1,727,853✔
47
      uError("SML %s error, not find mark:%d:%s", __FUNCTION__, i, jsonName[i]);
×
48
      return TSDB_CODE_TSC_INVALID_JSON;
×
49
    }
50
  }
51
  return TSDB_CODE_SUCCESS;
431,973✔
52
}
53

54
static int32_t smlConvertJSONBool(SSmlKv *pVal, char *typeStr, cJSON *value) {
413,377✔
55
  if (strcasecmp(typeStr, "bool") != 0) {
413,377✔
56
    uError("SML:invalid type(%s) for JSON Bool", typeStr);
×
57
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
58
  }
59
  pVal->type = TSDB_DATA_TYPE_BOOL;
413,377✔
60
  pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
413,377✔
61
  pVal->i = value->valueint;
413,377✔
62

63
  return TSDB_CODE_SUCCESS;
413,377✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
620,342✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
620,342✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
108,142✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,380✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,380✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
105,762✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
105,762✔
75
    pVal->i = value->valuedouble;
105,762✔
76
    return TSDB_CODE_SUCCESS;
105,762✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
512,200✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
104,572✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,380✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,380✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
102,192✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
102,192✔
86
    pVal->i = value->valuedouble;
102,192✔
87
    return TSDB_CODE_SUCCESS;
102,192✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
407,628✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
103,402✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,380✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,380✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
101,022✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
101,022✔
97
    pVal->i = value->valuedouble;
101,022✔
98
    return TSDB_CODE_SUCCESS;
101,022✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
304,226✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
103,385✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
103,385✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
103,385✔
105
      pVal->i = INT64_MAX;
94,605✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
8,780✔
107
      pVal->i = INT64_MIN;
1,190✔
108
    } else {
109
      pVal->i = value->valuedouble;
7,590✔
110
    }
111
    return TSDB_CODE_SUCCESS;
103,385✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
200,841✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
102,219✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,380✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,380✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
99,839✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
99,839✔
121
    pVal->f = value->valuedouble;
99,839✔
122
    return TSDB_CODE_SUCCESS;
99,839✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
98,622✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
98,622✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
98,622✔
128
    pVal->d = value->valuedouble;
98,622✔
129
    return TSDB_CODE_SUCCESS;
98,622✔
130
  }
131

132
  // if reach here means type is unsupported
133
  uError("SML:invalid type(%s) for JSON Number", typeStr);
×
134
  return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
135
}
136

137
static int32_t smlConvertJSONString(SSmlKv *pVal, char *typeStr, cJSON *value) {
437,423✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
437,423✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
318,428✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
118,995✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
118,995✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
101,740✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
17,255✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
17,255✔
147
  }
148
  pVal->length = strlen(value->valuestring);
420,168✔
149

150
  if ((pVal->type == TSDB_DATA_TYPE_BINARY || pVal->type == TSDB_DATA_TYPE_VARBINARY) && pVal->length > TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE) {
420,168✔
151
    return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
×
152
  }
153
  if (pVal->type == TSDB_DATA_TYPE_NCHAR &&
420,168✔
154
      pVal->length > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
101,740✔
155
    return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
×
156
  }
157

158
  pVal->value = value->valuestring;
420,168✔
159
  return TSDB_CODE_SUCCESS;
420,168✔
160
}
161

162
static int32_t smlParseValueFromJSONObj(cJSON *root, SSmlKv *kv) {
1,282,297✔
163
  int32_t ret = TSDB_CODE_SUCCESS;
1,282,297✔
164
  int32_t size = cJSON_GetArraySize(root);
1,282,297✔
165

166
  if (size != OTD_JSON_SUB_FIELDS_NUM) {
1,282,297✔
167
    return TSDB_CODE_TSC_INVALID_JSON;
×
168
  }
169

170
  cJSON *value = cJSON_GetObjectItem(root, "value");
1,282,297✔
171
  if (value == NULL) {
1,282,297✔
172
    return TSDB_CODE_TSC_INVALID_JSON;
×
173
  }
174

175
  cJSON *type = cJSON_GetObjectItem(root, "type");
1,282,297✔
176
  if (!cJSON_IsString(type)) {
1,282,297✔
177
    return TSDB_CODE_TSC_INVALID_JSON;
×
178
  }
179

180
  switch (value->type) {
1,282,297✔
181
    case cJSON_True:
413,377✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
413,377✔
184
      if (ret != TSDB_CODE_SUCCESS) {
413,377✔
185
        return ret;
×
186
      }
187
      break;
413,377✔
188
    }
189
    case cJSON_Number: {
620,342✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
620,342✔
191
      if (ret != TSDB_CODE_SUCCESS) {
620,342✔
192
        return ret;
9,520✔
193
      }
194
      break;
610,822✔
195
    }
196
    case cJSON_String: {
248,578✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
248,578✔
198
      if (ret != TSDB_CODE_SUCCESS) {
248,578✔
199
        return ret;
17,255✔
200
      }
201
      break;
231,323✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,255,522✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
2,174,134✔
211
  switch (root->type) {
2,174,134✔
212
    case cJSON_True:
279,761✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
279,761✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
279,761✔
216
      kv->i = root->valueint;
279,761✔
217
      break;
279,761✔
218
    }
219
    case cJSON_Number: {
422,054✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
422,054✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
422,054✔
222
      kv->d = root->valuedouble;
422,054✔
223
      break;
422,054✔
224
    }
225
    case cJSON_String: {
188,845✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
188,845✔
227
      if (ret != TSDB_CODE_SUCCESS) {
188,845✔
UNCOV
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
188,845✔
232
    }
233
    case cJSON_Object: {
1,282,297✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
1,282,297✔
235
      if (ret != TSDB_CODE_SUCCESS) {
1,282,297✔
236
        uError("SML:Failed to parse value from JSON Obj");
26,775✔
237
        return ret;
26,775✔
238
      }
239
      break;
1,255,522✔
240
    }
241
    default:
1,190✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,190✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
2,146,182✔
246
}
247

248
static int32_t smlProcessTagJson(SSmlHandle *info, cJSON *tags){
191,449✔
249
  SArray *preLineKV = info->preLineTagKV;
191,449✔
250
  taosArrayClearEx(preLineKV, freeSSmlKv);
191,449✔
251
  int     cnt = 0;
191,449✔
252

253
  int32_t tagNum = cJSON_GetArraySize(tags);
191,449✔
254
  if (unlikely(tagNum == 0)) {
191,449✔
255
    uError("SML:Tag should not be empty");
1,190✔
256
    return TSDB_CODE_TSC_INVALID_JSON;
1,190✔
257
  }
258
  for (int32_t i = 0; i < tagNum; ++i) {
1,917,589✔
259
    cJSON *tag = cJSON_GetArrayItem(tags, i);
1,743,946✔
260
    if (unlikely(tag == NULL)) {
1,743,972✔
261
      return TSDB_CODE_TSC_INVALID_JSON;
×
262
    }
263
    size_t keyLen = strlen(tag->string);
1,743,972✔
264
    if (unlikely(IS_INVALID_COL_LEN(keyLen))) {
1,743,972✔
265
      uError("SML:Tag key length is 0 or too large than 64");
595✔
266
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
595✔
267
    }
268

269
    // add kv to SSmlKv
270
    SSmlKv kv = {0};
1,743,377✔
271
    kv.key = tag->string;
1,743,377✔
272
    kv.keyLen = keyLen;
1,743,377✔
273

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
1,743,377✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,743,351✔
277
      return ret;
11,900✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
1,731,451✔
280
      return terrno;
×
281
    }
282

283
    if (info->dataFormat && !isSmlTagAligned(info, cnt, &kv)) {
1,731,451✔
284
      return TSDB_CODE_TSC_INVALID_JSON;
4,121✔
285
    }
286

287
    cnt++;
1,727,330✔
288
  }
289
  return TSDB_CODE_SUCCESS;
173,643✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
414,731✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
414,731✔
294
    elements->measureTag = info->preLine.measureTag;
57,569✔
295
    return TSDB_CODE_SUCCESS;
57,569✔
296
  }
297
  int32_t code = 0;
357,162✔
298
  int32_t lino = 0;
357,162✔
299
  if(info->dataFormat){
357,162✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
184,256✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
191,449✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
173,643✔
304
  return smlProcessChildTable(info, elements);
173,643✔
305

306
END:
183,519✔
307
  if(info->reRun){
183,519✔
308
    return TSDB_CODE_SUCCESS;
167,990✔
309
  }
310
  RETURN
15,529✔
311
}
312

313
static int64_t smlParseTSFromJSONObj(SSmlHandle *info, cJSON *root, int32_t toPrecision) {
136,088✔
314
  int32_t size = cJSON_GetArraySize(root);
136,088✔
315
  if (unlikely(size != OTD_JSON_SUB_FIELDS_NUM)) {
136,088✔
316
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
×
317
    return TSDB_CODE_TSC_INVALID_JSON;
×
318
  }
319

320
  cJSON *value = cJSON_GetObjectItem(root, "value");
136,088✔
321
  if (unlikely(!cJSON_IsNumber(value))) {
136,088✔
322
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,785✔
323
    return TSDB_CODE_TSC_INVALID_JSON;
1,785✔
324
  }
325

326
  cJSON *type = cJSON_GetObjectItem(root, "type");
134,303✔
327
  if (unlikely(!cJSON_IsString(type))) {
134,303✔
328
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
×
329
    return TSDB_CODE_TSC_INVALID_JSON;
×
330
  }
331

332
  double timeDouble = value->valuedouble;
134,303✔
333
  if (unlikely(smlDoubleToInt64OverFlow(timeDouble))) {
134,303✔
334
    smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL);
×
335
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
336
  }
337

338
  if (timeDouble == 0) {
134,303✔
339
    return taosGetTimestampNs() / smlFactorNS[toPrecision];
×
340
  }
341

342
  if (timeDouble < 0) {
134,303✔
343
    return (int64_t)timeDouble;
×
344
  }
345

346
  int64_t tsInt64 = (int64_t)timeDouble;
134,303✔
347
  size_t  typeLen = strlen(type->valuestring);
134,303✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
134,303✔
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
1,786✔
351
      return tsInt64 * smlFactorS[toPrecision];
1,786✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
132,517✔
355
    switch (type->valuestring[0]) {
132,517✔
356
      case 'm':
21,252✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
21,252✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
111,265✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
111,265✔
367
      default:
×
368
        return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
369
    }
370
  } else {
UNCOV
371
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
372
  }
373
}
374

375
uint8_t smlGetTimestampLen(int64_t num) {
91,118✔
376
  uint8_t len = 0;
91,118✔
377
  while ((num /= 10) != 0) {
1,051,386✔
378
    len++;
960,268✔
379
  }
380
  len++;
91,118✔
381
  return len;
91,118✔
382
}
383

384
static int64_t smlParseTSFromJSON(SSmlHandle *info, cJSON *timestamp) {
231,212✔
385
  // Timestamp must be the first KV to parse
386
  int32_t toPrecision = info->currSTableMeta ? info->currSTableMeta->tableInfo.precision : TSDB_TIME_PRECISION_NANO;
231,212✔
387
  if (cJSON_IsNumber(timestamp)) {
231,212✔
388
    // timestamp value 0 indicates current system time
389
    double timeDouble = timestamp->valuedouble;
93,339✔
390
    if (unlikely(smlDoubleToInt64OverFlow(timeDouble))) {
93,339✔
391
      smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL);
×
392
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
393
    }
394

395
    if (unlikely(timeDouble < 0)) {
93,326✔
396
      smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is negative", NULL);
×
397
      return (int64_t)timeDouble;
×
398
    } else if (unlikely(timeDouble == 0)) {
93,326✔
399
      return taosGetTimestampNs() / smlFactorNS[toPrecision];
7,146✔
400
    }
401

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
89,753✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
89,766✔
405
    if (unlikely(fromPrecision == -1)) {
89,766✔
406
      smlBuildInvalidDataMsg(&info->msgBuf,
×
407
                             "timestamp precision can only be seconds(10 digits) or milli seconds(13 digits)", NULL);
408
      return TSDB_CODE_SML_INVALID_DATA;
×
409
    }
410
    int64_t tsInt64 = (int64_t)timeDouble;
89,766✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
89,766✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
39,304✔
413
        return tsInt64 * smlFactorS[toPrecision];
39,304✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
50,462✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
137,873✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
136,088✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,785✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
1,785✔
424
  }
425
}
426

427
static int32_t smlParseJSONStringExt(SSmlHandle *info, cJSON *root, SSmlLineInfo *elements) {
434,288✔
428
  int32_t code = TSDB_CODE_SUCCESS;
434,288✔
429
  int32_t lino = 0;
434,288✔
430

431
  cJSON *metricJson = NULL;
434,288✔
432
  cJSON *tsJson = NULL;
434,288✔
433
  cJSON *valueJson = NULL;
434,288✔
434
  cJSON *tagsJson = NULL;
434,288✔
435

436
  int32_t size = cJSON_GetArraySize(root);
434,288✔
437
  // outmost json fields has to be exactly 4
438
  if (size != OTD_JSON_FIELDS_NUM) {
434,353✔
439
    uError("SML:0x%" PRIx64 " Invalid number of JSON fields in data point %d", info->id, size);
2,380✔
440
    return TSDB_CODE_TSC_INVALID_JSON;
2,380✔
441
  }
442

443
  cJSON **marks[OTD_JSON_FIELDS_NUM] = {&metricJson, &tsJson, &valueJson, &tagsJson};
431,973✔
444
  SML_CHECK_CODE(smlGetJsonElements(root, marks));
431,973✔
445
  // Parse metric
446
  SML_CHECK_CODE(smlParseMetricFromJSON(info, metricJson, elements));
431,986✔
447
  // Parse metric value
448
  SSmlKv kv = {.key = VALUE, .keyLen = VALUE_LEN};
430,796✔
449
  SML_CHECK_CODE(smlParseValueFromJSON(valueJson, &kv));
430,796✔
450

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
414,731✔
453
  SML_CHECK_NULL(elements->tags);
414,731✔
454

455
  elements->tagsLen = strlen(elements->tags);
414,731✔
456
  SML_CHECK_CODE(smlParseTagsFromJSON(info, tagsJson, elements));
414,731✔
457

458
  if (unlikely(info->reRun)) {
399,202✔
459
    goto END;
167,990✔
460
  }
461

462
  // Parse timestamp
463
  // notice!!! put ts back to tag to ensure get meta->precision
464
  int64_t ts = smlParseTSFromJSON(info, tsJson);
231,212✔
465
  if (unlikely(ts < 0)) {
231,199✔
466
    char* tmp = cJSON_PrintUnformatted(tsJson);
3,570✔
467
    if (tmp == NULL) {
3,570✔
468
      uError("SML:0x%" PRIx64 " Unable to parse timestamp from JSON payload %s %" PRId64, info->id, info->msgBuf.buf, ts);
×
469
    } else {
470
      uError("SML:0x%" PRIx64 " Unable to parse timestamp from JSON payload %s %s %" PRId64, info->id, info->msgBuf.buf,tmp, ts);
3,570✔
471
      taosMemoryFree(tmp);
3,570✔
472
    }
473
    SML_CHECK_CODE(TSDB_CODE_INVALID_TIMESTAMP);
3,570✔
474
  }
475
  SSmlKv kvTs = {0};
227,629✔
476
  smlBuildTsKv(&kvTs, ts);
227,629✔
477
  if (info->dataFormat){
227,642✔
478
    code = smlParseEndTelnetJsonFormat(info, elements, &kvTs, &kv);
55,718✔
479
  } else {
480
    code = smlParseEndTelnetJsonUnFormat(info, elements, &kvTs, &kv);
171,924✔
481
  }
482
  SML_CHECK_CODE(code);
227,629✔
483
  taosMemoryFreeClear(info->preLine.tags);
227,629✔
484
  info->preLine = *elements;
227,629✔
485
  elements->tags = NULL;
227,629✔
486

487
END:
431,973✔
488
  taosMemoryFree(elements->tags);
431,973✔
489
  RETURN
431,895✔
490
}
491

492
int32_t smlParseJSONExt(SSmlHandle *info, char *payload) {
204,384✔
493
  int32_t payloadNum = 0;
204,384✔
494
  int32_t ret = TSDB_CODE_SUCCESS;
204,384✔
495

496
  info->root = cJSON_Parse(payload);
204,384✔
497
  if (unlikely(info->root == NULL)) {
204,384✔
498
    uError("SML:0x%" PRIx64 " parse json failed:%s", info->id, payload);
2,380✔
499
    return TSDB_CODE_TSC_INVALID_JSON;
2,380✔
500
  }
501

502
  // multiple data points must be sent in JSON array
503
  if (cJSON_IsArray(info->root)) {
202,004✔
504
    payloadNum = cJSON_GetArraySize(info->root);
25,271✔
505
  } else if (cJSON_IsObject(info->root)) {
176,726✔
506
    payloadNum = 1;
176,733✔
507
  } else {
508
    uError("SML:0x%" PRIx64 " Invalid JSON type:%s", info->id, payload);
×
509
    return TSDB_CODE_TSC_INVALID_JSON;
×
510
  }
511

512
  info->lineNum = payloadNum;
201,978✔
513
  cJSON *head = (payloadNum == 1 && cJSON_IsObject(info->root)) ? info->root : info->root->child;
201,978✔
514

515
  int    cnt = 0;
201,978✔
516
  cJSON *dataPoint = head;
201,978✔
517
  while (dataPoint) {
597,610✔
518
    if (info->dataFormat) {
434,353✔
519
      SSmlLineInfo element = {0};
245,769✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
245,769✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
245,782✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
188,584✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
434,366✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
38,734✔
527
      return ret;
38,734✔
528
    }
529

530
    if (unlikely(info->reRun)) {
395,632✔
531
      cnt = 0;
167,990✔
532
      dataPoint = head;
167,990✔
533
      info->lineNum = payloadNum;
167,990✔
534
      ret = smlClearForRerun(info);
167,990✔
535
      if (ret != TSDB_CODE_SUCCESS) {
167,990✔
536
        return ret;
×
537
      }
538
      continue;
167,990✔
539
    }
540
    cnt++;
227,642✔
541
    dataPoint = dataPoint->next;
227,642✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
163,257✔
545
}
546

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