From 9dae6faf0c82827fa7b238eb37a6d190e9f24279 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 22 Oct 2025 16:26:45 +0000 Subject: [PATCH] file: Implement parsing the entire file as JSON object Signed-off-by: Michael Tremer --- src/daemon/file.c | 17 +++++++++++++++++ src/daemon/file.h | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/src/daemon/file.c b/src/daemon/file.c index 514ba02..b8d2082 100644 --- a/src/daemon/file.c +++ b/src/daemon/file.c @@ -25,6 +25,8 @@ #include #include +#include + #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; diff --git a/src/daemon/file.h b/src/daemon/file.h index 8660353..7f04659 100644 --- a/src/daemon/file.h +++ b/src/daemon/file.h @@ -23,6 +23,8 @@ #include +#include + 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); -- 2.47.3