From 3e5949f1ca33c0d58a2d68fe6fa6fe0016a125af Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 5 Oct 2024 14:16:48 +0000 Subject: [PATCH] util: Implement adding strings with length to a JSON object Signed-off-by: Michael Tremer --- src/libpakfire/include/pakfire/util.h | 1 + src/libpakfire/util.c | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 41e850132..e8c61557c 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -91,6 +91,7 @@ struct json_object* pakfire_json_parse(struct pakfire_ctx* ctx, const char* buffer, const size_t length); struct json_object* pakfire_json_parse_from_file(struct pakfire_ctx* ctx, const char* path); int pakfire_json_add_string(struct json_object* json, const char* name, const char* value); +int pakfire_json_add_stringn(struct json_object* json, const char* name, const char* value, size_t length); int pakfire_json_add_int64(struct json_object* json, const char* name, int64_t value); int pakfire_json_add_uint64(struct json_object* json, const char* name, uint64_t value); int pakfire_json_add_double(struct json_object* json, const char* name, double value); diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 852d7fdfd..73d8c7900 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -715,12 +715,20 @@ struct json_object* pakfire_json_parse_from_file(struct pakfire_ctx* ctx, const int pakfire_json_add_string(struct json_object* json, const char* name, const char* value) { + if (!value) + return 0; + + return pakfire_json_add_stringn(json, name, value, strlen(value)); +} + +int pakfire_json_add_stringn(struct json_object* json, + const char* name, const char* value, size_t length) { // No string? Nothing to do if (!value) return 0; // Convert string to JSON object - struct json_object* object = json_object_new_string(value); + struct json_object* object = json_object_new_string_len(value, length); if (!object) return 1; -- 2.47.2