]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: improve JSON support (add types)
authorKarel Zak <kzak@redhat.com>
Tue, 3 Apr 2018 13:08:40 +0000 (15:08 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 3 Apr 2018 13:10:10 +0000 (15:10 +0200)
This commit add SCOLS_JSON_{NUMBER,STRING,BOOLEAN} to specify column
type for JSON output formatting.

Signed-off-by: Karel Zak <kzak@redhat.com>
Documentation/TODO
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/column.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/smartcolsP.h
libsmartcols/src/table_print.c

index 1a183338b2cccc35a817946f46933ea7fb8164b7..14226dc114b694e0f537d31d150587d2e816acee 100644 (file)
@@ -1,13 +1,6 @@
 
  Note that items with (!) have high priority.
 
-libsmartcols
-------------
-   - (!) add libscols_column->json_type and scols_column_set_jsontype()
-     to generate proper JSON output. Now the library uses quotes (= all is
-     string) for everything. See for example SIZE in "lsblk --json --bytes"
-     output.
-
 cal
 ---
    - add option to print calendar in vertical way
index 974123540fd7cc74ee412eca13149afd893a6b9c..79786b544801d0c3cd9ae30f029207f280d9cbbf 100644 (file)
@@ -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
index b09015afc52b261aaf7df2db7c72cfb73cee286c..e9d6dc4041590af3f2c935e93684bb2efdfea0c1 100644 (file)
@@ -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
index d99d52ba0c23d962fb8b503691812a9c6688e66c..f8be0bc04321bf35a52141eeff9902e2793e0061 100644 (file)
@@ -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);
index a4ac39cc9391dc811557b34c41a52db7c65dfe69..331a5555466a770969c2d14110b14c0f81685c05 100644 (file)
@@ -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;
index 510e7a9800f6ca28caf7565fe46695d5b802faf2..f45ac3a242381b492dfe44026515b49263ecb199 100644 (file)
@@ -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 */
index 67e51076eac316c6cc484472a820fbf98c1d829b..26b138291618e0d2640353ad6638fad350991619 100644 (file)
@@ -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;