23#include <winpr/file.h>
24#include <winpr/json.h>
25#include <winpr/assert.h>
28#define TAG WINPR_TAG("utils.json.jansson")
30#if !defined(WITH_JANSSON)
31#error "This file must only be compiled if jansson library is linked in"
35#if !defined(JANSSON_VERSION_HEX) || (JANSSON_VERSION_HEX < 0x020d00)
36#error "The library detected is too old, need >= 2.13.0"
39static WINPR_TLS
char lasterror[256] = WINPR_C_ARRAY_INIT;
41#if defined(WITH_DEBUG_JANSSON)
43#define TAG WINPR_TAG("jansson")
45#define ccast(json) ccast_((json), __func__)
46static const json_t* ccast_(
const WINPR_JSON* json,
const char* fkt)
48 const json_t* jansson = (
const json_t*)json;
50 WLog_DBG(TAG,
"%s: nullptr", fkt);
53 WLog_DBG(TAG,
"%s: %" PRIuz, fkt, jansson->refcount);
58#define cast(json) cast_((json), __func__)
59static json_t* cast_(WINPR_JSON* json,
const char* fkt)
61 json_t* jansson = (json_t*)json;
63 WLog_DBG(TAG,
"%s: nullptr", fkt);
66 WLog_DBG(TAG,
"%s: %" PRIuz, fkt, jansson->refcount);
71#define revcast(json) revcast_((json), __func__)
72static WINPR_JSON* revcast_(json_t* json,
const char* fkt)
74 json_t* jansson = (json_t*)json;
76 WLog_DBG(TAG,
"%s: nullptr", fkt);
79 WLog_DBG(TAG,
"%s: %" PRIuz, fkt, jansson->refcount);
85static inline const json_t* ccast(
const WINPR_JSON* json)
87 return WINPR_CXX_COMPAT_CAST(
const json_t*, json);
91static inline json_t* cast(WINPR_JSON* json)
93 return WINPR_CXX_COMPAT_CAST(json_t*, json);
97static inline WINPR_JSON* revcast(json_t* json)
99 return WINPR_CXX_COMPAT_CAST(WINPR_JSON*, json);
105 return _snprintf(buffer, len,
"jansson %s", jansson_version_str());
108static WINPR_JSON* updateError(WINPR_JSON* json,
const json_error_t* error)
112 (void)_snprintf(lasterror,
sizeof(lasterror),
"[%d:%d:%d] %s [%s]", error->line,
113 error->column, error->position, error->text, error->source);
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);
126 if (!value || (buffer_length == 0))
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);
137 json_delete(cast(item));
142 return revcast(json_array_get(ccast(array), index));
147 return json_array_size(ccast(array));
152 json_t* json = cast(WINPR_CAST_CONST_PTR_AWAY(
object, WINPR_JSON*));
153 void* it = json_object_iter(json);
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);
166 return revcast(json_object_get(ccast(
object),
string));
171 return json_object_get(ccast(
object),
string) !=
nullptr;
181 return json_string_value(cast(item));
186 return json_number_value(ccast(item));
190static INT64 checked_int(
double val)
192 if (isnan(val) || isinf(val))
195 "WINPR_JSON should read an integer, but a fractional NaN / INF number %lf was "
196 "found. Returning 0",
201 double intpart = NAN;
202 double fraction = modf(val, &intpart);
203 if (fabs(fraction) <= DBL_EPSILON)
206 "WINPR_JSON should read an integer, but a fractional number %lf was found. Dropping "
209 return (INT64)intpart;
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))
219 double val = json_real_value(it);
220 return checked_int(val);
222 if (json_is_number(it))
224 double val = json_number_value(it);
225 return checked_int(val);
231static UINT64 checked_uint(
double val)
233 if (isnan(val) || isinf(val))
236 "WINPR_JSON should read an unsigned, but a fractional negative number %lf was "
237 "found. Returning 0",
245 "WINPR_JSON should read an unsigned, but a fractional NaN / INF number %lf was "
246 "found. Returning 0",
251 double intpart = NAN;
252 double fraction = modf(val, &intpart);
253 if (fabs(fraction) <= DBL_EPSILON)
257 "WINPR_JSON should read an unsigned, but a fractional number %lf was found. Dropping "
260 return (UINT64)intpart;
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))
270 double val = json_real_value(it);
271 return checked_uint(val);
273 if (json_is_number(it))
275 double val = json_number_value(it);
276 return checked_uint(val);
283 const json_t* jitem = ccast(item);
301 return json_is_false(ccast(item));
306 return json_is_true(ccast(item));
311 return json_is_boolean(ccast(item));
316 return json_is_null(ccast(item));
321 return json_is_number(ccast(item));
326 return json_is_string(ccast(item));
331 return json_is_array(ccast(item));
336 return json_is_object(ccast(item));
341 return revcast(json_null());
346 return revcast(json_true());
351 return revcast(json_false());
356 return revcast(json_boolean(
boolean));
362 if (modf(num, &intpart) == 0.0)
363 return revcast(json_integer((json_int_t)intpart));
364 return revcast(json_real(num));
369 return revcast(json_string(
string));
374 return revcast(json_array());
379 return revcast(json_object());
382static WINPR_JSON* add_to_object(WINPR_JSON*
object,
const char* name, json_t* obj)
386 const int rc = json_object_set_new(cast(
object), name, obj);
394 json_t* obj = json_null();
395 return add_to_object(
object, name, obj);
400 json_t* obj = json_true();
401 return add_to_object(
object, name, obj);
406 json_t* obj = json_false();
407 return add_to_object(
object, name, obj);
412 json_t* obj = json_boolean(
boolean);
413 return add_to_object(
object, name, obj);
418 json_t* obj = json_real(number);
419 return add_to_object(
object, name, obj);
424 json_t* obj = json_integer(number);
425 return add_to_object(
object, name, obj);
430 if (number > INT64_MAX)
432 WLog_ERR(TAG,
"jansson does not support integers > %" PRId64, INT64_MAX);
436 json_t* obj = json_integer((int64_t)number);
437 return add_to_object(
object, name, obj);
442 json_t* obj = json_string(
string);
443 return add_to_object(
object, name, obj);
448 json_t* obj = json_object();
449 return add_to_object(
object, name, obj);
454 return json_array_append_new(cast(array), item) == 0;
459 json_t* obj = json_array();
460 return add_to_object(
object, name, obj);
465 return json_dumps(cast(item), JSON_INDENT(2) | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
470 return json_dumps(cast(item), JSON_COMPACT | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
UINT64 WINPR_JSON_GetUIntegerValue(const WINPR_JSON *item)
Return the unsigned Integer value of a JSON item.
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *item)
Check if JSON item is valid.
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
WINPR_JSON * WINPR_JSON_AddIntegerToObject(WINPR_JSON *object, const char *name, int64_t number)
WINPR_JSON_AddIntegerToObject.
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
char * WINPR_JSON_Print(WINPR_JSON *item)
Serialize a JSON instance to string for minimal size without formatting see WINPR_JSON_PrintUnformatt...
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
WINPR_JSON * WINPR_JSON_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
BOOL WINPR_JSON_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
INT64 WINPR_JSON_GetIntegerValue(const WINPR_JSON *item)
Return the Integer value of a JSON item.
WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
WINPR_JSON * WINPR_JSON_AddUIntegerToObject(WINPR_JSON *object, const char *name, uint64_t number)
WINPR_JSON_AddUIntegerToObject.
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
char * WINPR_JSON_PrintUnformatted(WINPR_JSON *item)
Serialize a JSON instance to string without formatting for human readable formatted output see WINPR_...
double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
WINPR_JSON * WINPR_JSON_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
WINPR_JSON * WINPR_JSON_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.