• 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 
GetJsonObjectStringItem(const cJSON * json,const char * const string,char * target,uint32_t targetLen)20 bool GetJsonObjectStringItem(const cJSON *json, const char * const string, char *target, uint32_t targetLen)
21 {
22     if (json == NULL || string == NULL || target == NULL) {
23         return false;
24     }
25     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
26     if (item == NULL || !cJSON_IsString(item)) {
27         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
28         return false;
29     }
30     uint32_t length = strlen(item->valuestring);
31     if (length >= targetLen) {
32         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "the length [%d] is to long for [%s]", length, string);
33         return false;
34     }
35     int ret = strcpy_s(target, targetLen, item->valuestring);
36     if (ret != 0) {
37         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "strcpy error %d\n", ret);
38         return false;
39     }
40     return true;
41 }
42 
GetJsonObjectNumberItem(const cJSON * json,const char * const string,int * target)43 bool GetJsonObjectNumberItem(const cJSON *json, const char * const string, int *target)
44 {
45     if (json == NULL || string == NULL || target == NULL) {
46         return false;
47     }
48     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
49     if (item == NULL || !cJSON_IsNumber(item) || (item->valuedouble < 0)) {
50         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
51         return false;
52     }
53     *target = (int)item->valuedouble;
54     return true;
55 }
56 
GetJsonObjectSignedNumberItem(const cJSON * json,const char * const string,int * target)57 bool GetJsonObjectSignedNumberItem(const cJSON *json, const char * const string, int *target)
58 {
59     if (json == NULL || string == NULL || target == NULL) {
60         return false;
61     }
62     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
63     if (item == NULL || !cJSON_IsNumber(item)) {
64         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
65         return false;
66     }
67     *target = (int)item->valuedouble;
68     return true;
69 }
70 
GetJsonObjectDoubleItem(const cJSON * json,const char * const string,double * target)71 bool GetJsonObjectDoubleItem(const cJSON *json, const char * const string, double *target)
72 {
73     if (json == NULL || string == NULL || target == NULL) {
74         return false;
75     }
76     cJSON* item = cJSON_GetObjectItemCaseSensitive(json, string);
77     if (item == NULL || !cJSON_IsNumber(item)) {
78         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
79         return false;
80     }
81     *target = item->valuedouble;
82     return true;
83 }
84 
GetJsonObjectNumber64Item(const cJSON * json,const char * const string,int64_t * target)85 bool GetJsonObjectNumber64Item(const cJSON *json, const char * const string, int64_t *target)
86 {
87     if (json == NULL || string == NULL || target == NULL) {
88         return false;
89     }
90     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
91     if (item == NULL || !cJSON_IsNumber(item) || (item->valuedouble < 0)) {
92         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
93         return false;
94     }
95     *target = (int64_t)item->valuedouble;
96     return true;
97 }
98 
GetJsonObjectInt32Item(const cJSON * json,const char * const string,int32_t * target)99 bool GetJsonObjectInt32Item(const cJSON *json, const char * const string, int32_t *target)
100 {
101     if (json == NULL || string == NULL || target == NULL) {
102         return false;
103     }
104     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
105     if (item == NULL || !cJSON_IsNumber(item)) {
106         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
107         return false;
108     }
109     *target = (int32_t)item->valuedouble;
110     return true;
111 }
112 
GetJsonObjectBoolItem(const cJSON * json,const char * const string,bool * target)113 bool GetJsonObjectBoolItem(const cJSON *json, const char * const string, bool *target)
114 {
115     if (json == NULL || string == NULL || target == NULL) {
116         return false;
117     }
118     cJSON *item = cJSON_GetObjectItemCaseSensitive(json, string);
119     if (item == NULL || !cJSON_IsBool(item)) {
120         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot find or invalid [%s]", string);
121         return false;
122     }
123     *target = (bool)item->valueint;
124     return true;
125 }
126 
AddStringToJsonObject(cJSON * json,const char * const string,const char * value)127 bool AddStringToJsonObject(cJSON *json, const char * const string, const char *value)
128 {
129     if (value == NULL || json == NULL || string == NULL) {
130         return false;
131     }
132     cJSON *item = cJSON_CreateString(value);
133     if (item == NULL) {
134         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot create cJSON string object [%s]", string);
135         return false;
136     }
137     if (!cJSON_AddItemToObject(json, string, item)) {
138         cJSON_Delete(item);
139         return false;
140     }
141     return true;
142 }
143 
AddNumberToJsonObject(cJSON * json,const char * const string,int num)144 bool AddNumberToJsonObject(cJSON *json, const char * const string, int num)
145 {
146     if (json == NULL || string == NULL) {
147         return false;
148     }
149     cJSON *item = cJSON_CreateNumber(num);
150     if (item == NULL) {
151         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot create cJSON number object [%s]", string);
152         return false;
153     }
154     if (!cJSON_AddItemToObject(json, string, item)) {
155         cJSON_Delete(item);
156         return false;
157     }
158     return true;
159 }
160 
AddNumber64ToJsonObject(cJSON * json,const char * const string,int64_t num)161 bool AddNumber64ToJsonObject(cJSON *json, const char * const string, int64_t num)
162 {
163     if (json == NULL || string == NULL) {
164         return false;
165     }
166     cJSON *item = cJSON_CreateNumber(num);
167     if (item == NULL) {
168         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot create cJSON number object [%s]", string);
169         return false;
170     }
171     if (!cJSON_AddItemToObject(json, string, item)) {
172         cJSON_Delete(item);
173         return false;
174     }
175     return true;
176 }
177 
AddBoolToJsonObject(cJSON * json,const char * const string,bool value)178 bool AddBoolToJsonObject(cJSON *json, const char * const string, bool value)
179 {
180     if (json == NULL || string == NULL) {
181         return false;
182     }
183     cJSON *item = cJSON_CreateBool(value);
184     if (item == NULL) {
185         SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Cannot create cJSON bool object [%s]", string);
186         return false;
187     }
188     if (!cJSON_AddItemToObject(json, string, item)) {
189         cJSON_Delete(item);
190         return false;
191     }
192     return true;
193 }