20#include <winpr/file.h>
21#include <winpr/json.h>
22#include <winpr/assert.h>
24#if !defined(WITH_JANSSON)
25#error "This file must only be compiled if jansson library is linked in"
31 return _snprintf(buffer, len,
"jansson %s", jansson_version_str());
36 json_error_t error = { 0 };
37 return json_loads(value, JSON_DECODE_ANY, &error);
42 json_error_t error = { 0 };
43 return json_loadb(value, buffer_length, JSON_DECODE_ANY, &error);
48 json_delete((json_t*)item);
53 return json_array_get(array, index);
58 return json_array_size(array);
63 void* it = json_object_iter((json_t*)
object);
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);
76 return json_object_get(
object,
string);
81 return json_object_get(
object,
string) != NULL;
91 return json_string_value(item);
96 return json_real_value((
const json_t*)item);
118 return json_is_false((
const json_t*)item);
123 return json_is_true((
const json_t*)item);
128 return json_is_boolean((
const json_t*)item);
133 return json_is_null((
const json_t*)item);
138 return json_is_number((
const json_t*)item);
143 return json_is_string((
const json_t*)item);
148 return json_is_array((
const json_t*)item);
153 return json_is_array((
const json_t*)item);
173 return json_boolean(
boolean);
178 return json_real(num);
183 return json_string(
string);
193 return json_object();
196static WINPR_JSON* add_to_object(WINPR_JSON*
object,
const char* name, json_t* obj)
200 if (json_object_set_new(
object, name, obj) != 0)
207 json_t* obj = json_null();
208 return add_to_object(
object, name, obj);
213 json_t* obj = json_true();
214 return add_to_object(
object, name, obj);
219 json_t* obj = json_false();
220 return add_to_object(
object, name, obj);
225 json_t* obj = json_boolean(
boolean);
226 return add_to_object(
object, name, obj);
231 json_t* obj = json_real(number);
232 return add_to_object(
object, name, obj);
237 json_t* obj = json_string(
string);
238 return add_to_object(
object, name, obj);
243 json_t* obj = json_object();
244 return add_to_object(
object, name, obj);
249 return json_array_append(array, item) == 0;
254 json_t* obj = json_array();
255 return add_to_object(
object, name, obj);
260 return json_dumps(item, JSON_INDENT(2) | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
265 return json_dumps(item, JSON_COMPACT | JSON_ENSURE_ASCII | JSON_SORT_KEYS);
WINPR_JSON * WINPR_JSON_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
WINPR_JSON * WINPR_JSON_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
char * WINPR_JSON_Print(WINPR_JSON *item)
Serialize a JSON instance to string for minimal size without formatting see WINPR_JSON_PrintUnformatt...
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
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_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
WINPR_JSON * WINPR_JSON_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *item)
Check if JSON item is valid.
char * WINPR_JSON_PrintUnformatted(WINPR_JSON *item)
Serialize a JSON instance to string without formatting for human readable formatted output see WINPR_...
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
BOOL WINPR_JSON_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.