1 /*
2 * Copyright (C) 2025 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 <cinttypes>
18 #include <cstdint>
19 #include <string>
20 #include "securec.h"
21 namespace OHOS {
22 namespace AccountSA {
23 #define RECURSE_FLAG_TRUE 1
24 #define DECIMALISM 10
25
IsKeyExist(const CJson * jsonObj,const std::string & key)26 bool IsKeyExist(const CJson *jsonObj, const std::string &key)
27 {
28 if (jsonObj == nullptr || key.empty()) {
29 return false;
30 }
31
32 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
33 return item != nullptr;
34 }
35
IsKeyExist(const CJsonUnique & jsonObj,const std::string & key)36 bool IsKeyExist(const CJsonUnique &jsonObj, const std::string &key)
37 {
38 return IsKeyExist(jsonObj.get(), key);
39 }
40
IsArray(const CJson * const item)41 bool IsArray(const CJson *const item)
42 {
43 return cJSON_IsArray(item) != 0;
44 }
45
IsArray(const CJsonUnique & item)46 bool IsArray(const CJsonUnique &item)
47 {
48 return IsArray(item.get());
49 }
50
IsBool(const CJson * const item)51 bool IsBool(const CJson *const item)
52 {
53 return cJSON_IsBool(item) != 0;
54 }
55
IsBool(const CJsonUnique & item)56 bool IsBool(const CJsonUnique &item)
57 {
58 return IsBool(item.get());
59 }
60
IsNumber(const CJson * const item)61 bool IsNumber(const CJson *const item)
62 {
63 return cJSON_IsNumber(item) != 0;
64 }
65
IsNumber(const CJsonUnique & item)66 bool IsNumber(const CJsonUnique &item)
67 {
68 return IsNumber(item.get());
69 }
70
IsString(const CJson * const item)71 bool IsString(const CJson *const item)
72 {
73 return cJSON_IsString(item) != 0;
74 }
75
IsString(const CJsonUnique & item)76 bool IsString(const CJsonUnique &item)
77 {
78 return IsString(item.get());
79 }
80
IsObject(const CJson * const item)81 bool IsObject(const CJson *const item)
82 {
83 return cJSON_IsObject(item) != 0;
84 }
85
IsObject(const CJsonUnique & item)86 bool IsObject(const CJsonUnique &item)
87 {
88 return IsObject(item.get());
89 }
90
IsInvalid(const CJson * item)91 bool IsInvalid(const CJson *item)
92 {
93 return cJSON_IsInvalid(item) != 0;
94 }
95
IsInvalid(const CJsonUnique & item)96 bool IsInvalid(const CJsonUnique &item)
97 {
98 return IsInvalid(item.get());
99 }
100
IsStructured(CJson * jsonObj)101 bool IsStructured(CJson *jsonObj)
102 {
103 return IsObject(jsonObj);
104 }
105
IsStructured(const CJsonUnique & jsonObj)106 bool IsStructured(const CJsonUnique &jsonObj)
107 {
108 return IsStructured(jsonObj.get());
109 }
110
FreeJson(CJson * jsonObj)111 void FreeJson(CJson *jsonObj)
112 {
113 cJSON_Delete(jsonObj);
114 }
115
CreateJsonFromString(const std::string & jsonStr)116 CJsonUnique CreateJsonFromString(const std::string &jsonStr)
117 {
118 if (jsonStr.empty()) {
119 return nullptr;
120 }
121 CJsonUnique aPtr(cJSON_Parse(jsonStr.c_str()), FreeJson);
122 return aPtr;
123 }
124
CreateJsonNull(void)125 CJsonUnique CreateJsonNull(void)
126 {
127 CJsonUnique aPtr(cJSON_CreateNull(), FreeJson);
128 return aPtr;
129 }
130
CreateJson(void)131 CJsonUnique CreateJson(void)
132 {
133 CJsonUnique aPtr(cJSON_CreateObject(), FreeJson);
134 return aPtr;
135 }
136
CreateJsonArray(void)137 CJsonUnique CreateJsonArray(void)
138 {
139 CJsonUnique aPtr(cJSON_CreateArray(), FreeJson);
140 return aPtr;
141 }
142
DeleteItemFromJson(CJson * jsonObj,const std::string & key)143 int DeleteItemFromJson(CJson *jsonObj, const std::string &key)
144 {
145 if (!IsKeyExist(jsonObj, key)) {
146 return 0;
147 }
148 cJSON_DeleteItemFromObjectCaseSensitive(jsonObj, key.c_str());
149 return 1;
150 }
151
DeleteItemFromJson(CJsonUnique & jsonObj,const std::string & key)152 int DeleteItemFromJson(CJsonUnique &jsonObj, const std::string &key)
153 {
154 return DeleteItemFromJson(jsonObj.get(), key);
155 }
156
FreeJsonString(char * jsonStr)157 void FreeJsonString(char *jsonStr)
158 {
159 if (jsonStr != nullptr) {
160 cJSON_free(jsonStr);
161 }
162 }
163
PackJsonToString(const CJson * jsonObj)164 std::string PackJsonToString(const CJson *jsonObj)
165 {
166 char *buf = cJSON_PrintUnformatted(jsonObj);
167 if (buf == nullptr) {
168 return std::string();
169 }
170 std::string bufStr = std::string(buf);
171 FreeJsonString(buf);
172 return bufStr;
173 }
174
PackJsonToString(const CJsonUnique & jsonObj)175 std::string PackJsonToString(const CJsonUnique &jsonObj)
176 {
177 return PackJsonToString(jsonObj.get());
178 }
179
GetItemNum(const CJson * jsonObj)180 int32_t GetItemNum(const CJson *jsonObj)
181 {
182 return cJSON_GetArraySize(jsonObj);
183 }
184
GetItemNum(const CJsonUnique & jsonObj)185 int32_t GetItemNum(const CJsonUnique &jsonObj)
186 {
187 return cJSON_GetArraySize(jsonObj.get());
188 }
189
GetObjFromJson(const CJson * jsonObj,const std::string & key,CJson ** value)190 bool GetObjFromJson(const CJson *jsonObj, const std::string &key, CJson **value)
191 {
192 if (jsonObj == nullptr || key.empty()) {
193 return false;
194 }
195
196 *value = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
197 return IsObject(*value);
198 }
199
GetObjFromJson(const CJson * jsonObj,const std::string & key)200 CJson *GetObjFromJson(const CJson *jsonObj, const std::string &key)
201 {
202 CJson *value = nullptr;
203 GetObjFromJson(jsonObj, key, &value);
204 return value;
205 }
206
GetObjFromJson(const CJsonUnique & jsonObj,const std::string & key)207 CJson *GetObjFromJson(const CJsonUnique &jsonObj, const std::string &key)
208 {
209 return GetObjFromJson(jsonObj.get(), key);
210 }
211
GetItemFromArray(const CJson * jsonArr,int32_t index)212 CJson *GetItemFromArray(const CJson *jsonArr, int32_t index)
213 {
214 return cJSON_GetArrayItem(jsonArr, index);
215 }
216
GetItemFromArray(const CJsonUnique & jsonArr,int32_t index)217 CJson *GetItemFromArray(const CJsonUnique &jsonArr, int32_t index)
218 {
219 return GetItemFromArray(jsonArr.get(), index);
220 }
221
GetStringFromJson(const CJson * jsonObj,const std::string & key,std::string & value)222 bool GetStringFromJson(const CJson *jsonObj, const std::string &key, std::string &value)
223 {
224 if (jsonObj == nullptr || key.empty()) {
225 return false;
226 }
227 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
228 if (IsString(item)) {
229 value = cJSON_GetStringValue(item);
230 return true;
231 }
232 return false;
233 }
234
GetStringFromJson(const CJson * jsonObj,const std::string & key)235 std::string GetStringFromJson(const CJson *jsonObj, const std::string &key)
236 {
237 std::string value;
238 GetStringFromJson(jsonObj, key, value);
239 return value;
240 }
241
GetStringFromJson(const CJsonUnique & jsonObj,const std::string & key)242 std::string GetStringFromJson(const CJsonUnique &jsonObj, const std::string &key)
243 {
244 return GetStringFromJson(jsonObj.get(), key);
245 }
246
CreateJsonFromMap(const std::map<std::string,std::string> & mapData)247 CJsonUnique CreateJsonFromMap(const std::map<std::string, std::string> &mapData)
248 {
249 CJson *jsonObj = cJSON_CreateObject();
250
251 for (const auto &pair : mapData) {
252 cJSON_AddStringToObject(jsonObj, pair.first.c_str(), pair.second.c_str());
253 }
254
255 CJsonUnique aPtr(jsonObj, FreeJson);
256 return aPtr;
257 }
258
PackJsonToMap(const CJson * jsonObj)259 std::map<std::string, std::string> PackJsonToMap(const CJson *jsonObj)
260 {
261 if (!IsObject(jsonObj)) {
262 return {};
263 }
264
265 std::map<std::string, std::string> mapData;
266 for (cJSON *item = jsonObj->child; item != nullptr; item = item->next) {
267 if (item->valuestring != nullptr) {
268 mapData[item->string] = item->valuestring;
269 }
270 }
271 return mapData;
272 }
273
PackJsonToMap(const CJsonUnique & jsonObj)274 std::map<std::string, std::string> PackJsonToMap(const CJsonUnique &jsonObj)
275 {
276 return PackJsonToMap(jsonObj.get());
277 }
278
GetVectorStringFromJson(const CJson * jsonObj,const std::string & key,std::vector<std::string> & value)279 bool GetVectorStringFromJson(const CJson *jsonObj, const std::string &key, std::vector<std::string> &value)
280 {
281 if (!IsKeyExist(jsonObj, key)) {
282 return false;
283 }
284 CJson *array = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
285 if (!IsArray(array)) {
286 return false;
287 }
288
289 int32_t arraySize = cJSON_GetArraySize(array);
290 for (int32_t i = 0; i < arraySize; i++) {
291 CJson *item = cJSON_GetArrayItem(array, i);
292 if (item->type == cJSON_String) {
293 value.push_back(item->valuestring);
294 }
295 }
296 return true;
297 }
298
GetVectorStringFromJson(const CJson * jsonObj,const std::string & key)299 std::vector<std::string> GetVectorStringFromJson(const CJson *jsonObj, const std::string &key)
300 {
301 std::vector<std::string> value;
302 GetVectorStringFromJson(jsonObj, key, value);
303 return value;
304 }
305
GetVectorStringFromJson(const CJsonUnique & jsonObj,const std::string & key)306 std::vector<std::string> GetVectorStringFromJson(const CJsonUnique &jsonObj, const std::string &key)
307 {
308 return GetVectorStringFromJson(jsonObj.get(), key);
309 }
310
GetVectorUint8FromJson(const CJson * jsonObj,const std::string & key)311 std::vector<uint8_t> GetVectorUint8FromJson(const CJson *jsonObj, const std::string &key)
312 {
313 if (!IsKeyExist(jsonObj, key)) {
314 return {};
315 }
316 std::vector<uint8_t> result;
317 CJson *array = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
318 if (!IsArray(array)) {
319 return result;
320 }
321
322 int32_t arraySize = cJSON_GetArraySize(array);
323 for (int32_t i = 0; i < arraySize; i++) {
324 CJson *item = cJSON_GetArrayItem(array, i);
325 if (item->type == cJSON_Number) {
326 result.push_back(item->valueint);
327 }
328 }
329 return result;
330 }
331
GetVectorUint8FromJson(const CJsonUnique & jsonObj,const std::string & key)332 std::vector<uint8_t> GetVectorUint8FromJson(const CJsonUnique &jsonObj, const std::string &key)
333 {
334 return GetVectorUint8FromJson(jsonObj.get(), key);
335 }
336
GetItemFromJson(const CJson * const object,const std::string & key)337 CJson *GetItemFromJson(const CJson *const object, const std::string &key)
338 {
339 if (!IsKeyExist(object, key)) {
340 return nullptr;
341 }
342 return cJSON_GetObjectItemCaseSensitive(object, key.c_str());
343 }
344
GetItemFromJson(CJsonUnique & object,const std::string & key)345 CJson *GetItemFromJson(CJsonUnique &object, const std::string &key)
346 {
347 return GetItemFromJson(object.get(), key);
348 }
349
AddSetStringToJson(CJson * jsonObj,const std::string & key,const std::set<std::string> & setData)350 bool AddSetStringToJson(CJson *jsonObj, const std::string &key, const std::set<std::string> &setData)
351 {
352 if (jsonObj == nullptr || key.empty()) {
353 return false;
354 }
355
356 CJson *array = cJSON_CreateArray();
357 if (array == nullptr) {
358 return false;
359 }
360 for (const std::string &item : setData) {
361 cJSON_AddItemToArray(array, cJSON_CreateString(item.c_str()));
362 }
363 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
364 if (item == nullptr) {
365 if (!cJSON_AddItemToObject(jsonObj, key.c_str(), array)) {
366 cJSON_Delete(array);
367 return false;
368 }
369 } else {
370 if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), array)) {
371 cJSON_Delete(array);
372 return false;
373 }
374 }
375 return true;
376 }
377
AddSetStringToJson(CJsonUnique & jsonObj,const std::string & key,const std::set<std::string> & setData)378 bool AddSetStringToJson(CJsonUnique &jsonObj, const std::string &key, const std::set<std::string> &setData)
379 {
380 return AddSetStringToJson(jsonObj.get(), key, setData);
381 }
382
GetSetStringFromJson(const CJson * jsonObj,const std::string & key,std::set<std::string> & setData)383 bool GetSetStringFromJson(const CJson *jsonObj, const std::string &key, std::set<std::string> &setData)
384 {
385 if (jsonObj == nullptr || key.empty()) {
386 return false;
387 }
388 CJson *arrayItem = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
389 if (!IsArray(arrayItem)) {
390 return false;
391 }
392 int32_t arraySize = cJSON_GetArraySize(arrayItem);
393 for (int32_t i = 0; i < arraySize; ++i) {
394 CJson *element = cJSON_GetArrayItem(arrayItem, i);
395 if (IsString(element)) {
396 setData.insert(element->valuestring);
397 }
398 }
399 return true;
400 }
401
GetSetStringFromJson(const CJsonUnique & jsonObj,const std::string & key,std::set<std::string> & setData)402 bool GetSetStringFromJson(const CJsonUnique &jsonObj, const std::string &key, std::set<std::string> &setData)
403 {
404 return GetSetStringFromJson(jsonObj.get(), key, setData);
405 }
406
AddVectorStringToJson(CJson * jsonObj,const std::string & key,const std::vector<std::string> & vec)407 bool AddVectorStringToJson(CJson *jsonObj, const std::string &key, const std::vector<std::string> &vec)
408 {
409 if (jsonObj == nullptr || key.empty()) {
410 return false;
411 }
412
413 CJson *array = cJSON_CreateArray();
414 if (array == nullptr) {
415 return false;
416 }
417 for (const auto &str : vec) {
418 AddStringToArray(array, str.c_str());
419 }
420 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
421 if (item == nullptr) {
422 if (!cJSON_AddItemToObject(jsonObj, key.c_str(), array)) {
423 cJSON_Delete(array);
424 return false;
425 }
426 } else {
427 if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), array)) {
428 cJSON_Delete(array);
429 return false;
430 }
431 }
432 return true;
433 }
434
AddVectorStringToJson(CJsonUnique & jsonObj,const std::string & key,const std::vector<std::string> & vec)435 bool AddVectorStringToJson(CJsonUnique &jsonObj, const std::string &key, const std::vector<std::string> &vec)
436 {
437 return AddVectorStringToJson(jsonObj.get(), key, vec);
438 }
439
GetIntFromJson(const CJson * jsonObj,const std::string & key,int32_t & value)440 bool GetIntFromJson(const CJson *jsonObj, const std::string &key, int32_t &value)
441 {
442 if (jsonObj == nullptr || key.empty()) {
443 return false;
444 }
445
446 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
447 if (IsNumber(item)) {
448 value = static_cast<int32_t>(cJSON_GetNumberValue(item));
449 return true;
450 }
451 if (IsString(item)) {
452 std::string str = cJSON_GetStringValue(item);
453 if (str.empty()) {
454 return false;
455 }
456 value = static_cast<int32_t>(strtol(str.c_str(), nullptr, DECIMALISM));
457 return true;
458 }
459 return false;
460 }
461
GetIntFromJson(const CJsonUnique & jsonObj,const std::string & key,int32_t & value)462 bool GetIntFromJson(const CJsonUnique &jsonObj, const std::string &key, int32_t &value)
463 {
464 return GetIntFromJson(jsonObj.get(), key, value);
465 }
466
GetUint64FromJson(const CJson * jsonObj,const std::string & key,uint64_t & value)467 bool GetUint64FromJson(const CJson *jsonObj, const std::string &key, uint64_t &value)
468 {
469 if (!IsKeyExist(jsonObj, key)) {
470 return false;
471 }
472 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
473 if (item == nullptr) {
474 return false;
475 }
476 if (IsString(item)) {
477 std::string str = cJSON_GetStringValue(item);
478 if (str.empty()) {
479 return false;
480 }
481 value = strtoull(str.c_str(), nullptr, DECIMALISM);
482 return true;
483 }
484 if (IsNumber(item)) {
485 value = static_cast<uint64_t>(cJSON_GetNumberValue(item));
486 return true;
487 }
488
489 return false;
490 }
491
GetInt64FromJson(const CJson * jsonObj,const std::string & key,int64_t & value)492 bool GetInt64FromJson(const CJson *jsonObj, const std::string &key, int64_t &value)
493 {
494 if (!IsKeyExist(jsonObj, key)) {
495 return false;
496 }
497 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
498 if (item == nullptr) {
499 return false;
500 }
501 if (IsString(item)) {
502 std::string str = cJSON_GetStringValue(item);
503 if (str.empty()) {
504 return false;
505 }
506 value = static_cast<int64_t>(strtoull(str.c_str(), nullptr, DECIMALISM));
507 return true;
508 }
509 if (IsNumber(item)) {
510 value = static_cast<int64_t>(cJSON_GetNumberValue(item));
511 return true;
512 }
513
514 return false;
515 }
516
GetBoolFromJson(const CJson * jsonObj,const std::string & key,bool & value)517 bool GetBoolFromJson(const CJson *jsonObj, const std::string &key, bool &value)
518 {
519 if (jsonObj == nullptr || key.empty()) {
520 return false;
521 }
522
523 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
524 if (IsBool(item)) {
525 value = cJSON_IsTrue(item) != 0;
526 return true;
527 }
528 return false;
529 }
530
GetBoolFromJson(const CJsonUnique & jsonObj,const std::string & key,bool & value)531 bool GetBoolFromJson(const CJsonUnique &jsonObj, const std::string &key, bool &value)
532 {
533 return GetBoolFromJson(jsonObj.get(), key, value);
534 }
535
GetBoolFromJson(const CJson * jsonObj,const std::string & key)536 bool GetBoolFromJson(const CJson *jsonObj, const std::string &key)
537 {
538 bool value = false;
539 GetBoolFromJson(jsonObj, key, value);
540 return value;
541 }
542
GetBoolFromJson(const CJsonUnique & jsonObj,const std::string & key)543 bool GetBoolFromJson(const CJsonUnique &jsonObj, const std::string &key)
544 {
545 return GetBoolFromJson(jsonObj.get(), key);
546 }
547
AddObjToJson(CJson * jsonObj,const std::string & key,const CJson * childObj)548 bool AddObjToJson(CJson *jsonObj, const std::string &key, const CJson *childObj)
549 {
550 if (jsonObj == nullptr || key.empty() || childObj == nullptr) {
551 return false;
552 }
553
554 CJson *tmpObj = cJSON_Duplicate(childObj, RECURSE_FLAG_TRUE);
555 if (tmpObj == nullptr) {
556 return false;
557 }
558
559 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
560 if (item == nullptr) {
561 if (!cJSON_AddItemToObject(jsonObj, key.c_str(), tmpObj)) {
562 cJSON_Delete(tmpObj);
563 return false;
564 }
565 } else {
566 if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmpObj)) {
567 cJSON_Delete(tmpObj);
568 return false;
569 }
570 }
571 return true;
572 }
573
AddObjToJson(CJsonUnique & jsonObj,const std::string & key,CJsonUnique & childObj)574 bool AddObjToJson(CJsonUnique &jsonObj, const std::string &key, CJsonUnique &childObj)
575 {
576 return AddObjToJson(jsonObj.get(), key, childObj.get());
577 }
578
AddVectorUint8ToJson(CJson * jsonObj,const std::string & key,std::vector<uint8_t> arr)579 bool AddVectorUint8ToJson(CJson *jsonObj, const std::string &key, std::vector<uint8_t> arr)
580 {
581 if (jsonObj == nullptr || key.empty()) {
582 return false;
583 }
584 CJson *array = cJSON_CreateArray();
585 if (array == nullptr) {
586 return false;
587 }
588 for (size_t i = 0; i < arr.size(); i++) {
589 cJSON_AddItemToArray(array, cJSON_CreateNumber(arr[i]));
590 }
591
592 CJson *tmpObj = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
593 if (tmpObj == nullptr) {
594 if (!cJSON_AddItemToObject(jsonObj, key.c_str(), array)) {
595 cJSON_Delete(array);
596 return false;
597 }
598 } else {
599 if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), array)) {
600 cJSON_Delete(array);
601 return false;
602 }
603 }
604 return true;
605 }
606
AddVectorUint8ToJson(CJsonUnique & jsonObj,const std::string & key,std::vector<uint8_t> arr)607 bool AddVectorUint8ToJson(CJsonUnique &jsonObj, const std::string &key, std::vector<uint8_t> arr)
608 {
609 return AddVectorUint8ToJson(jsonObj.get(), key, arr);
610 }
611
AddObjToArray(CJson * jsonArr,CJson * item)612 bool AddObjToArray(CJson *jsonArr, CJson *item)
613 {
614 if (!IsArray(jsonArr) || item == nullptr) {
615 return false;
616 }
617
618 CJson *tmpObj = cJSON_Duplicate(item, RECURSE_FLAG_TRUE);
619 if (tmpObj == nullptr) {
620 return false;
621 }
622 if (!cJSON_AddItemToArray(jsonArr, tmpObj)) {
623 cJSON_Delete(tmpObj);
624 return false;
625 }
626 return true;
627 }
628
AddObjToArray(CJsonUnique & jsonArr,CJsonUnique & item)629 bool AddObjToArray(CJsonUnique &jsonArr, CJsonUnique &item)
630 {
631 return AddObjToArray(jsonArr.get(), item.get());
632 }
633
AddStringToArray(CJson * jsonArr,const char * string)634 bool AddStringToArray(CJson *jsonArr, const char *string)
635 {
636 if (!IsArray(jsonArr) || string == nullptr) {
637 return false;
638 }
639
640 CJson *strObj = cJSON_CreateString(string);
641 if (strObj == nullptr) {
642 return false;
643 }
644 if (!cJSON_AddItemToArray(jsonArr, strObj)) {
645 cJSON_Delete(strObj);
646 return false;
647 }
648 return true;
649 }
650
AddStringToArray(CJsonUnique & jsonArr,const char * string)651 bool AddStringToArray(CJsonUnique &jsonArr, const char *string)
652 {
653 return AddStringToArray(jsonArr.get(), string);
654 }
655
AddStringToJson(CJson * jsonObj,const std::string & key,const std::string & value)656 bool AddStringToJson(CJson *jsonObj, const std::string &key, const std::string &value)
657 {
658 if (jsonObj == nullptr || key.empty()) {
659 return false;
660 }
661
662 CJson *tmpObj = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
663 if (tmpObj == nullptr) {
664 if (cJSON_AddStringToObject(jsonObj, key.c_str(), value.c_str()) == nullptr) {
665 return false;
666 }
667 } else {
668 CJson *tmp = cJSON_CreateString(value.c_str());
669 if (tmp == nullptr) {
670 return false;
671 }
672 if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) {
673 cJSON_Delete(tmp);
674 return false;
675 }
676 }
677
678 return true;
679 }
680
AddStringToJson(CJsonUnique & jsonObj,const std::string & key,const std::string & value)681 bool AddStringToJson(CJsonUnique &jsonObj, const std::string &key, const std::string &value)
682 {
683 return AddStringToJson(jsonObj.get(), key, value);
684 }
685
AddBoolToJson(CJson * jsonObj,const std::string & key,const bool value)686 bool AddBoolToJson(CJson *jsonObj, const std::string &key, const bool value)
687 {
688 if (jsonObj == nullptr || key.empty()) {
689 return false;
690 }
691
692 CJson *tmpObj = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
693 if (tmpObj == nullptr) {
694 if (cJSON_AddBoolToObject(jsonObj, key.c_str(), value) == nullptr) {
695 return false;
696 }
697 } else {
698 CJson *tmp = cJSON_CreateBool(value);
699 if (tmp == nullptr) {
700 return false;
701 }
702 if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) {
703 cJSON_Delete(tmp);
704 return false;
705 }
706 }
707
708 return true;
709 }
710
AddBoolToJson(CJsonUnique & jsonObj,const std::string & key,const bool value)711 bool AddBoolToJson(CJsonUnique &jsonObj, const std::string &key, const bool value)
712 {
713 return AddBoolToJson(jsonObj.get(), key, value);
714 }
715
AddIntToJson(CJson * jsonObj,const std::string & key,const int value)716 bool AddIntToJson(CJson *jsonObj, const std::string &key, const int value)
717 {
718 if (jsonObj == nullptr || key.empty()) {
719 return false;
720 }
721
722 CJson *tmpObj = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
723 if (tmpObj == nullptr) {
724 if (cJSON_AddNumberToObject(jsonObj, key.c_str(), value) == nullptr) {
725 return false;
726 }
727 } else {
728 CJson *tmp = cJSON_CreateNumber(value);
729 if (tmp == nullptr) {
730 return false;
731 }
732 if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) {
733 cJSON_Delete(tmp);
734 return false;
735 }
736 }
737
738 return true;
739 }
740
AddIntToJson(CJsonUnique & jsonObj,const std::string & key,const int value)741 bool AddIntToJson(CJsonUnique &jsonObj, const std::string &key, const int value)
742 {
743 return AddIntToJson(jsonObj.get(), key, value);
744 }
745
AddUint64ToJson(CJson * jsonObj,const std::string & key,uint64_t value)746 bool AddUint64ToJson(CJson *jsonObj, const std::string &key, uint64_t value)
747 {
748 if (jsonObj == nullptr || key.empty()) {
749 return false;
750 }
751 char buffer[65] = {0};
752 if (sprintf_s(buffer, sizeof(buffer), "%" PRIu64, value) <= 0) {
753 return false;
754 }
755
756 return AddStringToJson(jsonObj, key, buffer);
757 }
758
AddUint64ToJson(CJsonUnique & jsonObj,const std::string & key,uint64_t value)759 bool AddUint64ToJson(CJsonUnique &jsonObj, const std::string &key, uint64_t value)
760 {
761 return AddUint64ToJson(jsonObj.get(), key, value);
762 }
763
AddInt64ToJson(CJson * jsonObj,const std::string & key,int64_t value)764 bool AddInt64ToJson(CJson *jsonObj, const std::string &key, int64_t value)
765 {
766 if (jsonObj == nullptr || key.empty()) {
767 return false;
768 }
769 char buffer[65] = {0};
770 if (sprintf_s(buffer, sizeof(buffer), "%" PRId64, value) <= 0) {
771 return false;
772 }
773
774 return AddStringToJson(jsonObj, key, buffer);
775 }
776
AddInt64ToJson(CJsonUnique & jsonObj,const std::string & key,int64_t value)777 bool AddInt64ToJson(CJsonUnique &jsonObj, const std::string &key, int64_t value)
778 {
779 return AddInt64ToJson(jsonObj.get(), key, value);
780 }
781
CreateJsonString(const char * string)782 CJsonUnique CreateJsonString(const char *string)
783 {
784 cJSON *rawPtr = cJSON_CreateString(string);
785 CJsonUnique uniquePtr(rawPtr, FreeJson);
786 return uniquePtr;
787 }
788
GetJsonNumberValue(const CJsonUnique & item)789 double GetJsonNumberValue(const CJsonUnique &item)
790 {
791 return GetJsonNumberValue(item.get());
792 }
793
GetJsonNumberValue(cJSON * item)794 double GetJsonNumberValue(cJSON *item)
795 {
796 if (item == nullptr) {
797 return static_cast<double>(0);
798 }
799 return cJSON_GetNumberValue(item);
800 }
801
GetJsonArrayFromJson(const CJson * jsonObj,const std::string & key)802 CJson *GetJsonArrayFromJson(const CJson *jsonObj, const std::string &key)
803 {
804 if (jsonObj == nullptr || key.empty()) {
805 return nullptr;
806 }
807 CJson *item = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
808 if (IsArray(item)) {
809 return item;
810 }
811 return nullptr;
812 }
813 } // namespace AccountSA
814 } // namespace OHOS
815