]> git.ipfire.org Git - pakfire.git/commitdiff
util: Implement adding strings with length to a JSON object
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Oct 2024 14:16:48 +0000 (14:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Oct 2024 14:16:48 +0000 (14:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/util.c

index 41e8501329b34e184c6f8224eb9fd2558e67e642..e8c61557cd9a42f55463754133827f32cef57dc7 100644 (file)
@@ -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);
index 852d7fdfd1bce33ceb2698624ca29c70d9e65344..73d8c7900ed24b92fefbc3e7887f98ed07dfdad8 100644 (file)
@@ -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;