From d32abad79fd374e0287401ea762b1709f0bb6be8 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 3 Feb 2025 11:34:38 +0000 Subject: [PATCH] json: Add more helper functions to read JSON objects Signed-off-by: Michael Tremer --- src/pakfire/json.c | 43 +++++++++++++++++++++++++++++++++++++++++-- src/pakfire/json.h | 3 +++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/pakfire/json.c b/src/pakfire/json.c index 47c3b7fc..f54f4ae1 100644 --- a/src/pakfire/json.c +++ b/src/pakfire/json.c @@ -213,7 +213,8 @@ int pakfire_json_add_array(struct json_object* json, const char* name, struct js return json_object_object_add(json, name, object); } -int pakfire_json_get_int64(struct json_object* json, const char* key, int64_t* value) { +static int __pakfire_json_get_object(struct json_object* json, + const char* key, const json_type type, struct json_object** o) { struct json_object* object = NULL; int r; @@ -223,12 +224,50 @@ int pakfire_json_get_int64(struct json_object* json, const char* key, int64_t* v return -ENOMSG; // Check if this is an integer - r = json_object_is_type(object, json_type_int); + r = json_object_is_type(object, type); if (!r) return -ENOMSG; + // Return the pointer + *o = object; + + return 0; +} + +int pakfire_json_get_string(struct json_object* json, const char* key, const char** value) { + struct json_object* object = NULL; + int r; + + // Fetch the object + r = __pakfire_json_get_object(json, key, json_type_string, &object); + if (r < 0) + return r; + + // Return the value + *value = json_object_get_string(object); + + return 0; +} + +int pakfire_json_get_int64(struct json_object* json, const char* key, int64_t* value) { + struct json_object* object = NULL; + int r; + + // Fetch the object + r = __pakfire_json_get_object(json, key, json_type_int, &object); + if (r < 0) + return r; + // Return the value *value = json_object_get_int64(object); return 0; } + +int pakfire_json_get_object(struct json_object* json, const char* key, struct json_object** object) { + return __pakfire_json_get_object(json, key, json_type_object, object); +} + +int pakfire_json_get_array(struct json_object* json, const char* key, struct json_object** array) { + return __pakfire_json_get_object(json, key, json_type_array, array); +} diff --git a/src/pakfire/json.h b/src/pakfire/json.h index e30b0825..dcd6df47 100644 --- a/src/pakfire/json.h +++ b/src/pakfire/json.h @@ -45,6 +45,9 @@ int pakfire_json_add_string_array(struct json_object* json, const char* name, ch int pakfire_json_add_object(struct json_object* json, const char* name, struct json_object** o); int pakfire_json_add_array(struct json_object* json, const char* name, struct json_object** array); +int pakfire_json_get_string(struct json_object* json, const char* key, const char** value); int pakfire_json_get_int64(struct json_object* json, const char* key, int64_t* value); +int pakfire_json_get_object(struct json_object* json, const char* key, struct json_object** object); +int pakfire_json_get_array(struct json_object* json, const char* key, struct json_object** array); #endif /* PAKFIRE_JSON_H */ -- 2.39.5