• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "json_util.h"
16 namespace OHOS::Security::SecurityGuard {
17 namespace JsonUtil {
GetNumberInt32(const cJSON * inJson,const std::string & item,int32_t & ret)18 bool GetNumberInt32(const cJSON *inJson, const std::string &item, int32_t &ret)
19 {
20     if (inJson == nullptr) {
21         return false;
22     }
23     cJSON *keyJson = cJSON_GetObjectItem(inJson, item.c_str());
24     if (keyJson == nullptr || !cJSON_IsNumber(keyJson)) {
25         return false;
26     }
27     double tmp = cJSON_GetNumberValue(keyJson);
28     if (tmp > INT32_MAX || tmp < INT32_MIN) {
29         return false;
30     }
31     ret = static_cast<int32_t>(tmp);
32     return true;
33 }
GetNumberInt64(const cJSON * inJson,const std::string & item,int64_t & ret)34 bool GetNumberInt64(const cJSON *inJson, const std::string &item, int64_t &ret)
35 {
36     if (inJson == nullptr) {
37         return false;
38     }
39     cJSON *keyJson = cJSON_GetObjectItem(inJson, item.c_str());
40     if (keyJson == nullptr || !cJSON_IsNumber(keyJson)) {
41         return false;
42     }
43     ret = static_cast<int64_t>(cJSON_GetNumberValue(keyJson));
44     return true;
45 }
46 
GetString(const cJSON * inJson,const std::string & item,std::string & ret)47 bool GetString(const cJSON *inJson, const std::string &item, std::string &ret)
48 {
49     if (inJson == nullptr) {
50         return false;
51     }
52     cJSON *keyJson = cJSON_GetObjectItem(inJson, item.c_str());
53     if (keyJson == nullptr || !cJSON_IsString(keyJson)) {
54         return false;
55     }
56     char *retValue = cJSON_GetStringValue(keyJson);
57     if (retValue == nullptr) {
58         return false;
59     }
60     ret = retValue;
61     return true;
62 }
63 
GetStringNoKey(const cJSON * inJson,std::string & ret)64 bool GetStringNoKey(const cJSON *inJson, std::string &ret)
65 {
66     if (inJson == nullptr) {
67         return false;
68     }
69 
70     if (!cJSON_IsString(inJson)) {
71         return false;
72     }
73     char *retValue = cJSON_GetStringValue(inJson);
74     if (retValue == nullptr) {
75         return false;
76     }
77     ret = retValue;
78     return true;
79 }
80 
GetBool(const cJSON * inJson,const std::string & item,bool & ret)81 bool GetBool(const cJSON *inJson, const std::string &item, bool &ret)
82 {
83     if (inJson == nullptr) {
84         return false;
85     }
86     cJSON *keyJson = cJSON_GetObjectItem(inJson, item.c_str());
87     if (keyJson == nullptr || !cJSON_IsBool(keyJson)) {
88         return false;
89     }
90     ret = cJSON_IsTrue(keyJson) ? true : false;
91     return true;
92 }
93 
AddString(cJSON * outJson,const std::string & item,const std::string & str)94 bool AddString(cJSON *outJson, const std::string &item, const std::string &str)
95 {
96     if (outJson == nullptr) {
97         return false;
98     }
99     if (cJSON_AddStringToObject(outJson, item.c_str(), str.c_str()) == nullptr) {
100         return false;
101     }
102     return true;
103 }
104 
AddNumberInt32(cJSON * outJson,const std::string & item,const int32_t & num)105 bool AddNumberInt32(cJSON *outJson, const std::string &item, const int32_t &num)
106 {
107     if (outJson == nullptr) {
108         return false;
109     }
110     if (cJSON_AddNumberToObject(outJson, item.c_str(), num) == nullptr) {
111         return false;
112     }
113     return true;
114 }
115 
AddNumberInt64(cJSON * outJson,const std::string & item,const int64_t & num)116 bool AddNumberInt64(cJSON *outJson, const std::string &item, const int64_t &num)
117 {
118     if (outJson == nullptr) {
119         return false;
120     }
121     if (cJSON_AddNumberToObject(outJson, item.c_str(), num) == nullptr) {
122         return false;
123     }
124     return true;
125 }
126 
AddStrArrayInfo(cJSON * object,const std::vector<std::string> & inVector,const char * strKey)127 bool AddStrArrayInfo(cJSON *object, const std::vector<std::string> &inVector, const char *strKey)
128 {
129     cJSON *strJsonArr = cJSON_CreateArray();
130     if (strJsonArr == nullptr) {
131         return false;
132     }
133     for (size_t i = 0; i < inVector.size(); i++) {
134         cJSON *item = cJSON_CreateString(inVector[i].c_str());
135         if (item == nullptr) {
136             cJSON_Delete(strJsonArr);
137             return false;
138         }
139         if (!cJSON_AddItemToArray(strJsonArr, item)) {
140             cJSON_Delete(item);
141             cJSON_Delete(strJsonArr);
142             return false;
143         }
144     }
145     if (!cJSON_AddItemToObject(object, strKey, strJsonArr)) {
146         cJSON_Delete(strJsonArr);
147         return false;
148     }
149     return true;
150 }
151 }
152 } // namespace OHOS::Security::SecurityGuard