• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "softbus_json_utils.h"
17 
18 #include <securec.h>
19 #include "softbus_def.h"
20 #include "softbus_error_code.h"
21 
GetStringItemByJsonObject(const cJSON * json,const char * const string,char * target,uint32_t targetLen)22 int32_t GetStringItemByJsonObject(const cJSON *json, const char * const string, char *target,
23     uint32_t targetLen)
24 {
25     if (json == NULL || string == NULL || target == NULL) {
26         return SOFTBUS_ERR;
27     }
28     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
29     if (item == NULL || !cJSON_IsString(item)) {
30         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
31         return SOFTBUS_ERR;
32     }
33     uint32_t length = strlen(item->valuestring);
34     if (length >= targetLen) {
35         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "the length [%d] is to long for [%s]", length, string);
36         return SOFTBUS_INVALID_PARAM;
37     }
38     int ret = strcpy_s(target, targetLen, item->valuestring);
39     if (ret != 0) {
40         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "strcpy error %d\n", ret);
41         return SOFTBUS_ERR;
42     }
43     return SOFTBUS_OK;
44 }
45 
GetJsonObjectStringItem(const cJSON * json,const char * const string,char * target,uint32_t targetLen)46 bool GetJsonObjectStringItem(const cJSON *json, const char * const string, char *target,
47     uint32_t targetLen)
48 {
49     if (json == NULL || string == NULL || target == NULL) {
50         return false;
51     }
52     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
53     if (item == NULL || !cJSON_IsString(item)) {
54         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
55         return false;
56     }
57     uint32_t length = strlen(item->valuestring);
58     if (length >= targetLen) {
59         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "the length [%d] is to long for [%s]", length, string);
60         return false;
61     }
62     int ret = strcpy_s(target, targetLen, item->valuestring);
63     if (ret != 0) {
64         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "strcpy error %d\n", ret);
65         return false;
66     }
67     return true;
68 }
69 
GetJsonObjectNumberItem(const cJSON * json,const char * const string,int * target)70 bool GetJsonObjectNumberItem(const cJSON *json, const char * const string, int *target)
71 {
72     if (json == NULL || string == NULL || target == NULL) {
73         return false;
74     }
75     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
76     if (item == NULL || !cJSON_IsNumber(item) || (item->valuedouble < 0)) {
77         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
78         return false;
79     }
80     *target = (int)item->valuedouble;
81     return true;
82 }
83 
GetJsonObjectSignedNumberItem(const cJSON * json,const char * const string,int * target)84 bool GetJsonObjectSignedNumberItem(const cJSON *json, const char * const string, int *target)
85 {
86     if (json == NULL || string == NULL || target == NULL) {
87         return false;
88     }
89     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
90     if (item == NULL || !cJSON_IsNumber(item)) {
91         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
92         return false;
93     }
94     *target = (int)item->valuedouble;
95     return true;
96 }
97 
GetJsonObjectDoubleItem(const cJSON * json,const char * const string,double * target)98 bool GetJsonObjectDoubleItem(const cJSON *json, const char * const string, double *target)
99 {
100     if (json == NULL || string == NULL || target == NULL) {
101         return false;
102     }
103     cJSON* item = cJSON_GetObjectItemCaseSensitive(json, string);
104     if (item == NULL || !cJSON_IsNumber(item)) {
105         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
106         return false;
107     }
108     *target = item->valuedouble;
109     return true;
110 }
111 
GetJsonObjectNumber16Item(const cJSON * json,const char * const string,uint16_t * target)112 bool GetJsonObjectNumber16Item(const cJSON *json, const char * const string, uint16_t *target)
113 {
114     if (json == NULL || string == NULL || target == NULL) {
115         return false;
116     }
117     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
118     if (item == NULL || !cJSON_IsNumber(item) || (item->valuedouble < 0)) {
119         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
120         return false;
121     }
122     *target = (uint16_t)item->valuedouble;
123     return true;
124 }
125 
GetJsonObjectNumber64Item(const cJSON * json,const char * const string,int64_t * target)126 bool GetJsonObjectNumber64Item(const cJSON *json, const char * const string, int64_t *target)
127 {
128     if (json == NULL || string == NULL || target == NULL) {
129         return false;
130     }
131     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
132     if (item == NULL || !cJSON_IsNumber(item) || (item->valuedouble < 0)) {
133         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
134         return false;
135     }
136     *target = (int64_t)item->valuedouble;
137     return true;
138 }
139 
GetJsonObjectSignedNumber64Item(const cJSON * json,const char * const string,int64_t * target)140 bool GetJsonObjectSignedNumber64Item(const cJSON *json, const char * const string, int64_t *target)
141 {
142     if (json == NULL || string == NULL || target == NULL) {
143         return false;
144     }
145     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
146     if (item == NULL || !cJSON_IsNumber(item)) {
147         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
148         return false;
149     }
150     *target = (int64_t)item->valuedouble;
151     return true;
152 }
153 
GetJsonObjectInt32Item(const cJSON * json,const char * const string,int32_t * target)154 bool GetJsonObjectInt32Item(const cJSON *json, const char * const string, int32_t *target)
155 {
156     if (json == NULL || string == NULL || target == NULL) {
157         return false;
158     }
159     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
160     if (item == NULL || !cJSON_IsNumber(item)) {
161         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
162         return false;
163     }
164     *target = (int32_t)item->valuedouble;
165     return true;
166 }
167 
GetJsonObjectBoolItem(const cJSON * json,const char * const string,bool * target)168 bool GetJsonObjectBoolItem(const cJSON *json, const char * const string, bool *target)
169 {
170     if (json == NULL || string == NULL || target == NULL) {
171         return false;
172     }
173     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
174     if (item == NULL || !cJSON_IsBool(item)) {
175         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
176         return false;
177     }
178     *target = (bool)item->valueint;
179     return true;
180 }
181 
AddStringToJsonObject(cJSON * json,const char * const string,const char * value)182 bool AddStringToJsonObject(cJSON *json, const char * const string, const char *value)
183 {
184     if (value == NULL || json == NULL || string == NULL) {
185         return false;
186     }
187     cJSON *item = cJSON_CreateString(value);
188     if (item == NULL) {
189         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot create cJSON string object [%s]", string);
190         return false;
191     }
192     if (!cJSON_AddItemToObject(json, string, item)) {
193         cJSON_Delete(item);
194         return false;
195     }
196     return true;
197 }
198 
AddNumberToJsonObject(cJSON * json,const char * const string,int num)199 bool AddNumberToJsonObject(cJSON *json, const char * const string, int num)
200 {
201     if (json == NULL || string == NULL) {
202         return false;
203     }
204     cJSON *item = cJSON_CreateNumber(num);
205     if (item == NULL) {
206         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot create cJSON number object [%s]", string);
207         return false;
208     }
209     if (!cJSON_AddItemToObject(json, string, item)) {
210         cJSON_Delete(item);
211         return false;
212     }
213     return true;
214 }
215 
AddNumber64ToJsonObject(cJSON * json,const char * const string,int64_t num)216 bool AddNumber64ToJsonObject(cJSON *json, const char * const string, int64_t num)
217 {
218     if (json == NULL || string == NULL) {
219         return false;
220     }
221     cJSON *item = cJSON_CreateNumber(num);
222     if (item == NULL) {
223         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot create cJSON number object [%s]", string);
224         return false;
225     }
226     if (!cJSON_AddItemToObject(json, string, item)) {
227         cJSON_Delete(item);
228         return false;
229     }
230     return true;
231 }
232 
AddBoolToJsonObject(cJSON * json,const char * const string,bool value)233 bool AddBoolToJsonObject(cJSON *json, const char * const string, bool value)
234 {
235     if (json == NULL || string == NULL) {
236         return false;
237     }
238     cJSON *item = cJSON_CreateBool(value);
239     if (item == NULL) {
240         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot create cJSON bool object [%s]", string);
241         return false;
242     }
243     if (!cJSON_AddItemToObject(json, string, item)) {
244         cJSON_Delete(item);
245         return false;
246     }
247     return true;
248 }