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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

74.69
/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) {
1,704,650✔
25
  elements->measureLen = strlen(metric->valuestring);
1,704,650!
26
  if (IS_INVALID_TABLE_LEN(elements->measureLen)) {
1,703,880!
27
    uError("SML:0x%" PRIx64 " Metric length is 0 or large than 192", info->id);
1,871!
28
    return TSDB_CODE_TSC_INVALID_TABLE_ID_LENGTH;
1,486✔
29
  }
30

31
  elements->measure = metric->valuestring;
1,702,779✔
32
  return TSDB_CODE_SUCCESS;
1,703,164✔
33
}
34

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
1,704,392✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
8,521,833✔
38
    cJSON *child = root->child;
6,817,056✔
39
    while (child != NULL) {
17,041,661✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
17,034,995!
41
        *marks[i] = child;
6,817,314✔
42
        break;
6,818,084✔
43
      }
44
      child = child->next;
10,224,605✔
45
    }
46
    if (*marks[i] == NULL) {
6,824,750!
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;
1,704,777✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
629,542✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
1,451,193✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
1,451,193!
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
248,383✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,972!
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,972✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
245,411✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
245,411✔
75
    pVal->i = value->valuedouble;
245,411!
76
    return TSDB_CODE_SUCCESS;
245,411✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
1,202,810!
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
243,925✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,972!
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,972✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
240,953✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
240,953✔
86
    pVal->i = value->valuedouble;
240,953!
87
    return TSDB_CODE_SUCCESS;
240,953✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
958,885!
91
    if (!IS_VALID_INT(value->valuedouble)) {
242,525✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,972!
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,972✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
239,553✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
239,553✔
97
    pVal->i = value->valuedouble;
239,553!
98
    return TSDB_CODE_SUCCESS;
239,553✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
716,360!
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
239,508✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
239,508✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
239,508✔
105
      pVal->i = INT64_MAX;
118,137✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
121,371✔
107
      pVal->i = INT64_MIN;
1,486✔
108
    } else {
109
      pVal->i = value->valuedouble;
119,885!
110
    }
111
    return TSDB_CODE_SUCCESS;
239,508✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
476,852!
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
241,125✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,972!
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,972✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
238,153✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
238,153✔
121
    pVal->f = value->valuedouble;
238,153✔
122
    return TSDB_CODE_SUCCESS;
238,153✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
235,727!
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
235,727✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
235,727✔
128
    pVal->d = value->valuedouble;
235,727✔
129
    return TSDB_CODE_SUCCESS;
235,727✔
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) {
1,074,715✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
1,074,715✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
812,012✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
262,703!
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
262,703✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
241,156✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
21,547!
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
21,547✔
147
  }
148
  pVal->length = strlen(value->valuestring);
1,053,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) {
1,053,168!
151
    return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
×
152
  }
153
  if (pVal->type == TSDB_DATA_TYPE_NCHAR &&
1,053,168✔
154
      pVal->length > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
241,156!
155
    return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
×
156
  }
157

158
  pVal->value = value->valuestring;
1,053,168✔
159
  return TSDB_CODE_SUCCESS;
1,052,783✔
160
}
161

162
static int32_t smlParseValueFromJSONObj(cJSON *root, SSmlKv *kv) {
2,617,294✔
163
  int32_t ret = TSDB_CODE_SUCCESS;
2,617,294✔
164
  int32_t size = cJSON_GetArraySize(root);
2,617,294✔
165

166
  if (size != OTD_JSON_SUB_FIELDS_NUM) {
2,617,294!
167
    return TSDB_CODE_TSC_INVALID_JSON;
×
168
  }
169

170
  cJSON *value = cJSON_GetObjectItem(root, "value");
2,617,294✔
171
  if (value == NULL) {
2,617,294!
172
    return TSDB_CODE_TSC_INVALID_JSON;
×
173
  }
174

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

180
  switch (value->type) {
2,617,294!
181
    case cJSON_True:
629,542✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
629,542✔
184
      if (ret != TSDB_CODE_SUCCESS) {
629,542!
185
        return ret;
×
186
      }
187
      break;
629,542✔
188
    }
189
    case cJSON_Number: {
1,451,193✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
1,451,193✔
191
      if (ret != TSDB_CODE_SUCCESS) {
1,451,193✔
192
        return ret;
11,888✔
193
      }
194
      break;
1,439,305✔
195
    }
196
    case cJSON_String: {
536,559✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
536,559✔
198
      if (ret != TSDB_CODE_SUCCESS) {
536,559✔
199
        return ret;
21,547✔
200
      }
201
      break;
515,012✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
2,583,859✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
4,955,557✔
211
  switch (root->type) {
4,955,557✔
212
    case cJSON_True:
402,148✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
402,148✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
402,148✔
216
      kv->i = root->valueint;
402,148✔
217
      break;
402,148✔
218
    }
219
    case cJSON_Number: {
1,396,473✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
1,396,473✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
1,396,088✔
222
      kv->d = root->valuedouble;
1,396,088✔
223
      break;
1,396,473✔
224
    }
225
    case cJSON_String: {
538,156✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
538,156✔
227
      if (ret != TSDB_CODE_SUCCESS) {
537,771!
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
537,771✔
232
    }
233
    case cJSON_Object: {
2,617,294✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
2,617,294✔
235
      if (ret != TSDB_CODE_SUCCESS) {
2,617,294✔
236
        uError("SML:Failed to parse value from JSON Obj");
33,435!
237
        return ret;
33,435✔
238
      }
239
      break;
2,583,859✔
240
    }
241
    default:
1,486✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,486✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
4,920,251✔
246
}
247

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

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

269
    // add kv to SSmlKv
270
    SSmlKv kv = {0};
3,252,307✔
271
    kv.key = tag->string;
3,252,307✔
272
    kv.keyLen = keyLen;
3,251,922✔
273

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
3,251,922✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
3,252,008✔
277
      return ret;
14,860✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
3,237,619!
280
      return terrno;
×
281
    }
282

283
    if (info->dataFormat && !isSmlTagAligned(info, cnt, &kv)) {
3,237,619✔
284
      return TSDB_CODE_TSC_INVALID_JSON;
2,154✔
285
    }
286

287
    cnt++;
3,235,465✔
288
  }
289
  return TSDB_CODE_SUCCESS;
320,028✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
1,683,103✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
1,683,103✔
294
    elements->measureTag = info->preLine.measureTag;
1,118,588✔
295
    return TSDB_CODE_SUCCESS;
1,118,588✔
296
  }
297
  int32_t code = 0;
564,515✔
298
  int32_t lino = 0;
564,515✔
299
  if(info->dataFormat){
564,515✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
335,310✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
338,972✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
319,729!
304
  return smlProcessChildTable(info, elements);
319,557✔
305

306
END:
244,786✔
307
  if(info->reRun){
244,786✔
308
    return TSDB_CODE_SUCCESS;
226,421✔
309
  }
310
  RETURN
18,365!
311
}
312

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

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

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

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

338
  if (timeDouble == 0) {
695,117!
339
    return taosGetTimestampNs() / smlFactorNS[toPrecision];
×
340
  }
341

342
  if (timeDouble < 0) {
695,117!
343
    return timeDouble;
×
344
  }
345

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

375
uint8_t smlGetTimestampLen(int64_t num) {
737,106✔
376
  uint8_t len = 0;
737,106✔
377
  while ((num /= 10) != 0) {
9,464,339✔
378
    len++;
8,727,233✔
379
  }
380
  len++;
737,106✔
381
  return len;
737,106✔
382
}
383

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

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
737,215!
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
737,215✔
405
    if (unlikely(fromPrecision == -1)) {
737,129!
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;
737,129!
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
737,129✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
39,313!
413
        return tsInt64 * smlFactorS[toPrecision];
39,313✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
697,816!
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
699,575✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
697,346✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
2,229✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
2,229✔
424
  }
425
}
426

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

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

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

443
  cJSON **marks[OTD_JSON_FIELDS_NUM] = {&metricJson, &tsJson, &valueJson, &tagsJson};
1,704,478✔
444
  SML_CHECK_CODE(smlGetJsonElements(root, marks));
1,704,478!
445
  // Parse metric
446
  SML_CHECK_CODE(smlParseMetricFromJSON(info, metricJson, elements));
1,704,650✔
447
  // Parse metric value
448
  SSmlKv kv = {.key = VALUE, .keyLen = VALUE_LEN};
1,703,164✔
449
  SML_CHECK_CODE(smlParseValueFromJSON(valueJson, &kv));
1,703,164✔
450

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
1,683,103✔
453
  SML_CHECK_NULL(elements->tags);
1,683,017!
454

455
  elements->tagsLen = strlen(elements->tags);
1,683,017!
456
  SML_CHECK_CODE(smlParseTagsFromJSON(info, tagsJson, elements));
1,683,017✔
457

458
  if (unlikely(info->reRun)) {
1,664,566✔
459
    goto END;
226,421✔
460
  }
461

462
  // Parse timestamp
463
  // notice!!! put ts back to tag to ensure get meta->precision
464
  int64_t ts = smlParseTSFromJSON(info, tsJson);
1,438,145✔
465
  if (unlikely(ts < 0)) {
1,438,231✔
466
    char* tmp = cJSON_PrintUnformatted(tsJson);
4,458✔
467
    if (tmp == NULL) {
4,458!
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);
4,458!
471
      taosMemoryFree(tmp);
4,458!
472
    }
473
    SML_CHECK_CODE(TSDB_CODE_INVALID_TIMESTAMP);
4,458!
474
  }
475
  SSmlKv kvTs = {0};
1,433,773✔
476
  smlBuildTsKv(&kvTs, ts);
1,433,773✔
477
  if (info->dataFormat){
1,433,687✔
478
    code = smlParseEndTelnetJsonFormat(info, elements, &kvTs, &kv);
957,284✔
479
  } else {
480
    code = smlParseEndTelnetJsonUnFormat(info, elements, &kvTs, &kv);
476,403✔
481
  }
482
  SML_CHECK_CODE(code);
1,433,687!
483
  taosMemoryFreeClear(info->preLine.tags);
1,433,687!
484
  info->preLine = *elements;
1,433,302✔
485
  elements->tags = NULL;
1,433,687✔
486

487
END:
1,704,093✔
488
  taosMemoryFree(elements->tags);
1,704,093!
489
  RETURN
1,704,650!
490
}
491

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

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

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

515
  int    cnt = 0;
355,102✔
516
  cJSON *dataPoint = head;
355,102✔
517
  while (dataPoint) {
2,014,911✔
518
    if (info->dataFormat) {
1,707,151✔
519
      SSmlLineInfo element = {0};
1,209,944✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
1,209,559✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
1,210,329!
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
497,207✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,707,536✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
47,342!
527
      return ret;
47,342✔
528
    }
529

530
    if (unlikely(info->reRun)) {
1,660,194✔
531
      cnt = 0;
226,421✔
532
      dataPoint = head;
226,421✔
533
      info->lineNum = payloadNum;
226,421✔
534
      ret = smlClearForRerun(info);
226,421✔
535
      if (ret != TSDB_CODE_SUCCESS) {
226,421!
536
        return ret;
×
537
      }
538
      continue;
226,421✔
539
    }
540
    cnt++;
1,433,773✔
541
    dataPoint = dataPoint->next;
1,433,773✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
307,760✔
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