FreeRDP
Loading...
Searching...
No Matches
jansson.c
1
20#include <winpr/file.h>
21#include <winpr/json.h>
22#include <winpr/assert.h>
23
24#if !defined(WITH_JANSSON)
25#error "This file must only be compiled if jansson library is linked in"
26#endif
27#include <jansson.h>
28
29int WINPR_JSON_version(char* buffer, size_t len)
30{
31 return _snprintf(buffer, len, "jansson %s", jansson_version_str());
32}
33
34WINPR_JSON* WINPR_JSON_Parse(const char* value)
35{
36 json_error_t error = { 0 };
37 return json_loads(value, JSON_DECODE_ANY, &error);
38}
39
40WINPR_JSON* WINPR_JSON_ParseWithLength(const char* value, size_t buffer_length)
41{
42 json_error_t error = { 0 };
43 return json_loadb(value, buffer_length, JSON_DECODE_ANY, &error);
44}
45
46void WINPR_JSON_Delete(WINPR_JSON* item)
47{
48 json_delete((json_t*)item);
49}
50
51WINPR_JSON* WINPR_JSON_GetArrayItem(const WINPR_JSON* array, size_t index)
52{
53 return json_array_get(array, index);
54}
55
56size_t WINPR_JSON_GetArraySize(const WINPR_JSON* array)
57{
58 return json_array_size(array);
59}
60
61WINPR_JSON* WINPR_JSON_GetObjectItem(const WINPR_JSON* object, const char* string)
62{
63 void* it = json_object_iter((json_t*)object);
64 while (it)
65 {
66 const char* name = json_object_iter_key(it);
67 if (_stricmp(name, string) == 0)
68 return json_object_iter_value(it);
69 it = json_object_iter_next((json_t*)object, it);
70 }
71 return NULL;
72}
73
74WINPR_JSON* WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON* object, const char* string)
75{
76 return json_object_get(object, string);
77}
78
79BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON* object, const char* string)
80{
81 return json_object_get(object, string) != NULL;
82}
83
84const char* WINPR_JSON_GetErrorPtr(void)
85{
86 return NULL;
87}
88
89const char* WINPR_JSON_GetStringValue(WINPR_JSON* item)
90{
91 return json_string_value(item);
92}
93
94double WINPR_JSON_GetNumberValue(const WINPR_JSON* item)
95{
96 return json_real_value((const json_t*)item);
97}
98
99BOOL WINPR_JSON_IsInvalid(const WINPR_JSON* item)
100{
101 if (WINPR_JSON_IsArray(item))
102 return FALSE;
103 if (WINPR_JSON_IsObject(item))
104 return FALSE;
105 if (WINPR_JSON_IsNull(item))
106 return FALSE;
107 if (WINPR_JSON_IsNumber(item))
108 return FALSE;
109 if (WINPR_JSON_IsBool(item))
110 return FALSE;
111 if (WINPR_JSON_IsString(item))
112 return FALSE;
113 return TRUE;
114}
115
116BOOL WINPR_JSON_IsFalse(const WINPR_JSON* item)
117{
118 return json_is_false((const json_t*)item);
119}
120
121BOOL WINPR_JSON_IsTrue(const WINPR_JSON* item)
122{
123 return json_is_true((const json_t*)item);
124}
125
126BOOL WINPR_JSON_IsBool(const WINPR_JSON* item)
127{
128 return json_is_boolean((const json_t*)item);
129}
130
131BOOL WINPR_JSON_IsNull(const WINPR_JSON* item)
132{
133 return json_is_null((const json_t*)item);
134}
135
136BOOL WINPR_JSON_IsNumber(const WINPR_JSON* item)
137{
138 return json_is_number((const json_t*)item);
139}
140
141BOOL WINPR_JSON_IsString(const WINPR_JSON* item)
142{
143 return json_is_string((const json_t*)item);
144}
145
146BOOL WINPR_JSON_IsArray(const WINPR_JSON* item)
147{
148 return json_is_array((const json_t*)item);
149}
150
151BOOL WINPR_JSON_IsObject(const WINPR_JSON* item)
152{
153 return json_is_array((const json_t*)item);
154}
155
156WINPR_JSON* WINPR_JSON_CreateNull(void)
157{
158 return json_null();
159}
160
161WINPR_JSON* WINPR_JSON_CreateTrue(void)
162{
163 return json_true();
164}
165
166WINPR_JSON* WINPR_JSON_CreateFalse(void)
167{
168 return json_false();
169}
170
171WINPR_JSON* WINPR_JSON_CreateBool(BOOL boolean)
172{
173 return json_boolean(boolean);
174}
175
176WINPR_JSON* WINPR_JSON_CreateNumber(double num)
177{
178 return json_real(num);
179}
180
181WINPR_JSON* WINPR_JSON_CreateString(const char* string)
182{
183 return json_string(string);
184}
185
186WINPR_JSON* WINPR_JSON_CreateArray(void)
187{
188 return json_array();
189}
190
191WINPR_JSON* WINPR_JSON_CreateObject(void)
192{
193 return json_object();
194}
195
196static WINPR_JSON* add_to_object(WINPR_JSON* object, const char* name, json_t* obj)
197{
198 if (!obj)
199 return NULL;
200 if (json_object_set_new(object, name, obj) != 0)
201 return NULL;
202 return obj;
203}
204
205WINPR_JSON* WINPR_JSON_AddNullToObject(WINPR_JSON* object, const char* name)
206{
207 json_t* obj = json_null();
208 return add_to_object(object, name, obj);
209}
210
211WINPR_JSON* WINPR_JSON_AddTrueToObject(WINPR_JSON* object, const char* name)
212{
213 json_t* obj = json_true();
214 return add_to_object(object, name, obj);
215}
216
217WINPR_JSON* WINPR_JSON_AddFalseToObject(WINPR_JSON* object, const char* name)
218{
219 json_t* obj = json_false();
220 return add_to_object(object, name, obj);
221}
222
223WINPR_JSON* WINPR_JSON_AddBoolToObject(WINPR_JSON* object, const char* name, BOOL boolean)
224{
225 json_t* obj = json_boolean(boolean);
226 return add_to_object(object, name, obj);
227}
228
229WINPR_JSON* WINPR_JSON_AddNumberToObject(WINPR_JSON* object, const char* name, double number)
230{
231 json_t* obj = json_real(number);
232 return add_to_object(object, name, obj);
233}
234
235WINPR_JSON* WINPR_JSON_AddStringToObject(WINPR_JSON* object, const char* name, const char* string)
236{
237 json_t* obj = json_string(string);
238 return add_to_object(object, name, obj);
239}
240
241WINPR_JSON* WINPR_JSON_AddObjectToObject(WINPR_JSON* object, const char* name)
242{
243 json_t* obj = json_object();
244 return add_to_object(object, name, obj);
245}
246
247BOOL WINPR_JSON_AddItemToArray(WINPR_JSON* array, WINPR_JSON* item)
248{
249 return json_array_append(array, item) == 0;
250}
251
252WINPR_JSON* WINPR_JSON_AddArrayToObject(WINPR_JSON* object, const char* name)
253{
254 json_t* obj = json_array();
255 return add_to_object(object, name, obj);
256}
257
258char* WINPR_JSON_Print(WINPR_JSON* item)
259{
260 return json_dumps(item, JSON_INDENT(2) | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
261}
262
263char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item)
264{
265 return json_dumps(item, JSON_COMPACT | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
266}
WINPR_JSON * WINPR_JSON_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
Definition jansson.c:171
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
Definition jansson.c:181
BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
Definition jansson.c:79
WINPR_JSON * WINPR_JSON_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
Definition jansson.c:229
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
Definition jansson.c:131
WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition jansson.c:61
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition jansson.c:141
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
Definition jansson.c:247
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
Definition jansson.c:252
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition jansson.c:126
double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition jansson.c:94
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
Definition jansson.c:211
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
Definition jansson.c:191
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
Definition jansson.c:186
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
Definition jansson.c:29
char * WINPR_JSON_Print(WINPR_JSON *item)
Serialize a JSON instance to string for minimal size without formatting see WINPR_JSON_PrintUnformatt...
Definition jansson.c:258
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
Definition jansson.c:217
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition jansson.c:136
WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition jansson.c:51
WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
Definition jansson.c:74
WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
Definition jansson.c:235
WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
Definition jansson.c:40
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
Definition jansson.c:166
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
Definition jansson.c:176
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition jansson.c:151
WINPR_JSON * WINPR_JSON_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
Definition jansson.c:223
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *item)
Check if JSON item is valid.
Definition jansson.c:99
char * WINPR_JSON_PrintUnformatted(WINPR_JSON *item)
Serialize a JSON instance to string without formatting for human readable formatted output see WINPR_...
Definition jansson.c:263
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
Definition jansson.c:156
const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition jansson.c:89
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
Definition jansson.c:205
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
Definition jansson.c:161
BOOL WINPR_JSON_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
Definition jansson.c:116
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition jansson.c:46
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition jansson.c:56
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition jansson.c:146
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
Definition jansson.c:84
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
Definition jansson.c:241
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition jansson.c:34
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition jansson.c:121