From b2c871f97844f6be248a417c2482d0665edc0cfe Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 3 Apr 2018 15:08:40 +0200 Subject: libsmartcols: improve JSON support (add types) This commit add SCOLS_JSON_{NUMBER,STRING,BOOLEAN} to specify column type for JSON output formatting. Signed-off-by: Karel Zak --- libsmartcols/docs/libsmartcols-sections.txt | 2 ++ libsmartcols/src/column.c | 38 +++++++++++++++++++++++++++++ libsmartcols/src/libsmartcols.h.in | 12 +++++++++ libsmartcols/src/libsmartcols.sym | 6 +++++ libsmartcols/src/smartcolsP.h | 2 ++ libsmartcols/src/table_print.c | 18 +++++++++++--- 6 files changed, 75 insertions(+), 3 deletions(-) (limited to 'libsmartcols') diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index 974123540..79786b544 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -22,6 +22,7 @@ libscols_column scols_column_get_color scols_column_get_flags scols_column_get_header +scols_column_get_json_type scols_column_get_safechars scols_column_get_table scols_column_get_whint @@ -37,6 +38,7 @@ scols_column_is_wrap scols_column_set_cmpfunc scols_column_set_color scols_column_set_flags +scols_column_set_json_type scols_column_set_safechars scols_column_set_whint scols_column_set_wrapfunc diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c index b09015afc..e9d6dc404 100644 --- a/libsmartcols/src/column.c +++ b/libsmartcols/src/column.c @@ -171,6 +171,44 @@ int scols_column_set_flags(struct libscols_column *cl, int flags) return 0; } +/** + * scols_column_set_json_type: + * @cl: a pointer to a struct libscols_column instance + * @type: SCOLS_JSON_* type + * + * Sets the type used for JSON formatting, the default is SCOLS_JSON_STRING. + * + * Returns: 0, a negative value in case of an error. + * + * Since: 2.33 + */ +int scols_column_set_json_type(struct libscols_column *cl, int type) +{ + if (!cl) + return -EINVAL; + + cl->json_type = type; + return 0; + +} + +/** + * scols_column_get_json_type: + * @cl: a pointer to a struct libscols_column instance + * + * Note that SCOLS_JSON_BOOLEAN interprets NULL, empty strings, '0', 'N' and + * 'n' as "false"; and everything else as "true". + * + * Returns: JSON type used for formatting or a negative value in case of an error. + * + * Since: 2.33 + */ +int scols_column_get_json_type(const struct libscols_column *cl) +{ + return cl ? cl->json_type : -EINVAL; +} + + /** * scols_column_get_table: * @cl: a pointer to a struct libscols_column instance diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index d99d52ba0..f8be0bc04 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -87,6 +87,15 @@ enum { SCOLS_FL_WRAP = (1 << 6) /* wrap long lines to multi-line cells */ }; +/* + * Column JSON types + */ +enum { + SCOLS_JSON_STRING = 0, /* default */ + SCOLS_JSON_NUMBER = 1, + SCOLS_JSON_BOOLEAN = 2 +}; + /* * Cell flags, see scols_cell_set_flags() before use */ @@ -154,6 +163,9 @@ extern size_t scols_column_get_width(const struct libscols_column *cl); extern int scols_column_set_safechars(struct libscols_column *cl, const char *safe); extern const char *scols_column_get_safechars(const struct libscols_column *cl); +extern int scols_column_set_json_type(struct libscols_column *cl, int type); +extern int scols_column_get_json_type(const struct libscols_column *cl); + extern int scols_column_set_flags(struct libscols_column *cl, int flags); extern int scols_column_get_flags(const struct libscols_column *cl); extern struct libscols_column *scols_new_column(void); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index a4ac39cc9..331a55554 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -176,3 +176,9 @@ SMARTCOLS_2.31 { scols_table_enable_noencoding; scols_table_is_noencoding; } SMARTCOLS_2.30; + + +SMARTCOLS_2.33 { + scols_column_set_json_type; + scols_column_get_json_type; +} SMARTCOLS_2.31; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 510e7a980..f45ac3a24 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -86,6 +86,8 @@ struct libscols_column { size_t width_treeart; /* size of the tree ascii art */ double width_hint; /* hint (N < 1 is in percent of termwidth) */ + int json_type; /* SCOLS_JSON_* */ + int flags; int is_extreme; char *color; /* default column color */ diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 67e51076e..26b138291 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -466,11 +466,23 @@ static int print_data(struct libscols_table *tb, case SCOLS_FMT_JSON: fputs_quoted_json_lower(scols_cell_get_data(&cl->header), tb->out); - fputs(": ", tb->out); + fputs(":", tb->out); if (!*data) fputs("null", tb->out); - else - fputs_quoted_json(data, tb->out); + else switch (cl->json_type) { + case SCOLS_JSON_STRING: + fputs_quoted_json(data, tb->out); + break; + case SCOLS_JSON_NUMBER: + fputs(data, tb->out); + break; + case SCOLS_JSON_BOOLEAN: + fputs(!*data ? "false" : + *data == '0' ? "false" : + *data == 'N' || *data == 'n' ? "false" : "true", + tb->out); + break; + } if (!is_last) fputs(", ", tb->out); return 0; -- cgit v1.2.3-55-g7522