]> git.ipfire.org Git - pakfire.git/commitdiff
json: Add helper function to write JSON data to file
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 12:22:20 +0000 (12:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 12:22:20 +0000 (12:22 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/json.c
src/pakfire/json.h

index c1fb8ceeeeeb4f1e0c64787ae04647c2b77e38f7..ebe6cd71d08557df23c9c5251111fa2915030f04 100644 (file)
@@ -32,6 +32,18 @@ const char* pakfire_json_to_string(struct json_object* json) {
        return json_object_to_json_string_ext(json, flags);
 }
 
+int pakfire_json_to_file(const char* path, id_t owner, gid_t group, mode_t mode,
+               struct json_object* json) {
+       const char* payload = NULL;
+
+       // Serialize the JSON object
+       payload = pakfire_json_to_string(json);
+       if (!payload)
+               return -ENOMEM;
+
+       return pakfire_file_write(path, owner, group, mode, "%s\n", payload);
+}
+
 int pakfire_json_parse(struct json_object** json, char** error,
                const char* buffer, const size_t length) {
        struct json_tokener* tokener = NULL;
index f8d7100fa84cce7401b9d54b4630c55aa4f85318..f0810329c5efdd2fb894c51bd5bfa44c0dfc893e 100644 (file)
 // To String
 const char* pakfire_json_to_string(struct json_object* json);
 
+// To File
+int pakfire_json_to_file(const char* path, id_t owner, gid_t group, mode_t mode,
+       struct json_object* json);
+
 // Parse
 int pakfire_json_parse(struct json_object** json, char** error,
        const char* buffer, const size_t length);