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

taosdata / TDengine / #4971

28 Feb 2026 08:05AM UTC coverage: 67.671% (-0.04%) from 67.707%
#4971

push

travis-ci

web-flow
fix(planner): disable project block merge in non-top-level subplans (#34617)

208281 of 307783 relevant lines covered (67.67%)

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

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
412,941✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
2,064,765✔
38
    cJSON *child = root->child;
1,651,824✔
39
    while (child != NULL) {
4,129,482✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
4,129,422✔
41
        *marks[i] = child;
1,651,764✔
42
        break;
1,651,764✔
43
      }
44
      child = child->next;
2,477,658✔
45
    }
46
    if (*marks[i] == NULL) {
1,651,824✔
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;
412,941✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
397,244✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
595,446✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
595,446✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
103,796✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,288✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,288✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
101,520✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
101,520✔
75
    pVal->i = value->valuedouble;
101,520✔
76
    return TSDB_CODE_SUCCESS;
101,520✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
491,650✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
100,376✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,288✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,288✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
98,088✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
98,088✔
86
    pVal->i = value->valuedouble;
98,088✔
87
    return TSDB_CODE_SUCCESS;
98,088✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
391,274✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
99,280✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,300✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,288✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
96,980✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
96,980✔
97
    pVal->i = value->valuedouble;
96,980✔
98
    return TSDB_CODE_SUCCESS;
96,980✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
291,994✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
99,178✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
99,178✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
99,178✔
105
      pVal->i = INT64_MAX;
90,948✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
8,230✔
107
      pVal->i = INT64_MIN;
1,144✔
108
    } else {
109
      pVal->i = value->valuedouble;
7,086✔
110
    }
111
    return TSDB_CODE_SUCCESS;
99,178✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
192,816✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
98,160✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,288✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,288✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
95,872✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
95,872✔
121
    pVal->f = value->valuedouble;
95,872✔
122
    return TSDB_CODE_SUCCESS;
95,872✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
94,656✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
94,656✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
94,656✔
128
    pVal->d = value->valuedouble;
94,656✔
129
    return TSDB_CODE_SUCCESS;
94,656✔
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) {
419,193✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
419,193✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
304,957✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
114,236✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
114,236✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
97,648✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
16,588✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
16,588✔
147
  }
148
  pVal->length = strlen(value->valuestring);
402,605✔
149

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

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

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

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

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

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

180
  switch (value->type) {
1,231,380✔
181
    case cJSON_True:
397,244✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
397,244✔
184
      if (ret != TSDB_CODE_SUCCESS) {
397,232✔
185
        return ret;
×
186
      }
187
      break;
397,232✔
188
    }
189
    case cJSON_Number: {
595,470✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
595,470✔
191
      if (ret != TSDB_CODE_SUCCESS) {
595,470✔
192
        return ret;
9,152✔
193
      }
194
      break;
586,318✔
195
    }
196
    case cJSON_String: {
238,666✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
238,666✔
198
      if (ret != TSDB_CODE_SUCCESS) {
238,666✔
199
        return ret;
16,588✔
200
      }
201
      break;
222,078✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,205,628✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
2,085,191✔
211
  switch (root->type) {
2,085,191✔
212
    case cJSON_True:
268,970✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
268,970✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
268,970✔
216
      kv->i = root->valueint;
268,970✔
217
      break;
268,970✔
218
    }
219
    case cJSON_Number: {
403,194✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
403,194✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
403,194✔
222
      kv->d = root->valuedouble;
403,194✔
223
      break;
403,194✔
224
    }
225
    case cJSON_String: {
180,539✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
180,539✔
227
      if (ret != TSDB_CODE_SUCCESS) {
180,527✔
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
180,539✔
232
    }
233
    case cJSON_Object: {
1,231,332✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
1,231,332✔
235
      if (ret != TSDB_CODE_SUCCESS) {
1,231,368✔
236
        uError("SML:Failed to parse value from JSON Obj");
25,740✔
237
        return ret;
25,740✔
238
      }
239
      break;
1,205,652✔
240
    }
241
    default:
1,156✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,156✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
2,058,355✔
246
}
247

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

253
  int32_t tagNum = cJSON_GetArraySize(tags);
183,648✔
254
  if (unlikely(tagNum == 0)) {
183,660✔
255
    uError("SML:Tag should not be empty");
1,144✔
256
    return TSDB_CODE_TSC_INVALID_JSON;
1,144✔
257
  }
258
  for (int32_t i = 0; i < tagNum; ++i) {
1,840,551✔
259
    cJSON *tag = cJSON_GetArrayItem(tags, i);
1,673,978✔
260
    if (unlikely(tag == NULL)) {
1,673,990✔
261
      return TSDB_CODE_TSC_INVALID_JSON;
×
262
    }
263
    size_t keyLen = strlen(tag->string);
1,673,990✔
264
    if (unlikely(IS_INVALID_COL_LEN(keyLen))) {
1,673,990✔
265
      uError("SML:Tag key length is 0 or too large than 64");
572✔
266
      return TSDB_CODE_TSC_INVALID_COLUMN_LENGTH;
572✔
267
    }
268

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

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
1,673,418✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,673,418✔
277
      return ret;
11,440✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
1,661,954✔
280
      return terrno;
×
281
    }
282

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

287
    cnt++;
1,658,035✔
288
  }
289
  return TSDB_CODE_SUCCESS;
166,573✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
396,377✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
396,377✔
294
    elements->measureTag = info->preLine.measureTag;
53,570✔
295
    return TSDB_CODE_SUCCESS;
53,570✔
296
  }
297
  int32_t code = 0;
342,807✔
298
  int32_t lino = 0;
342,807✔
299
  if(info->dataFormat){
342,807✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
176,782✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
183,636✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
166,573✔
304
  return smlProcessChildTable(info, elements);
166,549✔
305

306
END:
176,246✔
307
  if(info->reRun){
176,246✔
308
    return TSDB_CODE_SUCCESS;
161,335✔
309
  }
310
  RETURN
14,911✔
311
}
312

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

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

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

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

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

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

346
  int64_t tsInt64 = timeDouble;
128,374✔
347
  size_t  typeLen = strlen(type->valuestring);
128,374✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
128,374✔
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
1,710✔
351
      return tsInt64 * smlFactorS[toPrecision];
1,710✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
126,664✔
355
    switch (type->valuestring[0]) {
126,664✔
356
      case 'm':
19,700✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
19,700✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
106,964✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
106,964✔
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) {
84,899✔
376
  uint8_t len = 0;
84,899✔
377
  while ((num /= 10) != 0) {
990,548✔
378
    len++;
905,649✔
379
  }
380
  len++;
84,899✔
381
  return len;
84,899✔
382
}
383

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

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
84,911✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
84,911✔
405
    if (unlikely(fromPrecision == -1)) {
84,911✔
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;
84,911✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
84,911✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
37,721✔
413
        return tsInt64 * smlFactorS[toPrecision];
37,721✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
47,190✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
131,818✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
130,078✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,716✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
1,716✔
424
  }
425
}
426

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

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

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

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

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
396,389✔
453
  SML_CHECK_NULL(elements->tags);
396,377✔
454

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

458
  if (unlikely(info->reRun)) {
381,454✔
459
    goto END;
161,335✔
460
  }
461

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

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

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

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

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

515
  int    cnt = 0;
193,890✔
516
  cJSON *dataPoint = head;
193,890✔
517
  while (dataPoint) {
571,912✔
518
    if (info->dataFormat) {
415,205✔
519
      SSmlLineInfo element = {0};
235,122✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
235,122✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
235,146✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
180,083✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
415,241✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
37,219✔
527
      return ret;
37,219✔
528
    }
529

530
    if (unlikely(info->reRun)) {
378,022✔
531
      cnt = 0;
161,335✔
532
      dataPoint = head;
161,335✔
533
      info->lineNum = payloadNum;
161,335✔
534
      ret = smlClearForRerun(info);
161,335✔
535
      if (ret != TSDB_CODE_SUCCESS) {
161,335✔
536
        return ret;
×
537
      }
538
      continue;
161,335✔
539
    }
540
    cnt++;
216,687✔
541
    dataPoint = dataPoint->next;
216,687✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
156,707✔
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