]> git.ipfire.org Git - pakfire.git/commitdiff
json: Optionally return the length of the serialized string
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 16:47:59 +0000 (16:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 16:47:59 +0000 (16:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/dump.c
src/pakfire/archive.c
src/pakfire/daemon.c
src/pakfire/json.c
src/pakfire/json.h
src/pakfire/oci.c
src/pakfire/xfer.c

index 2f39b6d5f1ef1d21f21b6828f9921238361ef780..2037f92d555e0f96750267e48c897b69f41176fa 100644 (file)
@@ -102,7 +102,7 @@ int cli_dump_json(struct json_object* object) {
        int r;
 
        // Serialize the object as a JSON string
-       const char* buffer = pakfire_json_to_string(object);
+       const char* buffer = pakfire_json_to_string(object, NULL);
 
        // Print to the console
        r = printf("%s\n", buffer);
index 438b3f3db80fec7fb9197424c77e4aed6be15dee..8d21b28f15ea092e221bd721d4d3bf5113cbccb8 100644 (file)
@@ -483,7 +483,7 @@ static int pakfire_archive_parse_json_metadata(struct pakfire_archive* archive,
        }
 
        DEBUG(archive->ctx, "Successfully parsed package metadata:\n%s\n",
-               pakfire_json_to_string(archive->metadata));
+               pakfire_json_to_string(archive->metadata, NULL));
 
        // Success
        r = 0;
index 0e14473c501f8f1c3ed360e78d35367d13c5e66c..37241cbb98f23194148a68791a3f046057fe4fce 100644 (file)
@@ -548,7 +548,7 @@ static int pakfire_daemon_recv(struct pakfire_xfer* xfer,
                        goto ERROR;
 
        } else {
-               ERROR(daemon->ctx, "Unknown message. Ignoring:\n%s\n", pakfire_json_to_string(m));
+               ERROR(daemon->ctx, "Unknown message. Ignoring:\n%s\n", pakfire_json_to_string(m, NULL));
 
                r = 0;
        }
index ebe6cd71d08557df23c9c5251111fa2915030f04..a2e73738cac7fb73c07220730b80a0157317901d 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <string.h>
 #include <sys/mman.h>
 
 #include <pakfire/ctx.h>
 #include <pakfire/logging.h>
 #include <pakfire/util.h>
 
-const char* pakfire_json_to_string(struct json_object* json) {
+const char* pakfire_json_to_string(struct json_object* json, size_t* length) {
+       const char* s = NULL;
+
        const int flags = JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY;
 
-       return json_object_to_json_string_ext(json, flags);
+       // Serialize the object
+       s = json_object_to_json_string_ext(json, flags);
+       if (!s)
+               return NULL;
+
+       // Set length (if requested)
+       if (length)
+               *length = strlen(s);
+
+       return s;
 }
 
 int pakfire_json_to_file(const char* path, id_t owner, gid_t group, mode_t mode,
@@ -37,7 +49,7 @@ int pakfire_json_to_file(const char* path, id_t owner, gid_t group, mode_t mode,
        const char* payload = NULL;
 
        // Serialize the JSON object
-       payload = pakfire_json_to_string(json);
+       payload = pakfire_json_to_string(json, NULL);
        if (!payload)
                return -ENOMEM;
 
index f0810329c5efdd2fb894c51bd5bfa44c0dfc893e..d4b7c36d79c72f0fb9fcfcfef7d695fad78c17da 100644 (file)
@@ -28,7 +28,7 @@
 #include <pakfire/ctx.h>
 
 // To String
-const char* pakfire_json_to_string(struct json_object* json);
+const char* pakfire_json_to_string(struct json_object* json, size_t* length);
 
 // To File
 int pakfire_json_to_file(const char* path, id_t owner, gid_t group, mode_t mode,
index 2e4c2d50a4cae274183742c8da497c0911547404..8985bb4bd5bdeaead01ce143b968b274a1606258 100644 (file)
@@ -112,7 +112,7 @@ static int pakfire_oci_writer_write_json_blob(
        int r;
 
        // Serialize the JSON object
-       buffer = pakfire_json_to_string(json);
+       buffer = pakfire_json_to_string(json, NULL);
        if (!buffer) {
                r = -errno;
                goto ERROR;
index 8a398b0a12b1e96e6026e0955da57bf7f1850b1e..065a304a4966e8f8ea097b10ff346c83cd619ad1 100644 (file)
@@ -729,7 +729,7 @@ int pakfire_xfer_set_json_payload(struct pakfire_xfer* self, struct json_object*
                return r;
 
        // Convert the payload to string
-       s = pakfire_json_to_string(json);
+       s = pakfire_json_to_string(json, NULL);
        if (!s)
                return -EINVAL;