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);
}
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;
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;
}
#############################################################################*/
#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,
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;
#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,
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;
return r;
// Convert the payload to string
- s = pakfire_json_to_string(json);
+ s = pakfire_json_to_string(json, NULL);
if (!s)
return -EINVAL;