From: Michael Tremer Date: Wed, 25 Jun 2025 12:22:20 +0000 (+0000) Subject: json: Add helper function to write JSON data to file X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1055d20a3603c3cb3084b65e7d2aac5db60ee700;p=pakfire.git json: Add helper function to write JSON data to file Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/json.c b/src/pakfire/json.c index c1fb8cee..ebe6cd71 100644 --- a/src/pakfire/json.c +++ b/src/pakfire/json.c @@ -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; diff --git a/src/pakfire/json.h b/src/pakfire/json.h index f8d7100f..f0810329 100644 --- a/src/pakfire/json.h +++ b/src/pakfire/json.h @@ -30,6 +30,10 @@ // 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);