• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "jsonutil.h"
17 #include "mem_stat.h"
18 #include "cJSON.h"
19 
parse_json(const char * data)20 json_handle parse_json(const char *data)
21 {
22     cJSON *root = NULL;
23 
24     if (data != NULL) {
25         root = cJSON_Parse(data);
26     }
27     return (void *)root;
28 }
29 
free_json(json_handle handle)30 void free_json(json_handle handle)
31 {
32     if (handle != NULL) {
33         cJSON_Delete((cJSON *)handle);
34     }
35 }
36 
get_json_obj(json_pobject parent,const char * field)37 json_pobject get_json_obj(json_pobject parent, const char *field)
38 {
39     return cJSON_GetObjectItem((cJSON *)parent, field);
40 }
41 
get_json_int(json_pobject obj,const char * field)42 int32_t get_json_int(json_pobject obj, const char *field)
43 {
44     int32_t ret = -1;
45 
46     if (obj == NULL) {
47         return ret;
48     }
49     if (field == NULL) {
50         return ((cJSON *)obj)->valueint;
51     }
52 
53     cJSON *obj_value = NULL;
54 
55     do {
56         obj_value = (cJSON *)get_json_obj(obj, field);
57         if (obj_value == NULL) {
58             break;
59         }
60         if (!cJSON_IsNumber(obj_value)) {
61             break;
62         }
63         ret = obj_value->valueint;
64     } while (0);
65 
66     return ret;
67 }
68 
get_json_string(json_pobject obj,const char * field)69 const char *get_json_string(json_pobject obj, const char *field)
70 {
71     if (obj == NULL) {
72         return NULL;
73     }
74     if (field == NULL) {
75         return ((cJSON *)obj)->valuestring;
76     }
77     cJSON *obj_value = NULL;
78     const char *payload = NULL;
79 
80     do {
81         obj_value = (cJSON *)get_json_obj(obj, field);
82         if (obj_value == NULL) {
83             break;
84         }
85         payload = cJSON_GetStringValue(obj_value);
86         if (payload == NULL) {
87             break;
88         }
89     } while (0);
90     return payload;
91 }
92 
get_json_bool(json_pobject obj,const char * field)93 int32_t get_json_bool(json_pobject obj, const char *field)
94 {
95     int32_t ret = -1;
96 
97     if (obj == NULL) {
98         return ret;
99     }
100     if (field == NULL) {
101         return ((cJSON *)obj)->valueint;
102     }
103 
104     cJSON *obj_value = NULL;
105 
106     do {
107         obj_value = (cJSON *)get_json_obj(obj, field);
108         if (obj_value == NULL) {
109             break;
110         }
111         if (!cJSON_IsBool(obj_value)) {
112             break;
113         }
114         if (cJSON_IsFalse(obj_value)) {
115             ret = 0;
116         }
117         if (cJSON_IsTrue(obj_value)) {
118             ret = 1;
119         }
120     } while (0);
121     return ret;
122 }
123 
get_array_size(json_pobject obj)124 int32_t get_array_size(json_pobject obj)
125 {
126     if (cJSON_IsArray(obj)) {
127         return cJSON_GetArraySize(obj);
128     }
129     return -1;
130 }
131 
get_array_idx(json_pobject obj,int32_t idx)132 json_pobject get_array_idx(json_pobject obj, int32_t idx)
133 {
134     if (cJSON_IsArray(obj)) {
135         return cJSON_GetArrayItem(obj, idx);
136     }
137     return NULL;
138 }
139 
create_json_object(void)140 json_pobject create_json_object(void)
141 {
142     return cJSON_CreateObject();
143 }
144 
delete_json_object(json_pobject obj)145 void delete_json_object(json_pobject obj)
146 {
147     if (obj != NULL) {
148         cJSON_Delete((cJSON *)obj);
149     }
150 }
151 
add_item_to_object(json_pobject parent,const char * field,json_pobject obj)152 void add_item_to_object(json_pobject parent, const char *field, json_pobject obj)
153 {
154     cJSON_AddItemToObject((cJSON *)parent, field, (cJSON *)obj);
155 }
156 
add_string_to_object(json_pobject parent,const char * field,const char * value)157 json_pobject add_string_to_object(json_pobject parent, const char *field, const char *value)
158 {
159     return cJSON_AddStringToObject((cJSON *)parent, field, value);
160 }
161 
add_number_to_object(json_pobject parent,const char * field,int32_t value)162 json_pobject add_number_to_object(json_pobject parent, const char *field, int32_t value)
163 {
164     return cJSON_AddNumberToObject((cJSON *)parent, field, value);
165 }
166 
add_bool_to_object(json_pobject parent,const char * field,int32_t value)167 json_pobject add_bool_to_object(json_pobject parent, const char *field, int32_t value)
168 {
169     if (value == 0) {
170         return cJSON_AddFalseToObject((cJSON *)parent, field);
171     } else {
172         return cJSON_AddTrueToObject((cJSON *)parent, field);
173     }
174 }
175 
add_object_to_object(json_pobject parent,const char * field)176 json_pobject add_object_to_object(json_pobject parent, const char *field)
177 {
178     return cJSON_AddObjectToObject(parent, field);
179 }
180 
json_to_string(json_pobject obj)181 char *json_to_string(json_pobject obj)
182 {
183     if (obj != NULL) {
184         char *ret = cJSON_PrintUnformatted(obj);
185         return ret;
186     }
187     return NULL;
188 }
189 
free_json_string(char * json_str)190 void free_json_string(char *json_str)
191 {
192     FREE(json_str);
193 }
194 
add_array_to_object(json_pobject parent,const char * field)195 json_pobject add_array_to_object(json_pobject parent, const char *field)
196 {
197     return cJSON_AddArrayToObject(parent, field);
198 }
199 
add_string_to_array(json_pobject objArr,const char * value)200 void add_string_to_array(json_pobject objArr, const char *value)
201 {
202     cJSON_AddItemToArray(objArr, cJSON_CreateString(value));
203 }
204 
add_number_to_array(json_pobject objArr,int32_t value)205 void add_number_to_array(json_pobject objArr, int32_t value)
206 {
207     cJSON_AddItemToArray(objArr, cJSON_CreateNumber(value));
208 }
209 
add_bool_to_array(json_pobject objArr,int32_t value)210 void add_bool_to_array(json_pobject objArr, int32_t value)
211 {
212     cJSON_AddItemToArray(objArr, cJSON_CreateBool(value));
213 }
214 
add_obj_to_array(json_pobject objArr,json_pobject obj)215 void add_obj_to_array(json_pobject objArr, json_pobject obj)
216 {
217     cJSON_AddItemToArray(objArr, obj);
218 }
219