FreeRDP
Loading...
Searching...
No Matches
json-c.c
1
20#include <math.h>
21#include <errno.h>
22
23#include <winpr/file.h>
24#include <winpr/json.h>
25#include <winpr/assert.h>
26
27#if !defined(WITH_JSONC)
28#error "This file must only be compiled when json-c is enabled"
29#endif
30#include <json.h>
31
32#if JSON_C_MAJOR_VERSION == 0
33#if JSON_C_MINOR_VERSION < 14
34static struct json_object* json_object_new_null(void)
35{
36 return NULL;
37}
38#endif
39#endif
40
41int WINPR_JSON_version(char* buffer, size_t len)
42{
43 return _snprintf(buffer, len, "json-c %s", json_c_version());
44}
45
46WINPR_JSON* WINPR_JSON_Parse(const char* value)
47{
48 return json_tokener_parse(value);
49}
50
51WINPR_JSON* WINPR_JSON_ParseWithLength(const char* value, size_t buffer_length)
52{
53 WINPR_ASSERT(buffer_length <= INT_MAX);
54 json_tokener* tok = json_tokener_new();
55 if (!tok)
56 return NULL;
57 json_object* obj = json_tokener_parse_ex(tok, value, (int)buffer_length);
58 json_tokener_free(tok);
59 return obj;
60}
61
62void WINPR_JSON_Delete(WINPR_JSON* item)
63{
64 json_object_put((json_object*)item);
65}
66
67WINPR_JSON* WINPR_JSON_GetArrayItem(const WINPR_JSON* array, size_t index)
68{
69 return json_object_array_get_idx((const json_object*)array, index);
70}
71
72size_t WINPR_JSON_GetArraySize(const WINPR_JSON* array)
73{
74 return json_object_array_length((const json_object*)array);
75}
76
77WINPR_JSON* WINPR_JSON_GetObjectItem(const WINPR_JSON* object, const char* string)
78{
79 struct json_object_iterator it = json_object_iter_begin((const json_object*)object);
80 struct json_object_iterator itEnd = json_object_iter_end((const json_object*)object);
81 while (!json_object_iter_equal(&it, &itEnd))
82 {
83 const char* key = json_object_iter_peek_name(&it);
84 if (_stricmp(key, string) == 0)
85 {
86 return json_object_iter_peek_value(&it);
87 }
88 json_object_iter_next(&it);
89 }
90 return NULL;
91}
92
93WINPR_JSON* WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON* object, const char* string)
94{
95 return json_object_object_get((const json_object*)object, string);
96}
97
98BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON* object, const char* string)
99{
100 return json_object_object_get_ex((const json_object*)object, string, NULL);
101}
102
103const char* WINPR_JSON_GetErrorPtr(void)
104{
105 return json_util_get_last_err();
106}
107
108const char* WINPR_JSON_GetStringValue(WINPR_JSON* item)
109{
110 return json_object_get_string((json_object*)item);
111}
112
113double WINPR_JSON_GetNumberValue(const WINPR_JSON* item)
114{
115 return json_object_get_double((const json_object*)item);
116}
117
118BOOL WINPR_JSON_IsInvalid(const WINPR_JSON* item)
119{
120 if (WINPR_JSON_IsArray(item))
121 return FALSE;
122 if (WINPR_JSON_IsObject(item))
123 return FALSE;
124 if (WINPR_JSON_IsNull(item))
125 return FALSE;
126 if (WINPR_JSON_IsNumber(item))
127 return FALSE;
128 if (WINPR_JSON_IsBool(item))
129 return FALSE;
130 if (WINPR_JSON_IsString(item))
131 return FALSE;
132 return TRUE;
133}
134
135BOOL WINPR_JSON_IsFalse(const WINPR_JSON* item)
136{
137 if (!json_object_is_type((const json_object*)item, json_type_boolean))
138 return FALSE;
139 json_bool val = json_object_get_boolean((const json_object*)item);
140 return val == 0;
141}
142
143BOOL WINPR_JSON_IsTrue(const WINPR_JSON* item)
144{
145 if (!json_object_is_type((const json_object*)item, json_type_boolean))
146 return FALSE;
147 json_bool val = json_object_get_boolean((const json_object*)item);
148 return val != 0;
149}
150
151BOOL WINPR_JSON_IsBool(const WINPR_JSON* item)
152{
153 return json_object_is_type((const json_object*)item, json_type_boolean);
154}
155
156BOOL WINPR_JSON_IsNull(const WINPR_JSON* item)
157{
158 return json_object_is_type((const json_object*)item, json_type_null);
159}
160
161BOOL WINPR_JSON_IsNumber(const WINPR_JSON* item)
162{
163 return json_object_is_type((const json_object*)item, json_type_int) ||
164 json_object_is_type((const json_object*)item, json_type_double);
165}
166
167BOOL WINPR_JSON_IsString(const WINPR_JSON* item)
168{
169 return json_object_is_type((const json_object*)item, json_type_string);
170}
171
172BOOL WINPR_JSON_IsArray(const WINPR_JSON* item)
173{
174 return json_object_is_type((const json_object*)item, json_type_array);
175}
176
177BOOL WINPR_JSON_IsObject(const WINPR_JSON* item)
178{
179 return json_object_is_type((const json_object*)item, json_type_object);
180}
181
182WINPR_JSON* WINPR_JSON_CreateNull(void)
183{
184 return json_object_new_null();
185}
186
187WINPR_JSON* WINPR_JSON_CreateTrue(void)
188{
189 return json_object_new_boolean(TRUE);
190}
191
192WINPR_JSON* WINPR_JSON_CreateFalse(void)
193{
194 return json_object_new_boolean(FALSE);
195}
196
197WINPR_JSON* WINPR_JSON_CreateBool(BOOL boolean)
198{
199 return json_object_new_boolean(boolean);
200}
201
202WINPR_JSON* WINPR_JSON_CreateNumber(double num)
203{
204 return json_object_new_double(num);
205}
206
207WINPR_JSON* WINPR_JSON_CreateString(const char* string)
208{
209 return json_object_new_string(string);
210}
211
212WINPR_JSON* WINPR_JSON_CreateArray(void)
213{
214 return json_object_new_array();
215}
216
217WINPR_JSON* WINPR_JSON_CreateObject(void)
218{
219 return json_object_new_object();
220}
221
222WINPR_JSON* WINPR_JSON_AddNullToObject(WINPR_JSON* object, const char* name)
223{
224 struct json_object* obj = json_object_new_null();
225 if (json_object_object_add((json_object*)object, name, obj) != 0)
226 {
227 json_object_put(obj);
228 return NULL;
229 }
230 return obj;
231}
232
233WINPR_JSON* WINPR_JSON_AddTrueToObject(WINPR_JSON* object, const char* name)
234{
235 struct json_object* obj = json_object_new_boolean(TRUE);
236 if (json_object_object_add((json_object*)object, name, obj) != 0)
237 {
238 json_object_put(obj);
239 return NULL;
240 }
241 return obj;
242}
243
244WINPR_JSON* WINPR_JSON_AddFalseToObject(WINPR_JSON* object, const char* name)
245{
246 struct json_object* obj = json_object_new_boolean(FALSE);
247 if (json_object_object_add((json_object*)object, name, obj) != 0)
248 {
249 json_object_put(obj);
250 return NULL;
251 }
252 return obj;
253}
254
255WINPR_JSON* WINPR_JSON_AddBoolToObject(WINPR_JSON* object, const char* name, BOOL boolean)
256{
257 struct json_object* obj = json_object_new_boolean(boolean);
258 if (json_object_object_add((json_object*)object, name, obj) != 0)
259 {
260 json_object_put(obj);
261 return NULL;
262 }
263 return obj;
264}
265
266WINPR_JSON* WINPR_JSON_AddNumberToObject(WINPR_JSON* object, const char* name, double number)
267{
268 struct json_object* obj = json_object_new_double(number);
269 if (json_object_object_add((json_object*)object, name, obj) != 0)
270 {
271 json_object_put(obj);
272 return NULL;
273 }
274 return obj;
275}
276
277WINPR_JSON* WINPR_JSON_AddStringToObject(WINPR_JSON* object, const char* name, const char* string)
278{
279 struct json_object* obj = json_object_new_string(string);
280 if (json_object_object_add((json_object*)object, name, obj) != 0)
281 {
282 json_object_put(obj);
283 return NULL;
284 }
285 return obj;
286}
287
288WINPR_JSON* WINPR_JSON_AddObjectToObject(WINPR_JSON* object, const char* name)
289{
290 struct json_object* obj = json_object_new_object();
291 if (json_object_object_add((json_object*)object, name, obj) != 0)
292 {
293 json_object_put(obj);
294 return NULL;
295 }
296 return obj;
297}
298
299BOOL WINPR_JSON_AddItemToArray(WINPR_JSON* array, WINPR_JSON* item)
300{
301 const int rc = json_object_array_add((json_object*)array, (json_object*)item);
302 if (rc != 0)
303 return FALSE;
304 return TRUE;
305}
306
307WINPR_JSON* WINPR_JSON_AddArrayToObject(WINPR_JSON* object, const char* name)
308{
309 struct json_object* obj = json_object_new_array();
310 if (json_object_object_add((json_object*)object, name, obj) != 0)
311 {
312 json_object_put(obj);
313 return NULL;
314 }
315 return obj;
316}
317
318char* WINPR_JSON_Print(WINPR_JSON* item)
319{
320 const char* str = json_object_to_json_string_ext((json_object*)item, JSON_C_TO_STRING_PRETTY);
321 if (!str)
322 return NULL;
323 return _strdup(str);
324}
325
326char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item)
327{
328 const char* str = json_object_to_json_string_ext((json_object*)item, JSON_C_TO_STRING_PLAIN);
329 if (!str)
330 return NULL;
331 return _strdup(str);
332}
WINPR_JSON * WINPR_JSON_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
Definition json-c.c:197
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
Definition json-c.c:207
BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
Definition json-c.c:98
WINPR_JSON * WINPR_JSON_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
Definition json-c.c:266
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
Definition json-c.c:156
WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition json-c.c:77
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition json-c.c:167
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
Definition json-c.c:299
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
Definition json-c.c:307
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition json-c.c:151
double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition json-c.c:113
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
Definition json-c.c:233
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
Definition json-c.c:217
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
Definition json-c.c:212
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
Definition json-c.c:41
char * WINPR_JSON_Print(WINPR_JSON *item)
Serialize a JSON instance to string for minimal size without formatting see WINPR_JSON_PrintUnformatt...
Definition json-c.c:318
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
Definition json-c.c:244
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition json-c.c:161
WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition json-c.c:67
WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
Definition json-c.c:93
WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
Definition json-c.c:277
WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
Definition json-c.c:51
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
Definition json-c.c:192
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
Definition json-c.c:202
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition json-c.c:177
WINPR_JSON * WINPR_JSON_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
Definition json-c.c:255
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *item)
Check if JSON item is valid.
Definition json-c.c:118
char * WINPR_JSON_PrintUnformatted(WINPR_JSON *item)
Serialize a JSON instance to string without formatting for human readable formatted output see WINPR_...
Definition json-c.c:326
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
Definition json-c.c:182
const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition json-c.c:108
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
Definition json-c.c:222
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
Definition json-c.c:187
BOOL WINPR_JSON_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
Definition json-c.c:135
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition json-c.c:62
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition json-c.c:72
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition json-c.c:172
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
Definition json-c.c:103
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
Definition json-c.c:288
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition json-c.c:46
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition json-c.c:143