FreeRDP
Loading...
Searching...
No Matches
jansson.c
1
20#include <math.h>
21#include <float.h>
22
23#include <winpr/file.h>
24#include <winpr/json.h>
25#include <winpr/assert.h>
26
27#include "../log.h"
28#define TAG WINPR_TAG("utils.json.jansson")
29
30#if !defined(WITH_JANSSON)
31#error "This file must only be compiled if jansson library is linked in"
32#endif
33#include <jansson.h>
34
35#if !defined(JANSSON_VERSION_HEX) || (JANSSON_VERSION_HEX < 0x020d00)
36#error "The library detected is too old, need >= 2.13.0"
37#endif
38
39static WINPR_TLS char lasterror[256] = WINPR_C_ARRAY_INIT;
40
41#if defined(WITH_DEBUG_JANSSON)
42#include "../log.h"
43#define TAG WINPR_TAG("jansson")
44
45#define ccast(json) ccast_((json), __func__)
46static const json_t* ccast_(const WINPR_JSON* json, const char* fkt)
47{
48 const json_t* jansson = (const json_t*)json;
49 if (!jansson)
50 WLog_DBG(TAG, "%s: nullptr", fkt);
51 else
52 {
53 WLog_DBG(TAG, "%s: %" PRIuz, fkt, jansson->refcount);
54 }
55 return jansson;
56}
57
58#define cast(json) cast_((json), __func__)
59static json_t* cast_(WINPR_JSON* json, const char* fkt)
60{
61 json_t* jansson = (json_t*)json;
62 if (!jansson)
63 WLog_DBG(TAG, "%s: nullptr", fkt);
64 else
65 {
66 WLog_DBG(TAG, "%s: %" PRIuz, fkt, jansson->refcount);
67 }
68 return jansson;
69}
70
71#define revcast(json) revcast_((json), __func__)
72static WINPR_JSON* revcast_(json_t* json, const char* fkt)
73{
74 json_t* jansson = (json_t*)json;
75 if (!jansson)
76 WLog_DBG(TAG, "%s: nullptr", fkt);
77 else
78 {
79 WLog_DBG(TAG, "%s: %" PRIuz, fkt, jansson->refcount);
80 }
81 return jansson;
82}
83#else
84WINPR_ATTR_NODISCARD
85static inline const json_t* ccast(const WINPR_JSON* json)
86{
87 return WINPR_CXX_COMPAT_CAST(const json_t*, json);
88}
89
90WINPR_ATTR_NODISCARD
91static inline json_t* cast(WINPR_JSON* json)
92{
93 return WINPR_CXX_COMPAT_CAST(json_t*, json);
94}
95
96WINPR_ATTR_NODISCARD
97static inline WINPR_JSON* revcast(json_t* json)
98{
99 return WINPR_CXX_COMPAT_CAST(WINPR_JSON*, json);
100}
101#endif
102
103int WINPR_JSON_version(char* buffer, size_t len)
104{
105 return _snprintf(buffer, len, "jansson %s", jansson_version_str());
106}
107
108static WINPR_JSON* updateError(WINPR_JSON* json, const json_error_t* error)
109{
110 lasterror[0] = '\0';
111 if (!json)
112 (void)_snprintf(lasterror, sizeof(lasterror), "[%d:%d:%d] %s [%s]", error->line,
113 error->column, error->position, error->text, error->source);
114 return json;
115}
116
117WINPR_JSON* WINPR_JSON_Parse(const char* value)
118{
119 json_error_t error = WINPR_C_ARRAY_INIT;
120 WINPR_JSON* json = revcast(json_loads(value, JSON_DECODE_ANY, &error));
121 return updateError(json, &error);
122}
123
124WINPR_JSON* WINPR_JSON_ParseWithLength(const char* value, size_t buffer_length)
125{
126 if (!value || (buffer_length == 0))
127 return nullptr;
128
129 json_error_t error = WINPR_C_ARRAY_INIT;
130 const size_t slen = strnlen(value, buffer_length);
131 WINPR_JSON* json = revcast(json_loadb(value, slen, JSON_DECODE_ANY, &error));
132 return updateError(json, &error);
133}
134
135void WINPR_JSON_Delete(WINPR_JSON* item)
136{
137 json_delete(cast(item));
138}
139
140WINPR_JSON* WINPR_JSON_GetArrayItem(const WINPR_JSON* array, size_t index)
141{
142 return revcast(json_array_get(ccast(array), index));
143}
144
145size_t WINPR_JSON_GetArraySize(const WINPR_JSON* array)
146{
147 return json_array_size(ccast(array));
148}
149
150WINPR_JSON* WINPR_JSON_GetObjectItem(const WINPR_JSON* object, const char* string)
151{
152 json_t* json = cast(WINPR_CAST_CONST_PTR_AWAY(object, WINPR_JSON*));
153 void* it = json_object_iter(json);
154 while (it)
155 {
156 const char* name = json_object_iter_key(it);
157 if (_stricmp(name, string) == 0)
158 return revcast(json_object_iter_value(it));
159 it = json_object_iter_next(json, it);
160 }
161 return nullptr;
162}
163
164WINPR_JSON* WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON* object, const char* string)
165{
166 return revcast(json_object_get(ccast(object), string));
167}
168
169BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON* object, const char* string)
170{
171 return json_object_get(ccast(object), string) != nullptr;
172}
173
174const char* WINPR_JSON_GetErrorPtr(void)
175{
176 return lasterror;
177}
178
179const char* WINPR_JSON_GetStringValue(WINPR_JSON* item)
180{
181 return json_string_value(cast(item));
182}
183
184double WINPR_JSON_GetNumberValue(const WINPR_JSON* item)
185{
186 return json_number_value(ccast(item));
187}
188
189WINPR_ATTR_NODISCARD
190static INT64 checked_int(double val)
191{
192 if (isnan(val) || isinf(val))
193 {
194 WLog_WARN(TAG,
195 "WINPR_JSON should read an integer, but a fractional NaN / INF number %lf was "
196 "found. Returning 0",
197 val);
198 return 0;
199 }
200
201 double intpart = NAN;
202 double fraction = modf(val, &intpart);
203 if (fabs(fraction) <= DBL_EPSILON)
204 return (INT64)val;
205 WLog_WARN(TAG,
206 "WINPR_JSON should read an integer, but a fractional number %lf was found. Dropping "
207 "fraction %lf.",
208 val, fraction);
209 return (INT64)intpart;
210}
211
212INT64 WINPR_JSON_GetIntegerValue(const WINPR_JSON* item)
213{
214 const json_t* it = ccast(item);
215 if (json_is_integer(it))
216 return (INT64)json_integer_value(it);
217 if (json_is_real(it))
218 {
219 double val = json_real_value(it);
220 return checked_int(val);
221 }
222 if (json_is_number(it))
223 {
224 double val = json_number_value(it);
225 return checked_int(val);
226 }
227 return 0;
228}
229
230WINPR_ATTR_NODISCARD
231static UINT64 checked_uint(double val)
232{
233 if (isnan(val) || isinf(val))
234 {
235 WLog_WARN(TAG,
236 "WINPR_JSON should read an unsigned, but a fractional negative number %lf was "
237 "found. Returning 0",
238 val);
239 return 0;
240 }
241
242 if (val < 0.0)
243 {
244 WLog_WARN(TAG,
245 "WINPR_JSON should read an unsigned, but a fractional NaN / INF number %lf was "
246 "found. Returning 0",
247 val);
248 return 0;
249 }
250
251 double intpart = NAN;
252 double fraction = modf(val, &intpart);
253 if (fabs(fraction) <= DBL_EPSILON)
254 return (UINT64)val;
255
256 WLog_WARN(TAG,
257 "WINPR_JSON should read an unsigned, but a fractional number %lf was found. Dropping "
258 "fraction %lf.",
259 val, fraction);
260 return (UINT64)intpart;
261}
262
263UINT64 WINPR_JSON_GetUIntegerValue(const WINPR_JSON* item)
264{
265 const json_t* it = ccast(item);
266 if (json_is_integer(it))
267 return (UINT64)json_integer_value(it);
268 if (json_is_real(it))
269 {
270 double val = json_real_value(it);
271 return checked_uint(val);
272 }
273 if (json_is_number(it))
274 {
275 double val = json_number_value(it);
276 return checked_uint(val);
277 }
278 return 0;
279}
280
281BOOL WINPR_JSON_IsInvalid(const WINPR_JSON* item)
282{
283 const json_t* jitem = ccast(item);
284 if (WINPR_JSON_IsArray(jitem))
285 return FALSE;
286 if (WINPR_JSON_IsObject(jitem))
287 return FALSE;
288 if (WINPR_JSON_IsNull(jitem))
289 return FALSE;
290 if (WINPR_JSON_IsNumber(jitem))
291 return FALSE;
292 if (WINPR_JSON_IsBool(jitem))
293 return FALSE;
294 if (WINPR_JSON_IsString(jitem))
295 return FALSE;
296 return TRUE;
297}
298
299BOOL WINPR_JSON_IsFalse(const WINPR_JSON* item)
300{
301 return json_is_false(ccast(item));
302}
303
304BOOL WINPR_JSON_IsTrue(const WINPR_JSON* item)
305{
306 return json_is_true(ccast(item));
307}
308
309BOOL WINPR_JSON_IsBool(const WINPR_JSON* item)
310{
311 return json_is_boolean(ccast(item));
312}
313
314BOOL WINPR_JSON_IsNull(const WINPR_JSON* item)
315{
316 return json_is_null(ccast(item));
317}
318
319BOOL WINPR_JSON_IsNumber(const WINPR_JSON* item)
320{
321 return json_is_number(ccast(item));
322}
323
324BOOL WINPR_JSON_IsString(const WINPR_JSON* item)
325{
326 return json_is_string(ccast(item));
327}
328
329BOOL WINPR_JSON_IsArray(const WINPR_JSON* item)
330{
331 return json_is_array(ccast(item));
332}
333
334BOOL WINPR_JSON_IsObject(const WINPR_JSON* item)
335{
336 return json_is_object(ccast(item));
337}
338
339WINPR_JSON* WINPR_JSON_CreateNull(void)
340{
341 return revcast(json_null());
342}
343
344WINPR_JSON* WINPR_JSON_CreateTrue(void)
345{
346 return revcast(json_true());
347}
348
349WINPR_JSON* WINPR_JSON_CreateFalse(void)
350{
351 return revcast(json_false());
352}
353
354WINPR_JSON* WINPR_JSON_CreateBool(BOOL boolean)
355{
356 return revcast(json_boolean(boolean));
357}
358
359WINPR_JSON* WINPR_JSON_CreateNumber(double num)
360{
361 double intpart;
362 if (modf(num, &intpart) == 0.0)
363 return revcast(json_integer((json_int_t)intpart));
364 return revcast(json_real(num));
365}
366
367WINPR_JSON* WINPR_JSON_CreateString(const char* string)
368{
369 return revcast(json_string(string));
370}
371
372WINPR_JSON* WINPR_JSON_CreateArray(void)
373{
374 return revcast(json_array());
375}
376
377WINPR_JSON* WINPR_JSON_CreateObject(void)
378{
379 return revcast(json_object());
380}
381
382static WINPR_JSON* add_to_object(WINPR_JSON* object, const char* name, json_t* obj)
383{
384 if (!obj)
385 return nullptr;
386 const int rc = json_object_set_new(cast(object), name, obj);
387 if (rc != 0)
388 return nullptr;
389 return revcast(obj);
390}
391
392WINPR_JSON* WINPR_JSON_AddNullToObject(WINPR_JSON* object, const char* name)
393{
394 json_t* obj = json_null();
395 return add_to_object(object, name, obj);
396}
397
398WINPR_JSON* WINPR_JSON_AddTrueToObject(WINPR_JSON* object, const char* name)
399{
400 json_t* obj = json_true();
401 return add_to_object(object, name, obj);
402}
403
404WINPR_JSON* WINPR_JSON_AddFalseToObject(WINPR_JSON* object, const char* name)
405{
406 json_t* obj = json_false();
407 return add_to_object(object, name, obj);
408}
409
410WINPR_JSON* WINPR_JSON_AddBoolToObject(WINPR_JSON* object, const char* name, BOOL boolean)
411{
412 json_t* obj = json_boolean(boolean);
413 return add_to_object(object, name, obj);
414}
415
416WINPR_JSON* WINPR_JSON_AddNumberToObject(WINPR_JSON* object, const char* name, double number)
417{
418 json_t* obj = json_real(number);
419 return add_to_object(object, name, obj);
420}
421
422WINPR_JSON* WINPR_JSON_AddIntegerToObject(WINPR_JSON* object, const char* name, int64_t number)
423{
424 json_t* obj = json_integer(number);
425 return add_to_object(object, name, obj);
426}
427
428WINPR_JSON* WINPR_JSON_AddUIntegerToObject(WINPR_JSON* object, const char* name, uint64_t number)
429{
430 if (number > INT64_MAX)
431 {
432 WLog_ERR(TAG, "jansson does not support integers > %" PRId64, INT64_MAX);
433 return nullptr;
434 }
435
436 json_t* obj = json_integer((int64_t)number);
437 return add_to_object(object, name, obj);
438}
439
440WINPR_JSON* WINPR_JSON_AddStringToObject(WINPR_JSON* object, const char* name, const char* string)
441{
442 json_t* obj = json_string(string);
443 return add_to_object(object, name, obj);
444}
445
446WINPR_JSON* WINPR_JSON_AddObjectToObject(WINPR_JSON* object, const char* name)
447{
448 json_t* obj = json_object();
449 return add_to_object(object, name, obj);
450}
451
452BOOL WINPR_JSON_AddItemToArray(WINPR_JSON* array, WINPR_JSON* item)
453{
454 return json_array_append_new(cast(array), item) == 0;
455}
456
457WINPR_JSON* WINPR_JSON_AddArrayToObject(WINPR_JSON* object, const char* name)
458{
459 json_t* obj = json_array();
460 return add_to_object(object, name, obj);
461}
462
463char* WINPR_JSON_Print(WINPR_JSON* item)
464{
465 return json_dumps(cast(item), JSON_INDENT(2) | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
466}
467
468char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item)
469{
470 return json_dumps(cast(item), JSON_COMPACT | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
471}
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
Definition jansson.c:367
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
Definition jansson.c:339
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
Definition jansson.c:314
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
Definition jansson.c:344
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
Definition jansson.c:359
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
Definition jansson.c:446
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
Definition jansson.c:377
BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
Definition jansson.c:169
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition jansson.c:309
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
Definition jansson.c:452
UINT64 WINPR_JSON_GetUIntegerValue(const WINPR_JSON *item)
Return the unsigned Integer value of a JSON item.
Definition jansson.c:263
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *item)
Check if JSON item is valid.
Definition jansson.c:281
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition jansson.c:319
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
Definition jansson.c:103
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition jansson.c:145
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
Definition jansson.c:349
WINPR_JSON * WINPR_JSON_AddIntegerToObject(WINPR_JSON *object, const char *name, int64_t number)
WINPR_JSON_AddIntegerToObject.
Definition jansson.c:422
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
Definition jansson.c:404
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:463
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition jansson.c:304
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
Definition jansson.c:398
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:164
WINPR_JSON * WINPR_JSON_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
Definition jansson.c:416
WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition jansson.c:150
BOOL WINPR_JSON_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
Definition jansson.c:299
INT64 WINPR_JSON_GetIntegerValue(const WINPR_JSON *item)
Return the Integer value of a JSON item.
Definition jansson.c:212
WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
Definition jansson.c:124
WINPR_JSON * WINPR_JSON_AddUIntegerToObject(WINPR_JSON *object, const char *name, uint64_t number)
WINPR_JSON_AddUIntegerToObject.
Definition jansson.c:428
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
Definition jansson.c:174
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
Definition jansson.c:457
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition jansson.c:324
WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition jansson.c:140
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:468
double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition jansson.c:184
WINPR_JSON * WINPR_JSON_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
Definition jansson.c:354
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition jansson.c:329
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition jansson.c:334
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
Definition jansson.c:392
WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
Definition jansson.c:440
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition jansson.c:135
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
Definition jansson.c:372
const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition jansson.c:179
WINPR_JSON * WINPR_JSON_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
Definition jansson.c:410
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition jansson.c:117