From 293dc4f4f69f537f4ac2242db8d24160c2a78d81 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 3 Feb 2025 10:26:00 +0000 Subject: [PATCH] json: Add helper function to read integers Signed-off-by: Michael Tremer --- src/pakfire/json.c | 20 ++++++++++++++++++++ src/pakfire/json.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/src/pakfire/json.c b/src/pakfire/json.c index 3c754c04..47c3b7fc 100644 --- a/src/pakfire/json.c +++ b/src/pakfire/json.c @@ -212,3 +212,23 @@ int pakfire_json_add_array(struct json_object* json, const char* name, struct js // Add the object return json_object_object_add(json, name, object); } + +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 = json_object_object_get_ex(json, key, &object); + if (!r) + return -ENOMSG; + + // Check if this is an integer + r = json_object_is_type(object, json_type_int); + if (!r) + return -ENOMSG; + + // Return the value + *value = json_object_get_int64(object); + + return 0; +} diff --git a/src/pakfire/json.h b/src/pakfire/json.h index 2dddce04..e30b0825 100644 --- a/src/pakfire/json.h +++ b/src/pakfire/json.h @@ -45,4 +45,6 @@ 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_int64(struct json_object* json, const char* key, int64_t* value); + #endif /* PAKFIRE_JSON_H */ -- 2.39.5