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 <dsterba@suse.com>
This commit is contained in:
David Sterba 2023-08-17 22:50:41 +02:00
parent 29060ec176
commit b890183072
2 changed files with 12 additions and 0 deletions

View File

@ -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] == '%') {

View File

@ -18,6 +18,7 @@
#define __BTRFS_FORMAT_OUTPUT_H__
#include <stddef.h>
#include <stdbool.h>
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,