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
29#if !defined(JANSSON_VERSION_HEX) || (JANSSON_VERSION_HEX < 0x020d00)
30#error "The library detected is too old, need >= 2.13.0"
31#endif
32
33#if defined(WITH_DEBUG_JANSSON)
34#include "../log.h"
35#define TAG WINPR_TAG("jansson")
36
37#define ccast(json) ccast_((json), __func__)
38static const json_t* ccast_(const WINPR_JSON* json, const char* fkt)
39{
40 const json_t* jansson = (const json_t*)json;
41 if (!jansson)
42 WLog_DBG(TAG, "%s: NULL", fkt);
43 else
44 {
45 WLog_DBG(TAG, "%s: %" PRIuz, fkt, jansson->refcount);
46 }
47 return jansson;
48}
49
50#define cast(json) cast_((json), __func__)
51static json_t* cast_(WINPR_JSON* json, const char* fkt)
52{
53 json_t* jansson = (json_t*)json;
54 if (!jansson)
55 WLog_DBG(TAG, "%s: NULL", fkt);
56 else
57 {
58 WLog_DBG(TAG, "%s: %" PRIuz, fkt, jansson->refcount);
59 }
60 return jansson;
61}
62
63#define revcast(json) revcast_((json), __func__)
64static WINPR_JSON* revcast_(json_t* json, const char* fkt)
65{
66 json_t* jansson = (json_t*)json;
67 if (!jansson)
68 WLog_DBG(TAG, "%s: NULL", fkt);
69 else
70 {
71 WLog_DBG(TAG, "%s: %" PRIuz, fkt, jansson->refcount);
72 }
73 return jansson;
74}
75#else
76static inline const json_t* ccast(const WINPR_JSON* json)
77{
78 return WINPR_CXX_COMPAT_CAST(const json_t*, json);
79}
80
81static inline json_t* cast(WINPR_JSON* json)
82{
83 return WINPR_CXX_COMPAT_CAST(json_t*, json);
84}
85
86static inline WINPR_JSON* revcast(json_t* json)
87{
88 return WINPR_CXX_COMPAT_CAST(WINPR_JSON*, json);
89}
90#endif
91
92int WINPR_JSON_version(char* buffer, size_t len)
93{
94 return _snprintf(buffer, len, "jansson %s", jansson_version_str());
95}
96
97WINPR_JSON* WINPR_JSON_Parse(const char* value)
98{
99 json_error_t error = { 0 };
100 return revcast(json_loads(value, JSON_DECODE_ANY, &error));
101}
102
103WINPR_JSON* WINPR_JSON_ParseWithLength(const char* value, size_t buffer_length)
104{
105 json_error_t error = { 0 };
106 return revcast(json_loadb(value, buffer_length, JSON_DECODE_ANY, &error));
107}
108
109void WINPR_JSON_Delete(WINPR_JSON* item)
110{
111 json_delete(cast(item));
112}
113
114WINPR_JSON* WINPR_JSON_GetArrayItem(const WINPR_JSON* array, size_t index)
115{
116 return revcast(json_array_get(ccast(array), index));
117}
118
119size_t WINPR_JSON_GetArraySize(const WINPR_JSON* array)
120{
121 return json_array_size(ccast(array));
122}
123
124WINPR_JSON* WINPR_JSON_GetObjectItem(const WINPR_JSON* object, const char* string)
125{
126 json_t* json = cast(WINPR_CAST_CONST_PTR_AWAY(object, WINPR_JSON*));
127 void* it = json_object_iter(json);
128 while (it)
129 {
130 const char* name = json_object_iter_key(it);
131 if (_stricmp(name, string) == 0)
132 return revcast(json_object_iter_value(it));
133 it = json_object_iter_next(json, it);
134 }
135 return NULL;
136}
137
138WINPR_JSON* WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON* object, const char* string)
139{
140 return revcast(json_object_get(ccast(object), string));
141}
142
143BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON* object, const char* string)
144{
145 return json_object_get(ccast(object), string) != NULL;
146}
147
148const char* WINPR_JSON_GetErrorPtr(void)
149{
150 return NULL;
151}
152
153const char* WINPR_JSON_GetStringValue(WINPR_JSON* item)
154{
155 return json_string_value(cast(item));
156}
157
158double WINPR_JSON_GetNumberValue(const WINPR_JSON* item)
159{
160 return json_number_value(ccast(item));
161}
162
163BOOL WINPR_JSON_IsInvalid(const WINPR_JSON* json)
164{
165 const json_t* item = ccast(json);
166 if (WINPR_JSON_IsArray(item))
167 return FALSE;
168 if (WINPR_JSON_IsObject(item))
169 return FALSE;
170 if (WINPR_JSON_IsNull(item))
171 return FALSE;
172 if (WINPR_JSON_IsNumber(item))
173 return FALSE;
174 if (WINPR_JSON_IsBool(item))
175 return FALSE;
176 if (WINPR_JSON_IsString(item))
177 return FALSE;
178 return TRUE;
179}
180
181BOOL WINPR_JSON_IsFalse(const WINPR_JSON* item)
182{
183 return json_is_false(ccast(item));
184}
185
186BOOL WINPR_JSON_IsTrue(const WINPR_JSON* item)
187{
188 return json_is_true(ccast(item));
189}
190
191BOOL WINPR_JSON_IsBool(const WINPR_JSON* item)
192{
193 return json_is_boolean(ccast(item));
194}
195
196BOOL WINPR_JSON_IsNull(const WINPR_JSON* item)
197{
198 return json_is_null(ccast(item));
199}
200
201BOOL WINPR_JSON_IsNumber(const WINPR_JSON* item)
202{
203 return json_is_number(ccast(item));
204}
205
206BOOL WINPR_JSON_IsString(const WINPR_JSON* item)
207{
208 return json_is_string(ccast(item));
209}
210
211BOOL WINPR_JSON_IsArray(const WINPR_JSON* item)
212{
213 return json_is_array(ccast(item));
214}
215
216BOOL WINPR_JSON_IsObject(const WINPR_JSON* item)
217{
218 return json_is_object(ccast(item));
219}
220
221WINPR_JSON* WINPR_JSON_CreateNull(void)
222{
223 return revcast(json_null());
224}
225
226WINPR_JSON* WINPR_JSON_CreateTrue(void)
227{
228 return revcast(json_true());
229}
230
231WINPR_JSON* WINPR_JSON_CreateFalse(void)
232{
233 return revcast(json_false());
234}
235
236WINPR_JSON* WINPR_JSON_CreateBool(BOOL boolean)
237{
238 return revcast(json_boolean(boolean));
239}
240
241WINPR_JSON* WINPR_JSON_CreateNumber(double num)
242{
243 return revcast(json_real(num));
244}
245
246WINPR_JSON* WINPR_JSON_CreateString(const char* string)
247{
248 return revcast(json_string(string));
249}
250
251WINPR_JSON* WINPR_JSON_CreateArray(void)
252{
253 return revcast(json_array());
254}
255
256WINPR_JSON* WINPR_JSON_CreateObject(void)
257{
258 return revcast(json_object());
259}
260
261static WINPR_JSON* add_to_object(WINPR_JSON* object, const char* name, json_t* obj)
262{
263 if (!obj)
264 return NULL;
265 const int rc = json_object_set_new(cast(object), name, obj);
266 if (rc != 0)
267 return NULL;
268 return revcast(obj);
269}
270
271WINPR_JSON* WINPR_JSON_AddNullToObject(WINPR_JSON* object, const char* name)
272{
273 json_t* obj = json_null();
274 return add_to_object(object, name, obj);
275}
276
277WINPR_JSON* WINPR_JSON_AddTrueToObject(WINPR_JSON* object, const char* name)
278{
279 json_t* obj = json_true();
280 return add_to_object(object, name, obj);
281}
282
283WINPR_JSON* WINPR_JSON_AddFalseToObject(WINPR_JSON* object, const char* name)
284{
285 json_t* obj = json_false();
286 return add_to_object(object, name, obj);
287}
288
289WINPR_JSON* WINPR_JSON_AddBoolToObject(WINPR_JSON* object, const char* name, BOOL boolean)
290{
291 json_t* obj = json_boolean(boolean);
292 return add_to_object(object, name, obj);
293}
294
295WINPR_JSON* WINPR_JSON_AddNumberToObject(WINPR_JSON* object, const char* name, double number)
296{
297 json_t* obj = json_real(number);
298 return add_to_object(object, name, obj);
299}
300
301WINPR_JSON* WINPR_JSON_AddStringToObject(WINPR_JSON* object, const char* name, const char* string)
302{
303 json_t* obj = json_string(string);
304 return add_to_object(object, name, obj);
305}
306
307WINPR_JSON* WINPR_JSON_AddObjectToObject(WINPR_JSON* object, const char* name)
308{
309 json_t* obj = json_object();
310 return add_to_object(object, name, obj);
311}
312
313BOOL WINPR_JSON_AddItemToArray(WINPR_JSON* array, WINPR_JSON* item)
314{
315 return json_array_append_new(cast(array), item) == 0;
316}
317
318WINPR_JSON* WINPR_JSON_AddArrayToObject(WINPR_JSON* object, const char* name)
319{
320 json_t* obj = json_array();
321 return add_to_object(object, name, obj);
322}
323
324char* WINPR_JSON_Print(WINPR_JSON* item)
325{
326 return json_dumps(cast(item), JSON_INDENT(2) | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
327}
328
329char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item)
330{
331 return json_dumps(cast(item), JSON_COMPACT | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
332}
WINPR_JSON * WINPR_JSON_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
Definition jansson.c:236
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
Definition jansson.c:246
BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
Definition jansson.c:143
WINPR_JSON * WINPR_JSON_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
Definition jansson.c:295
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
Definition jansson.c:196
WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition jansson.c:124
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition jansson.c:206
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
Definition jansson.c:313
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
Definition jansson.c:318
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition jansson.c:191
double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition jansson.c:158
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
Definition jansson.c:277
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
Definition jansson.c:256
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
Definition jansson.c:251
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
Definition jansson.c:92
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:324
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
Definition jansson.c:283
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition jansson.c:201
WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition jansson.c:114
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:138
WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
Definition jansson.c:301
WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
Definition jansson.c:103
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
Definition jansson.c:231
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
Definition jansson.c:241
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition jansson.c:216
WINPR_JSON * WINPR_JSON_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
Definition jansson.c:289
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *json)
Check if JSON item is valid.
Definition jansson.c:163
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:329
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
Definition jansson.c:221
const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition jansson.c:153
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
Definition jansson.c:271
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
Definition jansson.c:226
BOOL WINPR_JSON_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
Definition jansson.c:181
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition jansson.c:109
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition jansson.c:119
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition jansson.c:211
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
Definition jansson.c:148
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
Definition jansson.c:307
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition jansson.c:97
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition jansson.c:186