23#include <winpr/file.h>
24#include <winpr/json.h>
25#include <winpr/assert.h>
27#if !defined(WITH_JSONC)
28#error "This file must only be compiled when json-c is enabled"
32#if JSON_C_MAJOR_VERSION == 0
33#if JSON_C_MINOR_VERSION < 14
34static struct json_object* json_object_new_null(
void)
43 return _snprintf(buffer, len,
"json-c %s", json_c_version());
48 return json_tokener_parse(value);
53 WINPR_ASSERT(buffer_length <= INT_MAX);
54 json_tokener* tok = json_tokener_new();
57 json_object* obj = json_tokener_parse_ex(tok, value, (
int)buffer_length);
58 json_tokener_free(tok);
64 json_object_put((json_object*)item);
69 return json_object_array_get_idx((
const json_object*)array, index);
74 return json_object_array_length((
const json_object*)array);
79 struct json_object_iterator it = json_object_iter_begin((
const json_object*)
object);
80 struct json_object_iterator itEnd = json_object_iter_end((
const json_object*)
object);
81 while (!json_object_iter_equal(&it, &itEnd))
83 const char* key = json_object_iter_peek_name(&it);
84 if (_stricmp(key,
string) == 0)
86 return json_object_iter_peek_value(&it);
88 json_object_iter_next(&it);
95 return json_object_object_get((
const json_object*)
object,
string);
100 return json_object_object_get_ex((
const json_object*)
object,
string, NULL);
105 return json_util_get_last_err();
110 return json_object_get_string((json_object*)item);
115 return json_object_get_double((
const json_object*)item);
137 if (!json_object_is_type((
const json_object*)item, json_type_boolean))
139 json_bool val = json_object_get_boolean((
const json_object*)item);
145 if (!json_object_is_type((
const json_object*)item, json_type_boolean))
147 json_bool val = json_object_get_boolean((
const json_object*)item);
153 return json_object_is_type((
const json_object*)item, json_type_boolean);
158 return json_object_is_type((
const json_object*)item, json_type_null);
163 return json_object_is_type((
const json_object*)item, json_type_int) ||
164 json_object_is_type((
const json_object*)item, json_type_double);
169 return json_object_is_type((
const json_object*)item, json_type_string);
174 return json_object_is_type((
const json_object*)item, json_type_array);
179 return json_object_is_type((
const json_object*)item, json_type_object);
184 return json_object_new_null();
189 return json_object_new_boolean(TRUE);
194 return json_object_new_boolean(FALSE);
199 return json_object_new_boolean(
boolean);
204 return json_object_new_double(num);
209 return json_object_new_string(
string);
214 return json_object_new_array();
219 return json_object_new_object();
224 struct json_object* obj = json_object_new_null();
225 if (json_object_object_add((json_object*)
object, name, obj) != 0)
227 json_object_put(obj);
235 struct json_object* obj = json_object_new_boolean(TRUE);
236 if (json_object_object_add((json_object*)
object, name, obj) != 0)
238 json_object_put(obj);
246 struct json_object* obj = json_object_new_boolean(FALSE);
247 if (json_object_object_add((json_object*)
object, name, obj) != 0)
249 json_object_put(obj);
257 struct json_object* obj = json_object_new_boolean(
boolean);
258 if (json_object_object_add((json_object*)
object, name, obj) != 0)
260 json_object_put(obj);
268 struct json_object* obj = json_object_new_double(number);
269 if (json_object_object_add((json_object*)
object, name, obj) != 0)
271 json_object_put(obj);
279 struct json_object* obj = json_object_new_string(
string);
280 if (json_object_object_add((json_object*)
object, name, obj) != 0)
282 json_object_put(obj);
290 struct json_object* obj = json_object_new_object();
291 if (json_object_object_add((json_object*)
object, name, obj) != 0)
293 json_object_put(obj);
301 const int rc = json_object_array_add((json_object*)array, (json_object*)item);
309 struct json_object* obj = json_object_new_array();
310 if (json_object_object_add((json_object*)
object, name, obj) != 0)
312 json_object_put(obj);
320 const char* str = json_object_to_json_string_ext((json_object*)item, JSON_C_TO_STRING_PRETTY);
328 const char* str = json_object_to_json_string_ext((json_object*)item, JSON_C_TO_STRING_PLAIN);
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.