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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

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

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
461,844✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
2,307,738✔
38
    cJSON *child = root->child;
1,845,894✔
39
    while (child != NULL) {
4,615,343✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
4,614,703✔
41
        *marks[i] = child;
1,845,905✔
42
        break;
1,846,556✔
43
      }
44
      child = child->next;
2,769,449✔
45
    }
46
    if (*marks[i] == NULL) {
1,847,196✔
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;
461,844✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
450,908✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
673,575✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
673,575✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
117,458✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,600✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,600✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
114,858✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
114,858✔
75
    pVal->i = value->valuedouble;
114,858✔
76
    return TSDB_CODE_SUCCESS;
114,858✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
556,117✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
113,558✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,600✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,600✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
110,958✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
110,958✔
86
    pVal->i = value->valuedouble;
110,958✔
87
    return TSDB_CODE_SUCCESS;
110,958✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
442,559✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
112,265✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,600✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,600✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
109,665✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
109,665✔
97
    pVal->i = value->valuedouble;
109,665✔
98
    return TSDB_CODE_SUCCESS;
109,665✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
330,294✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
112,264✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
112,264✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
112,264✔
105
      pVal->i = INT64_MAX;
103,350✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
8,914✔
107
      pVal->i = INT64_MIN;
1,300✔
108
    } else {
109
      pVal->i = value->valuedouble;
7,614✔
110
    }
111
    return TSDB_CODE_SUCCESS;
112,264✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
218,030✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
110,972✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,600✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,600✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
108,372✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
108,372✔
121
    pVal->f = value->valuedouble;
108,372✔
122
    return TSDB_CODE_SUCCESS;
108,372✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
107,058✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
107,058✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
107,058✔
128
    pVal->d = value->valuedouble;
107,058✔
129
    return TSDB_CODE_SUCCESS;
107,058✔
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) {
472,977✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
472,977✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
343,687✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
129,290✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
129,290✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
110,440✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
18,850✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
18,850✔
147
  }
148
  pVal->length = strlen(value->valuestring);
454,127✔
149

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

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

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

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

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

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

180
  switch (value->type) {
1,394,640✔
181
    case cJSON_True:
450,908✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
450,908✔
184
      if (ret != TSDB_CODE_SUCCESS) {
450,908✔
185
        return ret;
×
186
      }
187
      break;
450,908✔
188
    }
189
    case cJSON_Number: {
673,575✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
673,575✔
191
      if (ret != TSDB_CODE_SUCCESS) {
673,575✔
192
        return ret;
10,400✔
193
      }
194
      break;
663,175✔
195
    }
196
    case cJSON_String: {
270,157✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
270,157✔
198
      if (ret != TSDB_CODE_SUCCESS) {
270,157✔
199
        return ret;
18,850✔
200
      }
201
      break;
251,307✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,365,390✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
2,354,326✔
211
  switch (root->type) {
2,354,326✔
212
    case cJSON_True:
304,916✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
304,916✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
304,916✔
216
      kv->i = root->valueint;
304,916✔
217
      break;
304,916✔
218
    }
219
    case cJSON_Number: {
450,686✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
450,686✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
450,686✔
222
      kv->d = root->valuedouble;
450,686✔
223
      break;
450,686✔
224
    }
225
    case cJSON_String: {
202,808✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
202,808✔
227
      if (ret != TSDB_CODE_SUCCESS) {
202,820✔
UNCOV
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
202,820✔
232
    }
233
    case cJSON_Object: {
1,394,616✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
1,394,616✔
235
      if (ret != TSDB_CODE_SUCCESS) {
1,394,640✔
236
        uError("SML:Failed to parse value from JSON Obj");
29,250✔
237
        return ret;
29,250✔
238
      }
239
      break;
1,365,390✔
240
    }
241
    default:
1,300✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,300✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
2,323,812✔
246
}
247

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

253
  int32_t tagNum = cJSON_GetArraySize(tags);
207,953✔
254
  if (unlikely(tagNum == 0)) {
207,953✔
255
    uError("SML:Tag should not be empty");
1,300✔
256
    return TSDB_CODE_TSC_INVALID_JSON;
1,300✔
257
  }
258
  for (int32_t i = 0; i < tagNum; ++i) {
2,082,956✔
259
    cJSON *tag = cJSON_GetArrayItem(tags, i);
1,894,456✔
260
    if (unlikely(tag == NULL)) {
1,894,480✔
261
      return TSDB_CODE_TSC_INVALID_JSON;
×
262
    }
263
    size_t keyLen = strlen(tag->string);
1,894,480✔
264
    if (unlikely(IS_INVALID_COL_LEN(keyLen))) {
1,894,480✔
265
      uError("SML:Tag key length is 0 or too large than 64");
650✔
266
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
650✔
267
    }
268

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

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
1,893,830✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,893,830✔
277
      return ret;
13,000✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
1,880,806✔
280
      return terrno;
×
281
    }
282

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

287
    cnt++;
1,876,303✔
288
  }
289
  return TSDB_CODE_SUCCESS;
188,500✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
442,994✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
442,994✔
294
    elements->measureTag = info->preLine.measureTag;
54,278✔
295
    return TSDB_CODE_SUCCESS;
54,278✔
296
  }
297
  int32_t code = 0;
388,704✔
298
  int32_t lino = 0;
388,704✔
299
  if(info->dataFormat){
388,704✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
200,100✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
207,953✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
188,500✔
304
  return smlProcessChildTable(info, elements);
188,500✔
305

306
END:
200,216✔
307
  if(info->reRun){
200,216✔
308
    return TSDB_CODE_SUCCESS;
183,254✔
309
  }
310
  RETURN
16,962✔
311
}
312

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

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

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

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

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

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

346
  int64_t tsInt64 = (int64_t)timeDouble;
143,513✔
347
  size_t  typeLen = strlen(type->valuestring);
143,513✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
143,513✔
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
1,952✔
351
      return tsInt64 * smlFactorS[toPrecision];
1,952✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
141,561✔
355
    switch (type->valuestring[0]) {
141,561✔
356
      case 'm':
20,011✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
20,011✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
121,550✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
121,550✔
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) {
92,845✔
376
  uint8_t len = 0;
92,845✔
377
  while ((num /= 10) != 0) {
1,062,415✔
378
    len++;
969,570✔
379
  }
380
  len++;
92,845✔
381
  return len;
92,845✔
382
}
383

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

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
91,447✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
91,447✔
405
    if (unlikely(fromPrecision == -1)) {
91,447✔
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;
91,447✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
91,447✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
42,942✔
413
        return tsInt64 * smlFactorS[toPrecision];
42,942✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
48,505✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
147,413✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
145,463✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,950✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
1,950✔
424
  }
425
}
426

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

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

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

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

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
442,982✔
453
  SML_CHECK_NULL(elements->tags);
442,994✔
454

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

458
  if (unlikely(info->reRun)) {
426,032✔
459
    goto END;
183,254✔
460
  }
461

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

487
END:
461,796✔
488
  taosMemoryFree(elements->tags);
461,796✔
489
  RETURN
461,820✔
490
}
491

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

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

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

515
  int    cnt = 0;
219,609✔
516
  cJSON *dataPoint = head;
219,609✔
517
  while (dataPoint) {
641,704✔
518
    if (info->dataFormat) {
464,395✔
519
      SSmlLineInfo element = {0};
260,681✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
260,681✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
260,705✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
203,714✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
464,407✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
42,312✔
527
      return ret;
42,312✔
528
    }
529

530
    if (unlikely(info->reRun)) {
422,095✔
531
      cnt = 0;
183,254✔
532
      dataPoint = head;
183,254✔
533
      info->lineNum = payloadNum;
183,254✔
534
      ret = smlClearForRerun(info);
183,254✔
535
      if (ret != TSDB_CODE_SUCCESS) {
183,254✔
536
        return ret;
×
537
      }
538
      continue;
183,254✔
539
    }
540
    cnt++;
238,841✔
541
    dataPoint = dataPoint->next;
238,841✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
177,309✔
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