From b890183072d136cc4d5c3055e1e66ad2ca3785a3 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 17 Aug 2023 22:50:41 +0200 Subject: [PATCH] btrfs-progs: support unquoted values in json For null or boolean values the "..." quoting must not be done, add support for that. This is detected internally for each printed value. Signed-off-by: David Sterba --- common/format-output.c | 9 +++++++++ common/format-output.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/common/format-output.c b/common/format-output.c index 67cc71be..b55f8038 100644 --- a/common/format-output.c +++ b/common/format-output.c @@ -142,6 +142,12 @@ static void fmt_separator(struct format_ctx *fctx) } } +/* Detect formats or values that must not be quoted (null, bool) */ +static bool fmt_set_unquoted(struct format_ctx *fctx, const struct rowspec *row) +{ + return false; +} + void fmt_start(struct format_ctx *fctx, const struct rowspec *spec, int width, int indent) { @@ -206,6 +212,7 @@ void fmt_start_value(struct format_ctx *fctx, const struct rowspec *row) } else if (bconf.output_format == CMD_FORMAT_JSON) { if (strcmp(row->fmt, "list") == 0) { } else if (strcmp(row->fmt, "map") == 0) { + } else if (fctx->unquoted) { } else { putchar('"'); } @@ -224,6 +231,7 @@ void fmt_end_value(struct format_ctx *fctx, const struct rowspec *row) if (bconf.output_format == CMD_FORMAT_JSON) { if (strcmp(row->fmt, "list") == 0) { } else if (strcmp(row->fmt, "map") == 0) { + } else if (fctx->unquoted) { } else { putchar('"'); } @@ -319,6 +327,7 @@ void fmt_print(struct format_ctx *fctx, const char* key, ...) } } + fctx->unquoted = fmt_set_unquoted(fctx, row); fmt_start_value(fctx, row); if (row->fmt[0] == '%') { diff --git a/common/format-output.h b/common/format-output.h index ab12a7f1..cbd32658 100644 --- a/common/format-output.h +++ b/common/format-output.h @@ -18,6 +18,7 @@ #define __BTRFS_FORMAT_OUTPUT_H__ #include +#include struct rowspec { /* Identifier for the row */ @@ -73,6 +74,8 @@ struct format_ctx { char jtype[JSON_NESTING_LIMIT]; enum json_type memb[JSON_NESTING_LIMIT]; + /* Set if the value needs to be printed unquoted */ + bool unquoted; }; void fmt_start(struct format_ctx *fctx, const struct rowspec *spec, int width,