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

taosdata / TDengine / #4473

08 Jul 2025 09:38AM UTC coverage: 62.922% (+0.7%) from 62.22%
#4473

push

travis-ci

web-flow
Merge pull request #31712 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

158525 of 321496 branches covered (49.31%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

1333 existing lines in 67 files now uncovered.

245526 of 320647 relevant lines covered (76.57%)

17689640.25 hits per line

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

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

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

35
const char    *jsonName[OTD_JSON_FIELDS_NUM] = {"metric", "timestamp", "value", "tags"};
36
static int32_t smlGetJsonElements(cJSON *root, cJSON ***marks) {
5,309✔
37
  for (int i = 0; i < OTD_JSON_FIELDS_NUM; ++i) {
26,536✔
38
    cJSON *child = root->child;
21,227✔
39
    while (child != NULL) {
53,036!
40
      if (strcasecmp(child->string, jsonName[i]) == 0) {
53,045✔
41
        *marks[i] = child;
21,236✔
42
        break;
21,236✔
43
      }
44
      child = child->next;
31,809✔
45
    }
46
    if (*marks[i] == NULL) {
21,227!
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,309✔
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,867✔
67
  // tinyint
68
  if (strcasecmp(typeStr, "i8") == 0 || strcasecmp(typeStr, "tinyint") == 0) {
2,867!
69
    if (!IS_VALID_TINYINT(value->valuedouble)) {
483✔
70
      uError("SML:JSON value(%f) cannot fit in type(tinyint)", value->valuedouble);
3!
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)) {
475!
81
      uError("SML:JSON value(%f) cannot fit in type(smallint)", value->valuedouble);
2!
82
      return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
4✔
83
    }
84
    pVal->type = TSDB_DATA_TYPE_SMALLINT;
473✔
85
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
473✔
86
    pVal->i = value->valuedouble;
473✔
87
    return TSDB_CODE_SUCCESS;
473✔
88
  }
89
  // int
90
  if (strcasecmp(typeStr, "i32") == 0 || strcasecmp(typeStr, "int") == 0) {
1,909!
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,432✔
102
    pVal->type = TSDB_DATA_TYPE_BIGINT;
488✔
103
    pVal->length = (int16_t)tDataTypes[pVal->type].bytes;
488✔
104
    if (value->valuedouble >= (double)INT64_MAX) {
488✔
105
      pVal->i = INT64_MAX;
159✔
106
    } else if (value->valuedouble <= (double)INT64_MIN) {
329✔
107
      pVal->i = INT64_MIN;
2✔
108
    } else {
109
      pVal->i = value->valuedouble;
327✔
110
    }
111
    return TSDB_CODE_SUCCESS;
488✔
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,965✔
138
  if (strcasecmp(typeStr, "binary") == 0) {
2,965✔
139
    pVal->type = TSDB_DATA_TYPE_BINARY;
2,452✔
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,936✔
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,936!
151
    return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
×
152
  }
153
  if (pVal->type == TSDB_DATA_TYPE_NCHAR &&
2,936✔
154
      pVal->length > (TSDB_MAX_NCHAR_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
483!
155
    return TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN;
×
156
  }
157

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

162
static int32_t smlParseValueFromJSONObj(cJSON *root, SSmlKv *kv) {
4,904✔
163
  int32_t ret = TSDB_CODE_SUCCESS;
4,904✔
164
  int32_t size = cJSON_GetArraySize(root);
4,904✔
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,903!
172
    return TSDB_CODE_TSC_INVALID_JSON;
×
173
  }
174

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

180
  switch (value->type) {
4,902!
181
    case cJSON_True:
996✔
182
    case cJSON_False: {
183
      ret = smlConvertJSONBool(kv, type->valuestring, value);
996✔
184
      if (ret != TSDB_CODE_SUCCESS) {
996!
185
        return ret;
×
186
      }
187
      break;
996✔
188
    }
189
    case cJSON_Number: {
2,868✔
190
      ret = smlConvertJSONNumber(kv, type->valuestring, value);
2,868✔
191
      if (ret != TSDB_CODE_SUCCESS) {
2,868✔
192
        return ret;
16✔
193
      }
194
      break;
2,852✔
195
    }
196
    case cJSON_String: {
1,038✔
197
      ret = smlConvertJSONString(kv, type->valuestring, value);
1,038✔
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,856✔
208
}
209

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

245
  return TSDB_CODE_SUCCESS;
13,073✔
246
}
247

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

253
  int32_t tagNum = cJSON_GetArraySize(tags);
905✔
254
  if (unlikely(tagNum == 0)) {
905✔
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,670✔
259
    cJSON *tag = cJSON_GetArrayItem(tags, i);
7,813✔
260
    if (unlikely(tag == NULL)) {
7,809!
261
      return TSDB_CODE_TSC_INVALID_JSON;
40✔
262
    }
263
    size_t keyLen = strlen(tag->string);
7,809✔
264
    if (unlikely(IS_INVALID_COL_LEN(keyLen))) {
7,809!
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,808✔
271
    kv.key = tag->string;
7,808✔
272
    kv.keyLen = keyLen;
7,808✔
273

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

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

287
    cnt++;
7,767✔
288
  }
289
  return TSDB_CODE_SUCCESS;
857✔
290
}
291

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

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

313
static int64_t smlParseTSFromJSONObj(SSmlHandle *info, cJSON *root, int32_t toPrecision) {
1,657✔
314
  int32_t size = cJSON_GetArraySize(root);
1,657✔
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,658✔
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,656✔
327
  if (unlikely(!cJSON_IsString(type))) {
1,656!
328
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
×
329
    return TSDB_CODE_TSC_INVALID_JSON;
×
330
  }
331

332
  double timeDouble = value->valuedouble;
1,656✔
333
  if (unlikely(smlDoubleToInt64OverFlow(timeDouble))) {
1,656!
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,656!
339
    return taosGetTimestampNs() / smlFactorNS[toPrecision];
×
340
  }
341

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

346
  int64_t tsInt64 = timeDouble;
1,656✔
347
  size_t  typeLen = strlen(type->valuestring);
1,656✔
348
  if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) {
1,656!
349
    // seconds
350
    if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
7!
351
      return tsInt64 * smlFactorS[toPrecision];
7✔
352
    }
353
    return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
354
  } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) {
1,649!
355
    switch (type->valuestring[0]) {
1,650!
356
      case 'm':
1,463✔
357
      case 'M':
358
        // milliseconds
359
        return convertTimePrecision(tsInt64, TSDB_TIME_PRECISION_MILLI, toPrecision);
1,463✔
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,159✔
376
  uint8_t len = 0;
3,159✔
377
  while ((num /= 10) != 0) {
40,608✔
378
    len++;
37,449✔
379
  }
380
  len++;
3,159✔
381
  return len;
3,159✔
382
}
383

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

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

404
    int8_t fromPrecision = smlGetTsTypeByLen(tsLen);
3,152✔
405
    if (unlikely(fromPrecision == -1)) {
3,151!
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,151✔
411
    if (fromPrecision == TSDB_TIME_PRECISION_SECONDS) {
3,151✔
412
      if (smlFactorS[toPrecision] < INT64_MAX / tsInt64) {
106!
413
        return tsInt64 * smlFactorS[toPrecision];
106✔
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,658✔
420
    return smlParseTSFromJSONObj(info, timestamp, toPrecision);
1,657✔
421
  } else {
422
    smlBuildInvalidDataMsg(&info->msgBuf, "invalidate json", NULL);
1✔
423
    return TSDB_CODE_TSC_INVALID_JSON;
3✔
424
  }
425
}
426

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

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

436
  int32_t size = cJSON_GetArraySize(root);
5,314✔
437
  // outmost json fields has to be exactly 4
438
  if (size != OTD_JSON_FIELDS_NUM) {
5,314✔
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,310✔
444
  SML_CHECK_CODE(smlGetJsonElements(root, marks));
5,310!
445
  // Parse metric
446
  SML_CHECK_CODE(smlParseMetricFromJSON(info, metricJson, elements));
5,321✔
447
  // Parse metric value
448
  SSmlKv kv = {.key = VALUE, .keyLen = VALUE_LEN};
5,316✔
449
  SML_CHECK_CODE(smlParseValueFromJSON(valueJson, &kv));
5,316✔
450

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

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

458
  if (unlikely(info->reRun)) {
5,258✔
459
    goto END;
428✔
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,830✔
465
  if (unlikely(ts < 0)) {
4,831✔
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,825✔
476
  smlBuildTsKv(&kvTs, ts);
4,825✔
477
  if (info->dataFormat){
4,821✔
478
    code = smlParseEndTelnetJsonFormat(info, elements, &kvTs, &kv);
3,394✔
479
  } else {
480
    code = smlParseEndTelnetJsonUnFormat(info, elements, &kvTs, &kv);
1,427✔
481
  }
482
  SML_CHECK_CODE(code);
4,824!
483
  taosMemoryFreeClear(info->preLine.tags);
4,824!
484
  info->preLine = *elements;
4,828✔
485
  elements->tags = NULL;
4,828✔
486

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

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

496
  info->root = cJSON_Parse(payload);
836✔
497
  if (unlikely(info->root == NULL)) {
844✔
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)) {
840✔
504
    payloadNum = cJSON_GetArraySize(info->root);
537✔
505
  } else if (cJSON_IsObject(info->root)) {
301!
506
    payloadNum = 1;
301✔
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;
837✔
513
  cJSON *head = (payloadNum == 1 && cJSON_IsObject(info->root)) ? info->root : info->root->child;
837✔
514

515
  int    cnt = 0;
837✔
516
  cJSON *dataPoint = head;
837✔
517
  while (dataPoint) {
6,088✔
518
    if (info->dataFormat) {
5,314✔
519
      SSmlLineInfo element = {0};
3,860✔
520
      ret = smlParseJSONStringExt(info, dataPoint, &element);
3,860✔
521
      if (element.measureTagsLen != 0) taosMemoryFree(element.measureTag);
3,867!
522
    } else {
523
      ret = smlParseJSONStringExt(info, dataPoint, info->lines + cnt);
1,454✔
524
    }
525
    if (unlikely(ret != TSDB_CODE_SUCCESS)) {
5,321✔
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,253✔
531
      cnt = 0;
428✔
532
      dataPoint = head;
428✔
533
      info->lineNum = payloadNum;
428✔
534
      ret = smlClearForRerun(info);
428✔
535
      if (ret != TSDB_CODE_SUCCESS) {
426!
536
        return ret;
×
537
      }
538
      continue;
426✔
539
    }
540
    cnt++;
4,825✔
541
    dataPoint = dataPoint->next;
4,825✔
542
  }
543

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

© 2025 Coveralls, Inc