From: Michael Tremer Date: Wed, 25 Jun 2025 16:47:59 +0000 (+0000) Subject: json: Optionally return the length of the serialized string X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1f07e7e8cdf1a39081b29c5a2bcfc451fbc50eff;p=pakfire.git json: Optionally return the length of the serialized string Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/dump.c b/src/cli/lib/dump.c index 2f39b6d5..2037f92d 100644 --- a/src/cli/lib/dump.c +++ b/src/cli/lib/dump.c @@ -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); diff --git a/src/pakfire/archive.c b/src/pakfire/archive.c index 438b3f3d..8d21b28f 100644 --- a/src/pakfire/archive.c +++ b/src/pakfire/archive.c @@ -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; diff --git a/src/pakfire/daemon.c b/src/pakfire/daemon.c index 0e14473c..37241cbb 100644 --- a/src/pakfire/daemon.c +++ b/src/pakfire/daemon.c @@ -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; } diff --git a/src/pakfire/json.c b/src/pakfire/json.c index ebe6cd71..a2e73738 100644 --- a/src/pakfire/json.c +++ b/src/pakfire/json.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include @@ -26,10 +27,21 @@ #include #include -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; diff --git a/src/pakfire/json.h b/src/pakfire/json.h index f0810329..d4b7c36d 100644 --- a/src/pakfire/json.h +++ b/src/pakfire/json.h @@ -28,7 +28,7 @@ #include // 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, diff --git a/src/pakfire/oci.c b/src/pakfire/oci.c index 2e4c2d50..8985bb4b 100644 --- a/src/pakfire/oci.c +++ b/src/pakfire/oci.c @@ -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; diff --git a/src/pakfire/xfer.c b/src/pakfire/xfer.c index 8a398b0a..065a304a 100644 --- a/src/pakfire/xfer.c +++ b/src/pakfire/xfer.c @@ -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;