1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "json_utils.h"
17 #include <inttypes.h>
18 #include <string.h>
19 #include "securec.h"
20 #include "clib_error.h"
21 #include "hc_types.h"
22 #include "string_util.h"
23 #include "hc_log.h"
24
25 #define RECURSE_FLAG_TRUE 1
26 #define MAX_DEPTH 10
27 #define MAX_LEN 5
28 #define BIG_INT_ARR "bigIntArr"
29 #define SPLIT_LEN_ONE 1
30 #define SPLIT_LEN_TWO 2
31
GetCjsonMaxDepth(const char * jsonStr)32 static int32_t GetCjsonMaxDepth(const char *jsonStr)
33 {
34 int32_t max = 0;
35 uint32_t len = HcStrlen(jsonStr);
36 int32_t cnt = 0;
37 for (uint32_t i = 0; i < len; i++) {
38 if (jsonStr[i] == '{' || jsonStr[i] == '[') {
39 cnt++;
40 if (cnt > max) {
41 max = cnt;
42 }
43 } else if (jsonStr[i] == '}' || jsonStr[i] == ']') {
44 cnt--;
45 }
46 }
47 return max;
48 }
49
CreateJsonFromString(const char * jsonStr)50 CJson *CreateJsonFromString(const char *jsonStr)
51 {
52 if (jsonStr == NULL) {
53 return NULL;
54 }
55 int32_t depth = GetCjsonMaxDepth(jsonStr);
56 if (depth > MAX_DEPTH) {
57 LOGE("jsonStr depth %" LOG_PUB "d over 10", depth);
58 return NULL;
59 }
60 return cJSON_Parse(jsonStr);
61 }
62
CreateJson(void)63 CJson *CreateJson(void)
64 {
65 return cJSON_CreateObject();
66 }
67
CreateJsonArray(void)68 CJson *CreateJsonArray(void)
69 {
70 return cJSON_CreateArray();
71 }
72
DuplicateJson(const CJson * jsonObj)73 CJson *DuplicateJson(const CJson *jsonObj)
74 {
75 if (jsonObj == NULL) {
76 return NULL;
77 }
78 return cJSON_Duplicate(jsonObj, RECURSE_FLAG_TRUE);
79 }
80
FreeJson(CJson * jsonObj)81 void FreeJson(CJson *jsonObj)
82 {
83 cJSON_Delete(jsonObj);
84 }
85
DeleteItemFromJson(CJson * jsonObj,const char * key)86 void DeleteItemFromJson(CJson *jsonObj, const char *key)
87 {
88 if (jsonObj == NULL || key == NULL) {
89 return;
90 }
91 cJSON_DeleteItemFromObjectCaseSensitive(jsonObj, key);
92 }
93
DeleteAllItemExceptOne(CJson * jsonObj,const char * key)94 void DeleteAllItemExceptOne(CJson *jsonObj, const char *key)
95 {
96 if (jsonObj == NULL || key == NULL) {
97 return;
98 }
99
100 CJson *curItem = jsonObj->child;
101 CJson *nextItem = NULL;
102 while (curItem != NULL) {
103 nextItem = curItem->next;
104 if (!IsStrEqual(key, curItem->string)) {
105 cJSON_Delete(cJSON_DetachItemViaPointer(jsonObj, curItem));
106 }
107 curItem = nextItem;
108 }
109 }
110
DeleteAllItem(CJson * jsonObj)111 void DeleteAllItem(CJson *jsonObj)
112 {
113 if (jsonObj == NULL) {
114 return;
115 }
116
117 CJson *curItem = jsonObj->child;
118 CJson *nextItem = NULL;
119 while (curItem != NULL) {
120 nextItem = curItem->next;
121 cJSON_Delete(cJSON_DetachItemViaPointer(jsonObj, curItem));
122 curItem = nextItem;
123 }
124 }
125
DetachItemFromJson(CJson * jsonObj,const char * key)126 CJson *DetachItemFromJson(CJson *jsonObj, const char *key)
127 {
128 if (jsonObj == NULL || key == NULL) {
129 return NULL;
130 }
131
132 return cJSON_DetachItemFromObjectCaseSensitive(jsonObj, key);
133 }
134
ReplaceStringToInt(char * input,const char * keyName)135 static void ReplaceStringToInt(char *input, const char *keyName)
136 {
137 if (keyName == NULL) {
138 LOGE("input keyName is NULL.");
139 return;
140 }
141 uint32_t keywordLen = SPLIT_LEN_ONE + HcStrlen(keyName) + SPLIT_LEN_TWO;
142 char keyword[keywordLen + 1];
143 keyword[0] = '"';
144 if (strcpy_s(&keyword[1], HcStrlen(keyName) + 1, keyName) != EOK) {
145 LOGE("failed to copy keyword to buffer.");
146 return;
147 }
148 keyword[keywordLen - SPLIT_LEN_TWO] = '"';
149 keyword[keywordLen - SPLIT_LEN_ONE] = ':';
150 keyword[keywordLen] = '\0';
151 const char * const pos1 = strstr(input, keyword);
152 if (pos1 == NULL) {
153 LOGW("keyword not found.");
154 return;
155 }
156 const char * const pos2 = strstr(pos1 + keywordLen + 1, "\"");
157 if (pos2 == NULL) {
158 LOGW("json key format parse error.");
159 return;
160 }
161 uint32_t startOffset = pos1 - input + keywordLen;
162 uint32_t endOffset = pos2 - input;
163
164 bool shouldReplace = true;
165 for (uint32_t i = startOffset + 1; i < endOffset; i++) {
166 // only replace if the content is only composed of digits
167 shouldReplace &= input[i] >= '0' && input[i] <= '9';
168 }
169
170 if (shouldReplace) {
171 uint32_t readI = startOffset + SPLIT_LEN_ONE;
172 uint32_t writeI = startOffset;
173 while (input[readI]) {
174 input[writeI] = input[readI];
175 readI++;
176 writeI++;
177 if (readI == endOffset) {
178 readI++;
179 }
180 }
181 input[writeI] = '\0';
182 }
183 }
184
CreateKeyList(CJson * arr)185 static char **CreateKeyList(CJson *arr)
186 {
187 int keyListSize = GetItemNum(arr);
188 char **keyList = HcMalloc(keyListSize, sizeof(char *));
189 if (keyList == NULL) {
190 LOGE("Malloc keyList failed.");
191 return NULL;
192 }
193 for (int i = 0; i < keyListSize; i++) {
194 const char *str = GetStringValue(GetItemFromArray(arr, i));
195 if (str == NULL) {
196 keyList[i] = NULL;
197 } else {
198 keyList[i] = strdup(str);
199 }
200 }
201 return keyList;
202 }
203
DestroyKeyList(int size,char ** keyList)204 static void DestroyKeyList(int size, char **keyList)
205 {
206 for (int i = 0; i < size; i++) {
207 HcFree(keyList[i]);
208 }
209 HcFree(keyList);
210 }
211
PackJsonWithBigIntArrToString(const CJson * jsonObj,CJson * arr)212 static char *PackJsonWithBigIntArrToString(const CJson *jsonObj, CJson *arr)
213 {
214 int keyListSize = GetItemNum(arr);
215 char **keyList = CreateKeyList(arr);
216 if (keyList == NULL) {
217 return NULL;
218 }
219 CJson *dupJson = cJSON_Duplicate(jsonObj, RECURSE_FLAG_TRUE);
220 if (dupJson == NULL) {
221 DestroyKeyList(keyListSize, keyList);
222 LOGE("duplicate json failed.");
223 return NULL;
224 }
225 cJSON_DeleteItemFromObject(dupJson, BIG_INT_ARR);
226
227 char *jsonStr = NULL;
228 do {
229 jsonStr = cJSON_PrintUnformatted(dupJson);
230 if (jsonStr == NULL) {
231 LOGE("dup json to str failed.");
232 break;
233 }
234 for (int i = 0; i < keyListSize; i++) {
235 if (keyList[i] != NULL) {
236 ReplaceStringToInt(jsonStr, keyList[i]);
237 }
238 }
239 } while (0);
240 cJSON_free(dupJson);
241 DestroyKeyList(keyListSize, keyList);
242 return jsonStr;
243 }
244
PackJsonToString(const CJson * jsonObj)245 char *PackJsonToString(const CJson *jsonObj)
246 {
247 if (jsonObj == NULL) {
248 return NULL;
249 }
250 CJson *arr = GetObjFromJson(jsonObj, BIG_INT_ARR);
251 if (arr == NULL) {
252 return cJSON_PrintUnformatted(jsonObj);
253 }
254 return PackJsonWithBigIntArrToString(jsonObj, arr);
255 }
256
FreeJsonString(char * jsonStr)257 void FreeJsonString(char *jsonStr)
258 {
259 if (jsonStr != NULL) {
260 cJSON_free(jsonStr);
261 }
262 }
263
GetItemNum(const CJson * jsonObj)264 int GetItemNum(const CJson *jsonObj)
265 {
266 if (jsonObj == NULL) {
267 return 0;
268 }
269 return cJSON_GetArraySize(jsonObj);
270 }
271
GetItemKey(const CJson * item)272 const char *GetItemKey(const CJson *item)
273 {
274 if (item == NULL) {
275 return NULL;
276 }
277 return item->string;
278 }
279
GetObjFromJson(const CJson * jsonObj,const char * key)280 CJson *GetObjFromJson(const CJson *jsonObj, const char *key)
281 {
282 if (jsonObj == NULL || key == NULL) {
283 return NULL;
284 }
285
286 cJSON *objValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
287 if (objValue != NULL) {
288 return objValue;
289 }
290
291 int len = cJSON_GetArraySize(jsonObj);
292 for (int i = 0; i < len; i++) {
293 cJSON *item = cJSON_GetArrayItem(jsonObj, i);
294 if (cJSON_IsObject(item)) {
295 cJSON *obj = GetObjFromJson(item, key);
296 if (obj != NULL) {
297 return obj;
298 }
299 }
300 }
301
302 return NULL;
303 }
304
GetItemFromArray(const CJson * jsonArr,int index)305 CJson *GetItemFromArray(const CJson *jsonArr, int index)
306 {
307 if (jsonArr == NULL) {
308 return NULL;
309 }
310 return cJSON_GetArrayItem(jsonArr, index);
311 }
312
GetStringFromJson(const CJson * jsonObj,const char * key)313 const char *GetStringFromJson(const CJson *jsonObj, const char *key)
314 {
315 if (jsonObj == NULL || key == NULL) {
316 return NULL;
317 }
318
319 cJSON *jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
320 if (jsonObjTmp != NULL && cJSON_IsString(jsonObjTmp)) {
321 return cJSON_GetStringValue(jsonObjTmp);
322 }
323
324 int len = cJSON_GetArraySize(jsonObj);
325 for (int i = 0; i < len; i++) {
326 cJSON *item = cJSON_GetArrayItem(jsonObj, i);
327 if (cJSON_IsObject(item)) {
328 const char *resValue = GetStringFromJson(item, key);
329 if (resValue != NULL) {
330 return resValue;
331 }
332 }
333 }
334
335 return NULL;
336 }
337
GetByteLenFromJson(const CJson * jsonObj,const char * key,uint32_t * byteLen)338 int32_t GetByteLenFromJson(const CJson *jsonObj, const char *key, uint32_t *byteLen)
339 {
340 if (jsonObj == NULL || key == NULL || byteLen == NULL) {
341 return CLIB_ERR_NULL_PTR;
342 }
343
344 const char *valueStr = GetStringFromJson(jsonObj, key);
345 if (valueStr == NULL) {
346 return CLIB_ERR_JSON_GET;
347 }
348 *byteLen = HcStrlen(valueStr) / BYTE_TO_HEX_OPER_LENGTH;
349 return CLIB_SUCCESS;
350 }
351
GetByteFromJson(const CJson * jsonObj,const char * key,uint8_t * byte,uint32_t len)352 int32_t GetByteFromJson(const CJson *jsonObj, const char *key, uint8_t *byte, uint32_t len)
353 {
354 if (jsonObj == NULL || key == NULL || byte == NULL) {
355 return CLIB_ERR_NULL_PTR;
356 }
357
358 const char *valueStr = GetStringFromJson(jsonObj, key);
359 if (valueStr == NULL) {
360 return CLIB_ERR_JSON_GET;
361 }
362 if (len < HcStrlen(valueStr) / BYTE_TO_HEX_OPER_LENGTH) {
363 return CLIB_ERR_INVALID_LEN;
364 }
365 return HexStringToByte(valueStr, byte, len);
366 }
367
GetIntFromJson(const CJson * jsonObj,const char * key,int32_t * value)368 int32_t GetIntFromJson(const CJson *jsonObj, const char *key, int32_t *value)
369 {
370 if (jsonObj == NULL || key == NULL || value == NULL) {
371 return CLIB_ERR_NULL_PTR;
372 }
373
374 cJSON *jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
375 if (jsonObjTmp != NULL && cJSON_IsNumber(jsonObjTmp)) {
376 *value = (int)cJSON_GetNumberValue(jsonObjTmp);
377 return CLIB_SUCCESS;
378 }
379
380 int len = cJSON_GetArraySize(jsonObj);
381 for (int i = 0; i < len; i++) {
382 cJSON *item = cJSON_GetArrayItem(jsonObj, i);
383 if (cJSON_IsObject(item)) {
384 int32_t ret = GetIntFromJson(item, key, value);
385 if (ret == CLIB_SUCCESS) {
386 return ret;
387 }
388 }
389 }
390
391 return CLIB_ERR_JSON_GET;
392 }
393
GetUnsignedIntFromJson(const CJson * jsonObj,const char * key,uint32_t * value)394 int32_t GetUnsignedIntFromJson(const CJson *jsonObj, const char *key, uint32_t *value)
395 {
396 if (jsonObj == NULL || key == NULL || value == NULL) {
397 return CLIB_ERR_NULL_PTR;
398 }
399
400 cJSON *jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
401 if (jsonObjTmp != NULL && cJSON_IsNumber(jsonObjTmp)) {
402 double realValue = cJSON_GetNumberValue(jsonObjTmp);
403 if (realValue < 0) {
404 int32_t tmpValue = (int32_t)realValue;
405 *value = (uint32_t)tmpValue;
406 } else {
407 *value = (uint32_t)realValue;
408 }
409 return CLIB_SUCCESS;
410 }
411
412 int len = cJSON_GetArraySize(jsonObj);
413 for (int i = 0; i < len; i++) {
414 cJSON *item = cJSON_GetArrayItem(jsonObj, i);
415 if (cJSON_IsObject(item)) {
416 int32_t ret = GetUnsignedIntFromJson(item, key, value);
417 if (ret == CLIB_SUCCESS) {
418 return ret;
419 }
420 }
421 }
422
423 return CLIB_ERR_JSON_GET;
424 }
425
GetUint8FromJson(const CJson * jsonObj,const char * key,uint8_t * value)426 int32_t GetUint8FromJson(const CJson *jsonObj, const char *key, uint8_t *value)
427 {
428 if (jsonObj == NULL || key == NULL || value == NULL) {
429 return CLIB_ERR_NULL_PTR;
430 }
431
432 cJSON *jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
433 if (jsonObjTmp != NULL && cJSON_IsNumber(jsonObjTmp)) {
434 double realValue = cJSON_GetNumberValue(jsonObjTmp);
435 if (realValue < 0) {
436 int8_t tmpValue = (int8_t)realValue;
437 *value = (uint8_t)tmpValue;
438 } else {
439 *value = (uint8_t)realValue;
440 }
441 return CLIB_SUCCESS;
442 }
443
444 int len = cJSON_GetArraySize(jsonObj);
445 for (int i = 0; i < len; i++) {
446 cJSON *item = cJSON_GetArrayItem(jsonObj, i);
447 if (cJSON_IsObject(item)) {
448 int8_t ret = GetUint8FromJson(item, key, value);
449 if (ret == CLIB_SUCCESS) {
450 return ret;
451 }
452 }
453 }
454
455 return CLIB_ERR_JSON_GET;
456 }
457
GetInt64FromJson(const CJson * jsonObj,const char * key,int64_t * value)458 int32_t GetInt64FromJson(const CJson *jsonObj, const char *key, int64_t *value)
459 {
460 const char *str = GetStringFromJson(jsonObj, key);
461 if (str == NULL) {
462 return CLIB_ERR_JSON_GET;
463 }
464 *value = StringToInt64(str);
465 return CLIB_SUCCESS;
466 }
467
GetBoolFromJson(const CJson * jsonObj,const char * key,bool * value)468 int32_t GetBoolFromJson(const CJson *jsonObj, const char *key, bool *value)
469 {
470 if (jsonObj == NULL || key == NULL || value == NULL) {
471 return CLIB_ERR_NULL_PTR;
472 }
473
474 cJSON *jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
475 if (jsonObjTmp != NULL && cJSON_IsBool(jsonObjTmp)) {
476 *value = cJSON_IsTrue(jsonObjTmp) ? true : false;
477 return CLIB_SUCCESS;
478 }
479
480 int len = cJSON_GetArraySize(jsonObj);
481 for (int i = 0; i < len; i++) {
482 cJSON *item = cJSON_GetArrayItem(jsonObj, i);
483 if (cJSON_IsObject(item)) {
484 int32_t ret = GetBoolFromJson(item, key, value);
485 if (ret == CLIB_SUCCESS) {
486 return ret;
487 }
488 }
489 }
490
491 return CLIB_ERR_JSON_GET;
492 }
493
GetStringValue(const CJson * item)494 char *GetStringValue(const CJson *item)
495 {
496 if (item == NULL) {
497 return NULL;
498 }
499 return cJSON_GetStringValue(item);
500 }
501
AddObjToJson(CJson * jsonObj,const char * key,const CJson * childObj)502 int32_t AddObjToJson(CJson *jsonObj, const char *key, const CJson *childObj)
503 {
504 if (jsonObj == NULL || key == NULL || childObj == NULL) {
505 return CLIB_ERR_NULL_PTR;
506 }
507
508 cJSON *tmpObj = cJSON_Duplicate(childObj, RECURSE_FLAG_TRUE);
509 if (tmpObj == NULL) {
510 return CLIB_ERR_JSON_DUPLICATE;
511 }
512
513 cJSON *objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
514 if (objInJson == NULL) {
515 if (cJSON_AddItemToObject(jsonObj, key, tmpObj) == false) {
516 cJSON_Delete(tmpObj);
517 return CLIB_ERR_JSON_ADD;
518 }
519 } else {
520 if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key, tmpObj) == false) {
521 cJSON_Delete(tmpObj);
522 return CLIB_ERR_JSON_REPLACE;
523 }
524 }
525
526 return CLIB_SUCCESS;
527 }
528
AddObjToArray(CJson * jsonArr,CJson * item)529 int32_t AddObjToArray(CJson *jsonArr, CJson *item)
530 {
531 if (jsonArr == NULL || item == NULL) {
532 return CLIB_ERR_NULL_PTR;
533 }
534
535 if (cJSON_IsArray(jsonArr) == false) {
536 return CLIB_ERR_INVALID_PARAM;
537 }
538
539 bool ret = cJSON_AddItemToArray(jsonArr, item);
540 if (ret == false) {
541 return CLIB_ERR_JSON_ADD;
542 }
543
544 return CLIB_SUCCESS;
545 }
546
AddStringToArray(CJson * jsonArr,const char * string)547 int32_t AddStringToArray(CJson *jsonArr, const char *string)
548 {
549 if (jsonArr == NULL || string == NULL) {
550 return CLIB_ERR_NULL_PTR;
551 }
552
553 if (cJSON_IsArray(jsonArr) == false) {
554 return CLIB_ERR_INVALID_PARAM;
555 }
556
557 cJSON *strObj = cJSON_CreateString(string);
558 if (strObj == NULL) {
559 return CLIB_ERR_BAD_ALLOC;
560 }
561 bool ret = cJSON_AddItemToArray(jsonArr, strObj);
562 if (ret == false) {
563 cJSON_Delete(strObj);
564 return CLIB_ERR_JSON_ADD;
565 }
566
567 return CLIB_SUCCESS;
568 }
569
AddStringToJson(CJson * jsonObj,const char * key,const char * value)570 int32_t AddStringToJson(CJson *jsonObj, const char *key, const char *value)
571 {
572 if (jsonObj == NULL || key == NULL || value == NULL) {
573 return CLIB_ERR_NULL_PTR;
574 }
575
576 cJSON *objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
577 if (objInJson == NULL) {
578 if (cJSON_AddStringToObject(jsonObj, key, value) == NULL) {
579 return CLIB_ERR_JSON_GET;
580 }
581 } else {
582 cJSON *tmp = cJSON_CreateString(value);
583 if (tmp == NULL) {
584 LOGE("The operation of cJSON_CreateString failed.");
585 return CLIB_ERR_BAD_ALLOC;
586 }
587 if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key, tmp) == false) {
588 cJSON_Delete(tmp);
589 return CLIB_ERR_JSON_REPLACE;
590 }
591 }
592
593 return CLIB_SUCCESS;
594 }
595
AddByteToJson(CJson * jsonObj,const char * key,const uint8_t * byte,uint32_t len)596 int32_t AddByteToJson(CJson *jsonObj, const char *key, const uint8_t *byte, uint32_t len)
597 {
598 if (jsonObj == NULL || key == NULL || byte == NULL) {
599 return CLIB_ERR_NULL_PTR;
600 }
601
602 uint32_t hexLen = len * BYTE_TO_HEX_OPER_LENGTH + 1;
603 char *hexStr = (char *)HcMalloc(hexLen, 0);
604 if (hexStr == NULL) {
605 return CLIB_ERR_BAD_ALLOC;
606 }
607 int32_t ret = ByteToHexString(byte, len, hexStr, hexLen);
608 if (ret != CLIB_SUCCESS) {
609 HcFree(hexStr);
610 return ret;
611 }
612
613 ret = AddStringToJson(jsonObj, key, hexStr);
614 if (ret != CLIB_SUCCESS) {
615 HcFree(hexStr);
616 return ret;
617 }
618
619 HcFree(hexStr);
620 return CLIB_SUCCESS;
621 }
622
AddBoolToJson(CJson * jsonObj,const char * key,bool value)623 int32_t AddBoolToJson(CJson *jsonObj, const char *key, bool value)
624 {
625 if (jsonObj == NULL || key == NULL) {
626 return CLIB_ERR_NULL_PTR;
627 }
628
629 cJSON *objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
630 if (objInJson == NULL) {
631 if (cJSON_AddBoolToObject(jsonObj, key, value) == NULL) {
632 return CLIB_ERR_JSON_GET;
633 }
634 } else {
635 cJSON *tmp = cJSON_CreateBool(value);
636 if (tmp == NULL) {
637 LOGE("cJSON_CreateString failed.");
638 return CLIB_ERR_BAD_ALLOC;
639 }
640 if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key, tmp) == false) {
641 cJSON_Delete(tmp);
642 return CLIB_ERR_JSON_REPLACE;
643 }
644 }
645
646 return CLIB_SUCCESS;
647 }
648
AddIntToJson(CJson * jsonObj,const char * key,int value)649 int32_t AddIntToJson(CJson *jsonObj, const char *key, int value)
650 {
651 if (jsonObj == NULL || key == NULL) {
652 return CLIB_ERR_NULL_PTR;
653 }
654
655 cJSON *objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key);
656 if (objInJson == NULL) {
657 if (cJSON_AddNumberToObject(jsonObj, key, value) == NULL) {
658 return CLIB_ERR_JSON_GET;
659 }
660 } else {
661 cJSON *tmp = cJSON_CreateNumber(value);
662 if (tmp == NULL) {
663 return CLIB_ERR_BAD_ALLOC;
664 }
665 if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key, tmp) == false) {
666 cJSON_Delete(tmp);
667 return CLIB_ERR_JSON_REPLACE;
668 }
669 }
670
671 return CLIB_SUCCESS;
672 }
673
AddInt64StringToJson(CJson * jsonObj,const char * key,int64_t value)674 int32_t AddInt64StringToJson(CJson *jsonObj, const char *key, int64_t value)
675 {
676 char buffer[65] = { 0 };
677 if (sprintf_s(buffer, sizeof(buffer), "%" PRId64, value) <= 0) {
678 return CLIB_FAILED;
679 }
680 if (AddStringToJson(jsonObj, key, buffer) != CLIB_SUCCESS) {
681 return CLIB_ERR_JSON_ADD;
682 }
683 return CLIB_SUCCESS;
684 }
685
AddStringArrayToJson(CJson * jsonObj,const char * key,const char * const * stringArray,uint32_t arrayLen)686 int32_t AddStringArrayToJson(CJson *jsonObj, const char *key, const char * const *stringArray, uint32_t arrayLen)
687 {
688 if (jsonObj == NULL || key == NULL || stringArray == NULL) {
689 return CLIB_ERR_NULL_PTR;
690 }
691
692 cJSON *strArrayObj = cJSON_CreateStringArray(stringArray, arrayLen);
693 if (strArrayObj == NULL) {
694 return CLIB_ERR_BAD_ALLOC;
695 }
696 if (cJSON_AddItemToObject(jsonObj, key, strArrayObj) == false) {
697 cJSON_Delete(strArrayObj);
698 return CLIB_ERR_JSON_ADD;
699 }
700 return CLIB_SUCCESS;
701 }
702
ClearSensitiveStringInJson(CJson * jsonObj,const char * key)703 void ClearSensitiveStringInJson(CJson *jsonObj, const char *key)
704 {
705 if (jsonObj == NULL || key == NULL) {
706 return;
707 }
708 char *str = (char *)GetStringFromJson(jsonObj, key);
709 if (str == NULL) {
710 return;
711 }
712 (void)memset_s(str, HcStrlen(str), 0, HcStrlen(str));
713 }
714
ClearAndFreeJsonString(char * jsonStr)715 void ClearAndFreeJsonString(char *jsonStr)
716 {
717 if (jsonStr == NULL) {
718 return;
719 }
720 (void)memset_s(jsonStr, HcStrlen(jsonStr), 0, HcStrlen(jsonStr));
721 FreeJsonString(jsonStr);
722 }
723