]> git.ipfire.org Git - pakfire.git/commitdiff
json: Add helper function to read integers
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Feb 2025 10:26:00 +0000 (10:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Feb 2025 10:26:00 +0000 (10:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/json.c
src/pakfire/json.h

index 3c754c048cea0eaff818209c7aa3cd0967aaf4da..47c3b7fc1718682c6532bf4c85acd65d8ad02231 100644 (file)
@@ -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;
+}
index 2dddce04ba7518669981d0c46e91f81ef7e493a5..e30b08257484a5dae971bbb66f16a61577f0b1f0 100644 (file)
@@ -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 */