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

taosdata / TDengine / #4864

26 Nov 2025 05:46AM UTC coverage: 64.548% (+0.009%) from 64.539%
#4864

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

769 of 945 new or added lines in 33 files covered. (81.38%)

3006 existing lines in 116 files now uncovered.

158227 of 245129 relevant lines covered (64.55%)

111826500.07 hits per line

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

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

31
  elements->measure = metric->valuestring;
1,469,952✔
32
  return TSDB_CODE_SUCCESS;
1,469,952✔
33
}
34

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
1,471,023✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
7,353,522✔
38
    cJSON *child = root->child;
5,882,499✔
39
    while (child != NULL) {
14,706,041✔
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
14,705,957✔
41
        *marks[i] = child;
5,882,415✔
42
        break;
5,882,415✔
43
      }
44
      child = child->next;
8,823,542✔
45
    }
46
    if (*marks[i] == NULL) {
5,882,499✔
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,471,023✔
52
}
53

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

63
  return TSDB_CODE_SUCCESS;
498,655✔
64
}
65

66
static int32_t smlConvertJSONNumber(SSmlKv *pVal, char *typeStr, cJSON *value) {
1,060,494✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
1,060,494✔
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
182,134✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
2,468✔
71
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,468✔
72
    }
73
    pVal->type = TSDB_DATA_TYPE_TINYINT;
179,666✔
74
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
179,666✔
75
    pVal->i = value->valuedouble;
179,666✔
76
    return TSDB_CODE_SUCCESS;
179,666✔
77
  }
78
  // smallint
79
  if (strcasecmp(typeStr, "i16") == 0 || strcasecmp(typeStr, "smallint") == 0) {
878,360✔
80
    if (!IS_VALID_SMALLINT(value->valuedouble)) {
178,350✔
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2,468✔
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,468✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
175,964✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
175,964✔
86
    pVal->i = value->valuedouble;
175,964✔
87
    return TSDB_CODE_SUCCESS;
175,964✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
700,010✔
91
    if (!IS_VALID_INT(value->valuedouble)) {
177,455✔
92
      uError("SML:JSON value(%f) cannot fit in type(int)", value->valuedouble);
2,468✔
93
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,468✔
94
    }
95
    pVal->type = TSDB_DATA_TYPE_INT;
174,987✔
96
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
174,987✔
97
    pVal->i = value->valuedouble;
174,987✔
98
    return TSDB_CODE_SUCCESS;
174,987✔
99
  }
100
  // bigint
101
  if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) {
522,555✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
174,289✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
174,289✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
174,289✔
105
      pVal->i = INT64_MAX;
98,103✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
76,186✔
107
      pVal->i = INT64_MIN;
1,234✔
108
    } else {
109
      pVal->i = value->valuedouble;
74,952✔
110
    }
111
    return TSDB_CODE_SUCCESS;
174,289✔
112
  }
113
  // float
114
  if (strcasecmp(typeStr, "f32") == 0 || strcasecmp(typeStr, "float") == 0) {
348,266✔
115
    if (!IS_VALID_FLOAT(value->valuedouble)) {
176,478✔
116
      uError("SML:JSON value(%f) cannot fit in type(float)", value->valuedouble);
2,468✔
117
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
2,468✔
118
    }
119
    pVal->type = TSDB_DATA_TYPE_FLOAT;
174,010✔
120
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
174,010✔
121
    pVal->f = value->valuedouble;
174,010✔
122
    return TSDB_CODE_SUCCESS;
174,010✔
123
  }
124
  // double
125
  if (strcasecmp(typeStr, "f64") == 0 || strcasecmp(typeStr, "double") == 0) {
171,788✔
126
    pVal->type = TSDB_DATA_TYPE_DOUBLE;
171,788✔
127
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
171,788✔
128
    pVal->d = value->valuedouble;
171,788✔
129
    return TSDB_CODE_SUCCESS;
171,788✔
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) {
933,380✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
933,380✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
737,458✔
140
  } else if (strcasecmp(typeStr, "varbinary") == 0) {
195,922✔
141
    pVal->type = TSDB_DATA_TYPE_VARBINARY;
×
142
  } else if (strcasecmp(typeStr, "nchar") == 0) {
195,922✔
143
    pVal->type = TSDB_DATA_TYPE_NCHAR;
177,976✔
144
  } else {
145
    uError("SML:invalid type(%s) for JSON String", typeStr);
17,946✔
146
    return TSDB_CODE_TSC_INVALID_JSON_TYPE;
17,893✔
147
  }
148
  pVal->length = strlen(value->valuestring);
915,434✔
149

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

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

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

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

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

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

180
  switch (value->type) {
1,958,464✔
181
    case cJSON_True:
498,655✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
498,655✔
184
      if (ret != TSDB_CODE_SUCCESS) {
498,655✔
185
        return ret;
×
186
      }
187
      break;
498,655✔
188
    }
189
    case cJSON_Number: {
1,060,494✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
1,060,494✔
191
      if (ret != TSDB_CODE_SUCCESS) {
1,060,494✔
192
        return ret;
9,872✔
193
      }
194
      break;
1,050,622✔
195
    }
196
    case cJSON_String: {
399,315✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
399,315✔
198
      if (ret != TSDB_CODE_SUCCESS) {
399,315✔
199
        return ret;
17,893✔
200
      }
201
      break;
381,422✔
202
    }
203
    default:
×
204
      return TSDB_CODE_TSC_INVALID_JSON_TYPE;
×
205
  }
206

207
  return TSDB_CODE_SUCCESS;
1,930,699✔
208
}
209

210
static int32_t smlParseValueFromJSON(cJSON *root, SSmlKv *kv) {
4,354,825✔
211
  switch (root->type) {
4,354,825✔
212
    case cJSON_True:
354,568✔
213
    case cJSON_False: {
214
      kv->type = TSDB_DATA_TYPE_BOOL;
354,568✔
215
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
354,568✔
216
      kv->i = root->valueint;
354,568✔
217
      break;
354,568✔
218
    }
219
    case cJSON_Number: {
1,506,576✔
220
      kv->type = TSDB_DATA_TYPE_DOUBLE;
1,506,576✔
221
      kv->length = (int16_t)tDataTypes[kv->type].bytes;
1,506,576✔
222
      kv->d = root->valuedouble;
1,506,576✔
223
      break;
1,506,576✔
224
    }
225
    case cJSON_String: {
534,065✔
226
      int32_t ret = smlConvertJSONString(kv, "binary", root);
534,065✔
227
      if (ret != TSDB_CODE_SUCCESS) {
534,170✔
UNCOV
228
        uError("SML:Failed to parse binary value from JSON Obj");
×
229
        return ret;
×
230
      }
231
      break;
534,170✔
232
    }
233
    case cJSON_Object: {
1,958,224✔
234
      int32_t ret = smlParseValueFromJSONObj(root, kv);
1,958,224✔
235
      if (ret != TSDB_CODE_SUCCESS) {
1,958,464✔
236
        uError("SML:Failed to parse value from JSON Obj");
27,765✔
237
        return ret;
27,765✔
238
      }
239
      break;
1,930,699✔
240
    }
241
    default:
1,392✔
242
      return TSDB_CODE_TSC_INVALID_JSON;
1,392✔
243
  }
244

245
  return TSDB_CODE_SUCCESS;
4,326,013✔
246
}
247

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

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

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

274
    // value
275
    int32_t ret = smlParseValueFromJSON(tag, &kv);
2,885,319✔
276
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
2,885,214✔
277
      return ret;
12,340✔
278
    }
279
    if (taosArrayPush(preLineKV, &kv) == NULL) {
2,872,581✔
280
      return terrno;
×
281
    }
282

283
    if (info->dataFormat && !isSmlTagAligned(info, cnt, &kv)) {
2,872,581✔
284
      return TSDB_CODE_TSC_INVALID_JSON;
1,463✔
285
    }
286

287
    cnt++;
2,870,983✔
288
  }
289
  return TSDB_CODE_SUCCESS;
291,877✔
290
}
291

292
static int32_t smlParseTagsFromJSON(SSmlHandle *info, cJSON *tags, SSmlLineInfo *elements) {
1,453,116✔
293
  if (is_same_child_table_telnet(elements, &info->preLine) == 0) {
1,453,116✔
294
    elements->measureTag = info->preLine.measureTag;
955,848✔
295
    return TSDB_CODE_SUCCESS;
955,848✔
296
  }
297
  int32_t code = 0;
497,426✔
298
  int32_t lino = 0;
497,426✔
299
  if(info->dataFormat){
497,426✔
300
    SML_CHECK_CODE(smlProcessSuperTable(info, elements));
302,621✔
301
  }
302
  SML_CHECK_CODE(smlProcessTagJson(info, tags));
307,373✔
303
  SML_CHECK_CODE(smlJoinMeasureTag(elements));
291,959✔
304
  return smlProcessChildTable(info, elements);
291,959✔
305

306
END:
205,789✔
307
  if(info->reRun){
205,789✔
308
    return TSDB_CODE_SUCCESS;
190,636✔
309
  }
310
  RETURN
15,153✔
311
}
312

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

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

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

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

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

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

346
  int64_t tsInt64 = timeDouble;
465,036✔
347
  size_t  typeLen = strlen(type->valuestring);
465,036✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
465,036✔
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
934✔
351
      return tsInt64 * smlFactorS[toPrecision];
934✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
464,102✔
355
    switch (type->valuestring[0]) {
464,260✔
356
      case 'm':
348,881✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
348,881✔
360
      case 'u':
×
361
      case 'U':
362
        // microseconds
363
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MICRO, toPrecision);
×
364
      case 'n':
115,379✔
365
      case 'N':
366
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_NANO, toPrecision);
115,379✔
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) {
777,632✔
376
  uint8_t len = 0;
777,632✔
377
  while ((num /= 10) != 0) {
10,011,729✔
378
    len++;
9,234,097✔
379
  }
380
  len++;
777,632✔
381
  return len;
777,632✔
382
}
383

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

402
    uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble);
777,796✔
403

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
777,714✔
405
    if (unlikely(fromPrecision == -1)) {
777,796✔
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;
777,796✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
777,796✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
32,523✔
413
        return tsInt64 * smlFactorS[toPrecision];
32,523✔
414
      }
415
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
416
    } else {
417
      return convertTimePrecision(timeDouble, fromPrecision, toPrecision);
745,273✔
418
    }
419
  } else if (cJSON_IsObject(timestamp)) {
468,738✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
466,887✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1,851✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
1,851✔
424
  }
425
}
426

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

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

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

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

451
  // Parse tags
452
  elements->tags = cJSON_PrintUnformatted(tagsJson);
1,453,116✔
453
  SML_CHECK_NULL(elements->tags);
1,453,198✔
454

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

458
  if (unlikely(info->reRun)) {
1,438,121✔
459
    goto END;
190,636✔
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,247,485✔
465
  if (unlikely(ts < 0)) {
1,247,327✔
466
    char* tmp = cJSON_PrintUnformatted(tsJson);
3,702✔
467
    if (tmp == NULL) {
3,702✔
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,702✔
471
      taosMemoryFree(tmp);
3,702✔
472
    }
473
    SML_CHECK_CODE(TSDB_CODE_INVALID_TIMESTAMP);
3,702✔
474
  }
475
  SSmlKv kvTs = {0};
1,243,625✔
476
  smlBuildTsKv(&kvTs, ts);
1,243,625✔
477
  if (info->dataFormat){
1,243,707✔
478
    code = smlParseEndTelnetJsonFormat(info, elements, &kvTs, &kv);
843,004✔
479
  } else {
480
    code = smlParseEndTelnetJsonUnFormat(info, elements, &kvTs, &kv);
400,703✔
481
  }
482
  SML_CHECK_CODE(code);
1,243,484✔
483
  taosMemoryFreeClear(info->preLine.tags);
1,243,484✔
484
  info->preLine = *elements;
1,243,812✔
485
  elements->tags = NULL;
1,243,812✔
486

487
END:
1,471,196✔
488
  taosMemoryFree(elements->tags);
1,471,196✔
489
  RETURN
1,471,120✔
490
}
491

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

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

502
  // multiple data points must be sent in JSON array
503
  if (cJSON_IsArray(info->root)) {
307,406✔
504
    payloadNum = cJSON_GetArraySize(info->root);
124,432✔
505
  } else if (cJSON_IsObject(info->root)) {
182,900✔
506
    payloadNum = 1;
182,900✔
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;
307,381✔
513
  cJSON *head = (payloadNum == 1 && cJSON_IsObject(info->root)) ? info->root : info->root->child;
307,381✔
514

515
  int    cnt = 0;
307,381✔
516
  cJSON *dataPoint = head;
307,381✔
517
  while (dataPoint) {
1,741,671✔
518
    if (info->dataFormat) {
1,473,563✔
519
      SSmlLineInfo element = {0};
1,055,502✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
1,055,502✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
1,055,445✔
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
418,061✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
1,473,506✔
526
      uError("SML:0x%" PRIx64 " Invalid JSON Payload 2:%s", info->id, payload);
39,216✔
527
      return ret;
39,216✔
528
    }
529

530
    if (unlikely(info->reRun)) {
1,434,290✔
531
      cnt = 0;
190,636✔
532
      dataPoint = head;
190,636✔
533
      info->lineNum = payloadNum;
190,636✔
534
      ret = smlClearForRerun(info);
190,636✔
535
      if (ret != TSDB_CODE_SUCCESS) {
190,636✔
536
        return ret;
×
537
      }
538
      continue;
190,636✔
539
    }
540
    cnt++;
1,243,654✔
541
    dataPoint = dataPoint->next;
1,243,654✔
542
  }
543

544
  return TSDB_CODE_SUCCESS;
268,108✔
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