]> git.ipfire.org Git - telemetry.git/commitdiff
file: Implement parsing the entire file as JSON object
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 Oct 2025 16:26:45 +0000 (16:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 Oct 2025 16:26:45 +0000 (16:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/file.c
src/daemon/file.h

index 514ba02009c07ff0dc5b30d383a19b0909aafee9..b8d2082ee3ae7dc96f2f41aee6d6fa522245ad6f 100644 (file)
@@ -25,6 +25,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <systemd/sd-json.h>
+
 #include "ctx.h"
 #include "file.h"
 #include "string.h"
@@ -299,6 +301,21 @@ int td_file_parse_line(td_file* self, td_file_parser* parser, char* line, size_t
        return __td_file_parse(self->ctx, self, 0, line, length, parser);
 }
 
+int td_file_parse_json(td_file* self, sd_json_variant** ret, sd_json_parse_flags_t flags) {
+       unsigned int column = 0;
+       unsigned int line = 0;
+       int r;
+
+       // Parse the file
+       r = sd_json_parse_file(self->f, NULL, flags, ret, &line, &column);
+       if (r < 0) {
+               ERROR(self->ctx, "Failed to parse JSON output in line %u:%u\n", line, column);
+               return r;
+       }
+
+       return 0;
+}
+
 int td_read_uint64(td_ctx* ctx, const char* path, uint64_t* number) {
        td_file* file = NULL;
        int r;
index 8660353b2fbc411fcc04b3f6f2769dfc59c8e671..7f046599e5de3bbe9aaee593c722cc1e3c2b8258 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <stdint.h>
 
+#include <systemd/sd-json.h>
+
 typedef struct td_file td_file;
 
 #include "buffer.h"
@@ -70,6 +72,9 @@ typedef struct td_file_parser {
 int td_file_parse(td_file* self, td_file_parser* parser);
 int td_file_parse_line(td_file* self, td_file_parser* parser, char* line, size_t length);
 
+// Parse JSON
+int td_file_parse_json(td_file* self, sd_json_variant** json, sd_json_parse_flags_t flags);
+
 // Shorthands
 
 int td_read_uint64(td_ctx* ctx, const char* path, uint64_t* number);