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 "utils_json.h"
17
18 #include "cJSON.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
CreateJson(const char * data)24 JsonHandle CreateJson(const char *data)
25 {
26 cJSON *root = NULL;
27
28 if (data != NULL) {
29 root = cJSON_Parse(data);
30 } else {
31 root = cJSON_CreateObject();
32 }
33 return (void *)root;
34 }
35
DestroyJson(JsonHandle handle)36 void DestroyJson(JsonHandle handle)
37 {
38 if (handle != NULL) {
39 cJSON_Delete((cJSON *)handle);
40 }
41 }
42
GetJsonFieldInt(JsonHandle handle,const char * field)43 int32_t GetJsonFieldInt(JsonHandle handle, const char *field)
44 {
45 int32_t ret = -1;
46
47 if (handle == NULL) {
48 return ret;
49 }
50
51 if (field == NULL) {
52 return ((cJSON *)handle)->valueint;
53 }
54
55 cJSON *objValue = NULL;
56
57 do {
58 objValue = (cJSON *)GetJsonFieldJson(handle, field);
59 if (objValue == NULL) {
60 break;
61 }
62 if (!cJSON_IsNumber(objValue)) {
63 break;
64 }
65 ret = objValue->valueint;
66 } while (0);
67
68 return ret;
69 }
70
GetJsonFieldIntArray(JsonHandle handle,const char * field,int32_t * array,int32_t arrayLen)71 uint32_t GetJsonFieldIntArray(JsonHandle handle, const char *field, int32_t *array, int32_t arrayLen)
72 {
73 if (handle == NULL || field == NULL || array == NULL) {
74 return 0;
75 }
76
77 cJSON *objValue = cJSON_GetObjectItem(handle, field);
78 if (objValue == NULL) {
79 return 0;
80 }
81 if (!cJSON_IsArray(objValue)) {
82 return 0;
83 }
84
85 int size = cJSON_GetArraySize(objValue);
86 if (size > arrayLen) {
87 size = arrayLen;
88 }
89 uint32_t index = 0;
90 for (int32_t i = 0; i < size; i++) {
91 cJSON *item = cJSON_GetArrayItem(objValue, i);
92 if (!cJSON_IsNumber(item)) {
93 continue;
94 }
95 array[index++] = item->valueint;
96 }
97
98 return index;
99 }
100
AddFieldBoolToJson(JsonHandle handle,const char * field,bool value)101 void AddFieldBoolToJson(JsonHandle handle, const char *field, bool value)
102 {
103 if (handle == NULL || field == NULL) {
104 return;
105 }
106 (void)cJSON_AddBoolToObject((cJSON *)handle, field, value);
107 }
108
GetJsonFieldString(JsonHandle handle,const char * field)109 const char *GetJsonFieldString(JsonHandle handle, const char *field)
110 {
111 if (handle == NULL) {
112 return NULL;
113 }
114 if (field == NULL) {
115 return ((cJSON *)handle)->valuestring;
116 }
117 cJSON *objValue = NULL;
118 const char *payload = NULL;
119
120 do {
121 objValue = (cJSON *)GetJsonFieldJson(handle, field);
122 if (objValue == NULL) {
123 break;
124 }
125 payload = cJSON_GetStringValue(objValue);
126 if (payload == NULL) {
127 break;
128 }
129 } while (0);
130 return payload;
131 }
132
GetJsonFieldJson(JsonHandle handle,const char * field)133 JsonHandle GetJsonFieldJson(JsonHandle handle, const char *field)
134 {
135 return cJSON_GetObjectItem((cJSON *)handle, field);
136 }
137
GetJsonFieldJsonArray(JsonHandle handle,uint32_t num)138 JsonHandle GetJsonFieldJsonArray(JsonHandle handle, uint32_t num)
139 {
140 return cJSON_GetArrayItem((cJSON *)handle, num);
141 }
142
GetJsonFieldJsonArraySize(JsonHandle handle)143 int32_t GetJsonFieldJsonArraySize(JsonHandle handle)
144 {
145 return cJSON_GetArraySize((cJSON *)handle);
146 }
147
AddFieldIntToJson(JsonHandle handle,const char * field,int32_t value)148 void AddFieldIntToJson(JsonHandle handle, const char *field, int32_t value)
149 {
150 if (handle == NULL || field == NULL) {
151 return;
152 }
153 (void)cJSON_AddNumberToObject((cJSON *)handle, field, value);
154 }
155
AddFieldIntArrayToJson(JsonHandle handle,const char * field,const int32_t * array,int32_t arrayLen)156 void AddFieldIntArrayToJson(JsonHandle handle, const char *field, const int32_t *array, int32_t arrayLen)
157 {
158 if (handle == NULL || field == NULL || array == NULL) {
159 return;
160 }
161 cJSON *arrayObj = cJSON_CreateIntArray(array, arrayLen);
162 if (arrayObj == NULL) {
163 return;
164 }
165 (void)cJSON_AddItemToObject((cJSON *)handle, field, arrayObj);
166 }
167
AddFieldStringToJson(JsonHandle handle,const char * field,const char * value)168 void AddFieldStringToJson(JsonHandle handle, const char *field, const char *value)
169 {
170 if (handle == NULL || field == NULL || value == NULL) {
171 return;
172 }
173 (void)cJSON_AddStringToObject((cJSON *)handle, field, value);
174 }
175
AddFieldJsonToJson(JsonHandle handle,const char * field,JsonHandle json)176 void AddFieldJsonToJson(JsonHandle handle, const char *field, JsonHandle json)
177 {
178 if (handle == NULL || field == NULL || json == NULL) {
179 return;
180 }
181 (void)cJSON_AddItemToObject((cJSON *)handle, field, json);
182 }
183
ConvertJsonToString(JsonHandle handle)184 char *ConvertJsonToString(JsonHandle handle)
185 {
186 if (handle != NULL) {
187 char *ret = cJSON_PrintUnformatted((cJSON *)handle);
188 return ret;
189 }
190 return NULL;
191 }
192
CompareJsonData(JsonHandle handleA,JsonHandle handleB,bool caseSensitive)193 bool CompareJsonData(JsonHandle handleA, JsonHandle handleB, bool caseSensitive)
194 {
195 if (handleA == NULL || handleB == NULL) {
196 return false;
197 }
198 return cJSON_Compare(handleA, handleB, caseSensitive);
199 }
200
201 #ifdef __cplusplus
202 }
203 #endif