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

taosdata / TDengine / #4423

04 Jul 2025 08:20AM UTC coverage: 63.643% (+2.6%) from 61.007%
#4423

push

travis-ci

GitHub
Merge pull request #31575 from taosdata/fix/huoh/taos_log

160637 of 321218 branches covered (50.01%)

Branch coverage included in aggregate %.

247567 of 320175 relevant lines covered (77.32%)

16621050.3 hits per line

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

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

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
5,293✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
26,422✔
38
    cJSON *child = root->child;
21,129✔
39
    while (child != NULL) {
52,817!
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
52,830✔
41
        *marks[i] = child;
21,142✔
42
        break;
21,142✔
43
      }
44
      child = child->next;
31,688✔
45
    }
46
    if (*marks[i] == NULL) {
21,129!
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;
5,293✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
996✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
2,868✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
2,868!
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
484✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
4!
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
4✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
480✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
480✔
75
    pVal->i = value->valuedouble;
480✔
76
    return TSDB_CODE_SUCCESS;
480✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
2,384!
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
478✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
4!
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
4✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
474✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
474✔
86
    pVal->i = value->valuedouble;
474✔
87
    return TSDB_CODE_SUCCESS;
474✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
1,906!
91
    if (!IS_VALID_INT(value->valuedouble)) {
477✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
4!
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
4✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
473✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
473✔
97
    pVal->i = value->valuedouble;
473✔
98
    return TSDB_CODE_SUCCESS;
473✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
1,429✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
485✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
485✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
485✔
105
      pVal->i = INT64_MAX;
159✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
326✔
107
      pVal->i = INT64_MIN;
2✔
108
    } else {
109
      pVal->i = value->valuedouble;
324✔
110
    }
111
    return TSDB_CODE_SUCCESS;
485✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
944!
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
476✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
4!
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
4✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
472✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
472✔
121
    pVal->f = value->valuedouble;
472✔
122
    return TSDB_CODE_SUCCESS;
472✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
468!
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
468✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
468✔
128
    pVal->d = value->valuedouble;
468✔
129
    return TSDB_CODE_SUCCESS;
468✔
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) {
2,941✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
2,941✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
2,428✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
513!
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
513✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
484✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
29!
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
29✔
147
  }
148
  pVal->length = strlen(value->valuestring);
2,912✔
149

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

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

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

166
  if (size != OTD_JSON_SUB_FIELDS_NUM) {
4,900!
167
    return TSDB_CODE_TSC_INVALID_JSON;
×
168
  }
169

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

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

180
  switch (value->type) {
4,901!
181
    case cJSON_True:
996✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
996✔
184
      if (ret != TSDB_CODE_SUCCESS) {
995!
185
        return ret;
×
186
      }
187
      break;
995✔
188
    }
189
    case cJSON_Number: {
2,868✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
2,868✔
191
      if (ret != TSDB_CODE_SUCCESS) {
2,865✔
192
        return ret;
16✔
193
      }
194
      break;
2,849✔
195
    }
196
    case cJSON_String: {
1,037✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
1,037✔
198
      if (ret != TSDB_CODE_SUCCESS) {
1,037✔
199
        return ret;
29✔
200
      }
201
      break;
1,008✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
4,852✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
13,056✔
211
  switch (root->type) {
13,056!
212
    case cJSON_True:
786✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
786✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
786✔
216
      kv->i = root->valueint;
786✔
217
      break;
786✔
218
    }
219
    case cJSON_Number: {
5,471✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
5,471✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
5,471✔
222
      kv->d = root->valuedouble;
5,471✔
223
      break;
5,471✔
224
    }
225
    case cJSON_String: {
1,904✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
1,904✔
227
      if (ret != TSDB_CODE_SUCCESS) {
1,902!
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
1,903✔
232
    }
233
    case cJSON_Object: {
4,900✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
4,900✔
235
      if (ret != TSDB_CODE_SUCCESS) {
4,894✔
236
        uError("SML:Failed to parse value from JSON Obj");
45!
237
        return ret;
45✔
238
      }
239
      break;
4,849✔
240
    }
241
    default:
×
242
      return TSDB_CODE_TSC_INVALID_JSON;
×
243
  }
244

245
  return TSDB_CODE_SUCCESS;
13,009✔
246
}
247

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

253
  int32_t tagNum = cJSON_GetArraySize(tags);
885✔
254
  if (unlikely(tagNum == 0)) {
885✔
255
    uError("SML:Tag should not be empty");
2!
256
    return TSDB_CODE_TSC_INVALID_JSON;
2✔
257
  }
258
  for (int32_t i = 0; i < tagNum; ++i) {
8,624✔
259
    cJSON *tag = cJSON_GetArrayItem(tags, i);
7,774✔
260
    if (unlikely(tag == NULL)) {
7,777!
261
      return TSDB_CODE_TSC_INVALID_JSON;
37✔
262
    }
263
    size_t keyLen = strlen(tag->string);
7,777✔
264
    if (unlikely(IS_INVALID_COL_LEN(keyLen))) {
7,777!
265
      uError("SML:Tag key length is 0 or too large than 64");
1!
266
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
1✔
267
    }
268

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

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
7,776✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
7,774✔
277
      return ret;
20✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
7,758!
280
      return terrno;
×
281
    }
282

283
    if (info->dataFormat && !isSmlTagAligned(info, cnt, &kv)) {
7,758✔
284
      return TSDB_CODE_TSC_INVALID_JSON;
16✔
285
    }
286

287
    cnt++;
7,741✔
288
  }
289
  return TSDB_CODE_SUCCESS;
850✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
5,266✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
5,266✔
294
    elements->measureTag = info->preLine.measureTag;
3,972✔
295
    return TSDB_CODE_SUCCESS;
3,972✔
296
  }
297
  int32_t code = 0;
1,296✔
298
  int32_t lino = 0;
1,296✔
299
  if(info->dataFormat){
1,296✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
850✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
883✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
849!
304
  return smlProcessChildTable(info, elements);
845✔
305

306
END:
448✔
307
  if(info->reRun){
448✔
308
    return TSDB_CODE_SUCCESS;
419✔
309
  }
310
  RETURN
29!
311
}
312

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

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

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

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

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

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

346
  int64_t tsInt64 = timeDouble;
1,654✔
347
  size_t  typeLen = strlen(type->valuestring);
1,654✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
1,654!
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
6!
351
      return tsInt64 * smlFactorS[toPrecision];
6✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
1,648!
355
    switch (type->valuestring[0]) {
1,648!
356
      case 'm':
1,461✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
1,461✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
187✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
187✔
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) {
3,145✔
376
  uint8_t len = 0;
3,145✔
377
  while ((num /= 10) != 0) {
40,508✔
378
    len++;
37,363✔
379
  }
380
  len++;
3,145✔
381
  return len;
3,145✔
382
}
383

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

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
3,142✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
3,144✔
405
    if (unlikely(fromPrecision == -1)) {
3,141!
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;
3,141✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
3,141✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
96!
413
        return tsInt64 * smlFactorS[toPrecision];
96✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
3,045✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
1,657✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
1,655✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
3✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
3✔
424
  }
425
}
426

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

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

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

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

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
5,257✔
453
  SML_CHECK_NULL(elements->tags);
5,266!
454

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

458
  if (unlikely(info->reRun)) {
5,232✔
459
    goto END;
419✔
460
  }
461

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

487
END:
5,290✔
488
  taosMemoryFree(elements->tags);
5,290!
489
  RETURN
5,292!
490
}
491

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

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

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

515
  int    cnt = 0;
828✔
516
  cJSON *dataPoint = head;
828✔
517
  while (dataPoint) {
6,057✔
518
    if (info->dataFormat) {
5,294✔
519
      SSmlLineInfo element = {0};
3,850✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
3,850✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
3,851!
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
1,444✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
5,297✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
68!
527
      return ret;
68✔
528
    }
529

530
    if (unlikely(info->reRun)) {
5,229✔
531
      cnt = 0;
419✔
532
      dataPoint = head;
419✔
533
      info->lineNum = payloadNum;
419✔
534
      ret = smlClearForRerun(info);
419✔
535
      if (ret != TSDB_CODE_SUCCESS) {
419!
536
        return ret;
×
537
      }
538
      continue;
419✔
539
    }
540
    cnt++;
4,810✔
541
    dataPoint = dataPoint->next;
4,810✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
763✔
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