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

taosdata / TDengine / #4917

07 Jan 2026 03:52PM UTC coverage: 65.42% (+0.02%) from 65.402%
#4917

push

travis-ci

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

31 of 34 new or added lines in 2 files covered. (91.18%)

819 existing lines in 129 files now uncovered.

202679 of 309814 relevant lines covered (65.42%)

116724351.99 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) {
399,373✔
25
  elements->measureLen = strlen(metric->valuestring);
399,373✔
26
  if (IS_INVALID_TABLE_LEN(elements->measureLen)) {
399,373✔
27
    uError("SML:0x%" PRIx64 " Metric length is 0 or large than 192", info->id);
1,100✔
28
    return TSDB_CODE_TSC_INVALID_TABLE_ID_LENGTH;
1,100✔
29
  }
30

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
398,797✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
1,996,269✔
38
    cJSON *child = root->child;
1,597,472✔
39
    while (child != NULL) {
3,993,643✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
3,993,619✔
41
        *marks[i] = child;
1,597,448✔
42
        break;
1,597,448✔
43
      }
44
      child = child->next;
2,396,171✔
45
    }
46
    if (*marks[i] == NULL) {
1,597,472✔
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;
398,797✔
52
}
53

54
static int32_t smlConvertJSONBool(SSmlKv *pVal, char *typeStr, cJSON *value) {
382,108✔
55
  if (strcasecmp(typeStr, "bool") != 0) {
382,108✔
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;
382,108✔
60
  pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
382,108✔
61
  pVal->i = value->valueint;
382,108✔
62

63
  return TSDB_CODE_SUCCESS;
382,108✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
573,393✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
573,393✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
99,934✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,200✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,200✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
97,734✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
97,734✔
75
    pVal->i = value->valuedouble;
97,734✔
76
    return TSDB_CODE_SUCCESS;
97,734✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
473,459✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
96,658✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,200✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,200✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
94,458✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
94,458✔
86
    pVal->i = value->valuedouble;
94,458✔
87
    return TSDB_CODE_SUCCESS;
94,458✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
376,801✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
95,595✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,200✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,200✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
93,395✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
93,395✔
97
    pVal->i = value->valuedouble;
93,395✔
98
    return TSDB_CODE_SUCCESS;
93,395✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
281,206✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
95,552✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
95,552✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
95,552✔
105
      pVal->i = INT64_MAX;
87,450✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
8,102✔
107
      pVal->i = INT64_MIN;
1,100✔
108
    } else {
109
      pVal->i = value->valuedouble;
7,002✔
110
    }
111
    return TSDB_CODE_SUCCESS;
95,552✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
185,654✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
94,508✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,200✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,200✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
92,308✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
92,308✔
121
    pVal->f = value->valuedouble;
92,308✔
122
    return TSDB_CODE_SUCCESS;
92,308✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
91,146✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
91,146✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
91,146✔
128
    pVal->d = value->valuedouble;
91,146✔
129
    return TSDB_CODE_SUCCESS;
91,146✔
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) {
404,386✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
404,386✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
294,408✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
109,978✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
109,978✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
94,028✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
15,950✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
15,950✔
147
  }
148
  pVal->length = strlen(value->valuestring);
388,436✔
149

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

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

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

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

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

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

180
  switch (value->type) {
1,185,276✔
181
    case cJSON_True:
382,108✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
382,108✔
184
      if (ret != TSDB_CODE_SUCCESS) {
382,108✔
185
        return ret;
×
186
      }
187
      break;
382,108✔
188
    }
189
    case cJSON_Number: {
573,393✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
573,393✔
191
      if (ret != TSDB_CODE_SUCCESS) {
573,393✔
192
        return ret;
8,800✔
193
      }
194
      break;
564,593✔
195
    }
196
    case cJSON_String: {
229,775✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
229,775✔
198
      if (ret != TSDB_CODE_SUCCESS) {
229,775✔
199
        return ret;
15,950✔
200
      }
201
      break;
213,825✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,160,526✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
2,010,184✔
211
  switch (root->type) {
2,010,184✔
212
    case cJSON_True:
258,636✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
258,636✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
258,636✔
216
      kv->i = root->valueint;
258,636✔
217
      break;
258,636✔
218
    }
219
    case cJSON_Number: {
390,573✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
390,573✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
390,573✔
222
      kv->d = root->valuedouble;
390,573✔
223
      break;
390,573✔
224
    }
225
    case cJSON_String: {
174,611✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
174,611✔
227
      if (ret != TSDB_CODE_SUCCESS) {
174,611✔
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
174,611✔
232
    }
233
    case cJSON_Object: {
1,185,276✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
1,185,276✔
235
      if (ret != TSDB_CODE_SUCCESS) {
1,185,276✔
236
        uError("SML:Failed to parse value from JSON Obj");
24,750✔
237
        return ret;
24,750✔
238
      }
239
      break;
1,160,526✔
240
    }
241
    default:
1,100✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,100✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
1,984,346✔
246
}
247

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

253
  int32_t tagNum = cJSON_GetArraySize(tags);
177,049✔
254
  if (unlikely(tagNum == 0)) {
177,049✔
255
    uError("SML:Tag should not be empty");
1,100✔
256
    return TSDB_CODE_TSC_INVALID_JSON;
1,100✔
257
  }
258
  for (int32_t i = 0; i < tagNum; ++i) {
1,773,059✔
259
    cJSON *tag = cJSON_GetArrayItem(tags, i);
1,612,461✔
260
    if (unlikely(tag == NULL)) {
1,612,473✔
261
      return TSDB_CODE_TSC_INVALID_JSON;
×
262
    }
263
    size_t keyLen = strlen(tag->string);
1,612,473✔
264
    if (unlikely(IS_INVALID_COL_LEN(keyLen))) {
1,612,473✔
265
      uError("SML:Tag key length is 0 or too large than 64");
550✔
266
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
550✔
267
    }
268

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

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
1,611,923✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,611,911✔
277
      return ret;
11,000✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
1,600,911✔
280
      return terrno;
×
281
    }
282

283
    if (info->dataFormat && !isSmlTagAligned(info, cnt, &kv)) {
1,600,911✔
284
      return TSDB_CODE_TSC_INVALID_JSON;
3,801✔
285
    }
286

287
    cnt++;
1,597,110✔
288
  }
289
  return TSDB_CODE_SUCCESS;
160,598✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
383,412✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
383,412✔
294
    elements->measureTag = info->preLine.measureTag;
53,262✔
295
    return TSDB_CODE_SUCCESS;
53,262✔
296
  }
297
  int32_t code = 0;
330,150✔
298
  int32_t lino = 0;
330,150✔
299
  if(info->dataFormat){
330,150✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
170,408✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
177,049✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
160,598✔
304
  return smlProcessChildTable(info, elements);
160,586✔
305

306
END:
169,552✔
307
  if(info->reRun){
169,552✔
308
    return TSDB_CODE_SUCCESS;
155,186✔
309
  }
310
  RETURN
14,366✔
311
}
312

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

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

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

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

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

342
  if (timeDouble < 0) {
124,143✔
343
    return timeDouble;
×
344
  }
345

346
  int64_t tsInt64 = timeDouble;
124,143✔
347
  size_t  typeLen = strlen(type->valuestring);
124,143✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
124,143✔
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
1,652✔
351
      return tsInt64 * smlFactorS[toPrecision];
1,652✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
122,491✔
355
    switch (type->valuestring[0]) {
122,491✔
356
      case 'm':
19,641✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
19,641✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
102,850✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
102,850✔
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) {
83,099✔
376
  uint8_t len = 0;
83,099✔
377
  while ((num /= 10) != 0) {
971,102✔
378
    len++;
888,003✔
379
  }
380
  len++;
83,099✔
381
  return len;
83,099✔
382
}
383

384
static int64_t smlParseTSFromJSON(SSmlHandle *info, cJSON *timestamp) {
213,848✔
385
  // Timestamp must be the first KV to parse
386
  int32_t toPrecision = info->currSTableMeta ? info->currSTableMeta->tableInfo.precision : TSDB_TIME_PRECISION_NANO;
213,848✔
387
  if (cJSON_IsNumber(timestamp)) {
213,848✔
388
    // timestamp value 0 indicates current system time
389
    double timeDouble = timestamp->valuedouble;
86,417✔
390
    if (unlikely(smlDoubleToInt64OverFlow(timeDouble))) {
86,417✔
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)) {
86,417✔
396
      smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is negative", NULL);
×
397
      return timeDouble;
×
398
    } else if (unlikely(timeDouble == 0)) {
86,417✔
399
      return taosGetTimestampNs() / smlFactorNS[toPrecision];
6,612✔
400
    }
401

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
83,111✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
83,123✔
405
    if (unlikely(fromPrecision == -1)) {
83,111✔
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 = timeDouble;
83,111✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
83,111✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
36,355✔
413
        return tsInt64 * smlFactorS[toPrecision];
36,355✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
46,756✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
127,443✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
125,793✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,650✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
1,650✔
424
  }
425
}
426

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

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

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

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

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
383,423✔
453
  SML_CHECK_NULL(elements->tags);
383,424✔
454

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

458
  if (unlikely(info->reRun)) {
369,022✔
459
    goto END;
155,186✔
460
  }
461

462
  // Parse timestamp
463
  // notice!!! put ts back to tag to ensure get meta->precision
464
  int64_t ts = smlParseTSFromJSON(info, tsJson);
213,836✔
465
  if (unlikely(ts < 0)) {
213,848✔
466
    char* tmp = cJSON_PrintUnformatted(tsJson);
3,300✔
467
    if (tmp == NULL) {
3,300✔
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,300✔
471
      taosMemoryFree(tmp);
3,300✔
472
    }
473
    SML_CHECK_CODE(TSDB_CODE_INVALID_TIMESTAMP);
3,300✔
474
  }
475
  SSmlKv kvTs = {0};
210,548✔
476
  smlBuildTsKv(&kvTs, ts);
210,548✔
477
  if (info->dataFormat){
210,548✔
478
    code = smlParseEndTelnetJsonFormat(info, elements, &kvTs, &kv);
53,000✔
479
  } else {
480
    code = smlParseEndTelnetJsonUnFormat(info, elements, &kvTs, &kv);
157,548✔
481
  }
482
  SML_CHECK_CODE(code);
210,560✔
483
  taosMemoryFreeClear(info->preLine.tags);
210,560✔
484
  info->preLine = *elements;
210,560✔
485
  elements->tags = NULL;
210,560✔
486

487
END:
399,362✔
488
  taosMemoryFree(elements->tags);
399,362✔
489
  RETURN
399,362✔
490
}
491

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

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

502
  // multiple data points must be sent in JSON array
503
  if (cJSON_IsArray(info->root)) {
186,796✔
504
    payloadNum = cJSON_GetArraySize(info->root);
23,417✔
505
  } else if (cJSON_IsObject(info->root)) {
163,379✔
506
    payloadNum = 1;
163,379✔
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;
186,796✔
513
  cJSON *head = (payloadNum == 1 && cJSON_IsObject(info->root)) ? info->root : info->root->child;
186,796✔
514

515
  int    cnt = 0;
186,796✔
516
  cJSON *dataPoint = head;
186,796✔
517
  while (dataPoint) {
552,542✔
518
    if (info->dataFormat) {
401,562✔
519
      SSmlLineInfo element = {0};
228,602✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
228,602✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
228,614✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
172,960✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
401,562✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
35,816✔
527
      return ret;
35,816✔
528
    }
529

530
    if (unlikely(info->reRun)) {
365,746✔
531
      cnt = 0;
155,186✔
532
      dataPoint = head;
155,186✔
533
      info->lineNum = payloadNum;
155,186✔
534
      ret = smlClearForRerun(info);
155,186✔
535
      if (ret != TSDB_CODE_SUCCESS) {
155,186✔
536
        return ret;
×
537
      }
538
      continue;
155,186✔
539
    }
540
    cnt++;
210,560✔
541
    dataPoint = dataPoint->next;
210,560✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
150,980✔
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