]> git.ipfire.org Git - pakfire.git/commitdiff
json: Add more helper functions to read JSON objects
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Feb 2025 11:34:38 +0000 (11:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Feb 2025 11:34:38 +0000 (11:34 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/json.c
src/pakfire/json.h

index 47c3b7fc1718682c6532bf4c85acd65d8ad02231..f54f4ae1a435d34622a9633b625ef459a01cd592 100644 (file)
@@ -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);
+}
index e30b08257484a5dae971bbb66f16a61577f0b1f0..dcd6df4785228468b89a9d3b77f196605b97a843 100644 (file)
@@ -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 */