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 nullptr;
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 nullptr;
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 nullptr;
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, nullptr);
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
118INT64 WINPR_JSON_GetIntegerValue(const WINPR_JSON* item)
119{
120 return json_object_get_int64(item);
121}
122
123UINT64 WINPR_JSON_GetUIntegerValue(const WINPR_JSON* item)
124{
125 return json_object_get_uint64(item);
126}
127
128BOOL WINPR_JSON_IsInvalid(const WINPR_JSON* item)
129{
130 if (WINPR_JSON_IsArray(item))
131 return FALSE;
132 if (WINPR_JSON_IsObject(item))
133 return FALSE;
134 if (WINPR_JSON_IsNull(item))
135 return FALSE;
136 if (WINPR_JSON_IsNumber(item))
137 return FALSE;
138 if (WINPR_JSON_IsBool(item))
139 return FALSE;
140 if (WINPR_JSON_IsString(item))
141 return FALSE;
142 return TRUE;
143}
144
145BOOL WINPR_JSON_IsFalse(const WINPR_JSON* item)
146{
147 if (!json_object_is_type((const json_object*)item, json_type_boolean))
148 return FALSE;
149 json_bool val = json_object_get_boolean((const json_object*)item);
150 return val == 0;
151}
152
153BOOL WINPR_JSON_IsTrue(const WINPR_JSON* item)
154{
155 if (!json_object_is_type((const json_object*)item, json_type_boolean))
156 return FALSE;
157 json_bool val = json_object_get_boolean((const json_object*)item);
158 return val != 0;
159}
160
161BOOL WINPR_JSON_IsBool(const WINPR_JSON* item)
162{
163 return json_object_is_type((const json_object*)item, json_type_boolean);
164}
165
166BOOL WINPR_JSON_IsNull(const WINPR_JSON* item)
167{
168 return json_object_is_type((const json_object*)item, json_type_null);
169}
170
171BOOL WINPR_JSON_IsNumber(const WINPR_JSON* item)
172{
173 return json_object_is_type((const json_object*)item, json_type_int) ||
174 json_object_is_type((const json_object*)item, json_type_double);
175}
176
177BOOL WINPR_JSON_IsString(const WINPR_JSON* item)
178{
179 return json_object_is_type((const json_object*)item, json_type_string);
180}
181
182BOOL WINPR_JSON_IsArray(const WINPR_JSON* item)
183{
184 return json_object_is_type((const json_object*)item, json_type_array);
185}
186
187BOOL WINPR_JSON_IsObject(const WINPR_JSON* item)
188{
189 return json_object_is_type((const json_object*)item, json_type_object);
190}
191
192WINPR_JSON* WINPR_JSON_CreateNull(void)
193{
194 return json_object_new_null();
195}
196
197WINPR_JSON* WINPR_JSON_CreateTrue(void)
198{
199 return json_object_new_boolean(TRUE);
200}
201
202WINPR_JSON* WINPR_JSON_CreateFalse(void)
203{
204 return json_object_new_boolean(FALSE);
205}
206
207WINPR_JSON* WINPR_JSON_CreateBool(BOOL boolean)
208{
209 return json_object_new_boolean(boolean);
210}
211
212WINPR_JSON* WINPR_JSON_CreateNumber(double num)
213{
214 return json_object_new_double(num);
215}
216
217WINPR_JSON* WINPR_JSON_CreateString(const char* string)
218{
219 return json_object_new_string(string);
220}
221
222WINPR_JSON* WINPR_JSON_CreateArray(void)
223{
224 return json_object_new_array();
225}
226
227WINPR_JSON* WINPR_JSON_CreateObject(void)
228{
229 return json_object_new_object();
230}
231
232WINPR_JSON* WINPR_JSON_AddNullToObject(WINPR_JSON* object, const char* name)
233{
234 struct json_object* obj = json_object_new_null();
235 if (json_object_object_add((json_object*)object, name, obj) != 0)
236 {
237 json_object_put(obj);
238 return nullptr;
239 }
240 /* Be consistent with other cJSON and jansson implementations and return a value != nullptr for
241 * success. */
242 return object;
243}
244
245WINPR_JSON* WINPR_JSON_AddTrueToObject(WINPR_JSON* object, const char* name)
246{
247 struct json_object* obj = json_object_new_boolean(TRUE);
248 if (json_object_object_add((json_object*)object, name, obj) != 0)
249 {
250 json_object_put(obj);
251 return nullptr;
252 }
253 return obj;
254}
255
256WINPR_JSON* WINPR_JSON_AddFalseToObject(WINPR_JSON* object, const char* name)
257{
258 struct json_object* obj = json_object_new_boolean(FALSE);
259 if (json_object_object_add((json_object*)object, name, obj) != 0)
260 {
261 json_object_put(obj);
262 return nullptr;
263 }
264 return obj;
265}
266
267WINPR_JSON* WINPR_JSON_AddBoolToObject(WINPR_JSON* object, const char* name, BOOL boolean)
268{
269 struct json_object* obj = json_object_new_boolean(boolean);
270 if (json_object_object_add((json_object*)object, name, obj) != 0)
271 {
272 json_object_put(obj);
273 return nullptr;
274 }
275 return obj;
276}
277
278WINPR_JSON* WINPR_JSON_AddNumberToObject(WINPR_JSON* object, const char* name, double number)
279{
280 struct json_object* obj = json_object_new_double(number);
281 if (json_object_object_add((json_object*)object, name, obj) != 0)
282 {
283 json_object_put(obj);
284 return nullptr;
285 }
286 return obj;
287}
288
289WINPR_JSON* WINPR_JSON_AddIntegerToObject(WINPR_JSON* object, const char* name, int64_t number)
290{
291 struct json_object* obj = json_object_new_int64(number);
292 if (json_object_object_add((json_object*)object, name, obj) != 0)
293 {
294 json_object_put(obj);
295 return nullptr;
296 }
297 return obj;
298}
299
300WINPR_JSON* WINPR_JSON_AddUIntegerToObject(WINPR_JSON* object, const char* name, uint64_t number)
301{
302 struct json_object* obj = json_object_new_uint64(number);
303 if (json_object_object_add((json_object*)object, name, obj) != 0)
304 {
305 json_object_put(obj);
306 return nullptr;
307 }
308 return obj;
309}
310
311WINPR_JSON* WINPR_JSON_AddStringToObject(WINPR_JSON* object, const char* name, const char* string)
312{
313 struct json_object* obj = json_object_new_string(string);
314 if (json_object_object_add((json_object*)object, name, obj) != 0)
315 {
316 json_object_put(obj);
317 return nullptr;
318 }
319 return obj;
320}
321
322WINPR_JSON* WINPR_JSON_AddObjectToObject(WINPR_JSON* object, const char* name)
323{
324 struct json_object* obj = json_object_new_object();
325 if (json_object_object_add((json_object*)object, name, obj) != 0)
326 {
327 json_object_put(obj);
328 return nullptr;
329 }
330 return obj;
331}
332
333BOOL WINPR_JSON_AddItemToArray(WINPR_JSON* array, WINPR_JSON* item)
334{
335 const int rc = json_object_array_add((json_object*)array, (json_object*)item);
336 if (rc != 0)
337 return FALSE;
338 return TRUE;
339}
340
341WINPR_JSON* WINPR_JSON_AddArrayToObject(WINPR_JSON* object, const char* name)
342{
343 struct json_object* obj = json_object_new_array();
344 if (json_object_object_add((json_object*)object, name, obj) != 0)
345 {
346 json_object_put(obj);
347 return nullptr;
348 }
349 return obj;
350}
351
352char* WINPR_JSON_Print(WINPR_JSON* item)
353{
354 const char* str = json_object_to_json_string_ext((json_object*)item, JSON_C_TO_STRING_PRETTY);
355 if (!str)
356 return nullptr;
357 return _strdup(str);
358}
359
360char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item)
361{
362 const char* str = json_object_to_json_string_ext((json_object*)item, JSON_C_TO_STRING_PLAIN);
363 if (!str)
364 return nullptr;
365 return _strdup(str);
366}
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
Definition json-c.c:217
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
Definition json-c.c:192
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
Definition json-c.c:166
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
Definition json-c.c:197
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
Definition json-c.c:212
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
Definition json-c.c:322
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
Definition json-c.c:227
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
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition json-c.c:161
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
Definition json-c.c:333
UINT64 WINPR_JSON_GetUIntegerValue(const WINPR_JSON *item)
Return the unsigned Integer value of a JSON item.
Definition json-c.c:123
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *item)
Check if JSON item is valid.
Definition json-c.c:128
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition json-c.c:171
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
Definition json-c.c:41
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition json-c.c:72
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
Definition json-c.c:202
WINPR_JSON * WINPR_JSON_AddIntegerToObject(WINPR_JSON *object, const char *name, int64_t number)
WINPR_JSON_AddIntegerToObject.
Definition json-c.c:289
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
Definition json-c.c:256
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:352
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition json-c.c:153
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
Definition json-c.c:245
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_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
Definition json-c.c:278
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_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
Definition json-c.c:145
INT64 WINPR_JSON_GetIntegerValue(const WINPR_JSON *item)
Return the Integer value of a JSON item.
Definition json-c.c:118
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_AddUIntegerToObject(WINPR_JSON *object, const char *name, uint64_t number)
WINPR_JSON_AddUIntegerToObject.
Definition json-c.c:300
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
Definition json-c.c:103
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
Definition json-c.c:341
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition json-c.c:177
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
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:360
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_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
Definition json-c.c:207
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition json-c.c:182
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition json-c.c:187
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
Definition json-c.c:232
WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
Definition json-c.c:311
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition json-c.c:62
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
Definition json-c.c:222
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_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
Definition json-c.c:267
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition json-c.c:46