[ovs-dev] [PATCH 2/2] ovsdb-client: Add JSON output format.

Ben Pfaff blp at nicira.com
Tue Nov 2 22:08:45 UTC 2010


Requested-by: DK Moon <dkmoon at nicira.com>
CC: DK Moon <dkmoon at nicira.com>
---
 AUTHORS                 |    1 +
 ovsdb/ovsdb-client.1.in |   27 ++++++++++++++++++++++-
 ovsdb/ovsdb-client.c    |   52 +++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 76 insertions(+), 4 deletions(-)

diff --git a/AUTHORS b/AUTHORS
index 6a81668..0cbd468 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -40,6 +40,7 @@ Brandon Heller          brandonh at stanford.edu
 Bryan Fulton            bryan at nicira.com
 Cedric Hobbs            cedric at nicira.com
 Derek Cormier           derek.cormier at lab.ntt.co.jp
+DK Moon                 dkmoon at nicira.com
 Ghanem Bahri            bahri.ghanem at gmail.com
 Henrik Amren            henrik at nicira.com
 Jad Naous               jnaous at gmail.com
diff --git a/ovsdb/ovsdb-client.1.in b/ovsdb/ovsdb-client.1.in
index 8bd1c12..d7e71a3 100644
--- a/ovsdb/ovsdb-client.1.in
+++ b/ovsdb/ovsdb-client.1.in
@@ -123,18 +123,41 @@ Text-based tables with aligned columns.
 HTML tables.
 .IP "\fBcvs\fR"
 Comma-separated values as defined in RFC 4180.
+.IP "\fBjson\fR"
+JSON format as defined in RFC 4627.  The output is a sequence of JSON
+objects, each of which corresponds to one table.  Each JSON object has
+the following members with the noted values:
+.RS
+.IP "\fBcaption\fR"
+The table's caption.  This member is omitted if the table has no
+caption.
+.IP "\fBheadings\fR"
+An array with one element per table column.  Each array element is a
+string giving the corresponding column's heading.
+.IP "\fBdata\fR"
+An array with one element per table row.  Each element is also an
+array with one element per table column.  The elements of this
+second-level array are the cells that constitute the table.  Cells
+that represent OVSDB data or data types are expressed in the format
+described in the OVSDB specification; other cells are simply expressed
+as text strings.
+.RE
 .RE
 .
 .IP "\fB\-d \fIformat\fR"
 .IP "\fB\-\-data=\fIformat\fR"
-Sets the formatting for cells within output tables.  The following
-types of \fIformat\fR are available:
+Sets the formatting for cells within output tables for \fBtable\fR,
+\fBhtml\fR, and \fBcsv\fR output formats only.  The following
+\fIformat\fR types are available:
 .RS
 .IP "\fBstring\fR (default)"
 The simple format described in \fBovs\-vsctl\fR(8).
 .IP "\fBjson\fR"
 JSON.
 .RE
+.IP
+This option is ignored with \fB\-f json\fR, which always outputs cells
+in JSON format.
 .
 .IP "\fB\-\-no\-heading\fR"
 This option suppresses the heading row that otherwise appears in the
diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c
index 8502f19..6d19e5d 100644
--- a/ovsdb/ovsdb-client.c
+++ b/ovsdb/ovsdb-client.c
@@ -49,7 +49,8 @@ VLOG_DEFINE_THIS_MODULE(ovsdb_client);
 static enum {
     FMT_TABLE,                  /* Textual table. */
     FMT_HTML,                   /* HTML table. */
-    FMT_CSV                     /* Comma-separated lines. */
+    FMT_CSV,                    /* Comma-separated lines. */
+    FMT_JSON                    /* JSON. */
 } output_format;
 
 /* --no-headings: Whether table output should include headings. */
@@ -119,6 +120,8 @@ parse_options(int argc, char *argv[])
                 output_format = FMT_HTML;
             } else if (!strcmp(optarg, "csv")) {
                 output_format = FMT_CSV;
+            } else if (!strcmp(optarg, "json")) {
+                output_format = FMT_JSON;
             } else {
                 ovs_fatal(0, "unknown output format \"%s\"", optarg);
             }
@@ -196,7 +199,8 @@ usage(void)
     stream_usage("SERVER", true, true, true);
     printf("\nOutput formatting options:\n"
            "  -f, --format=FORMAT         set output formatting to FORMAT\n"
-           "                              (\"table\", \"html\", or \"csv\"\n"
+           "                              (\"table\", \"html\", \"csv\", "
+           "or \"json\")\n"
            "  --no-headings               omit table heading row\n"
            "  --pretty                    pretty-print JSON in output");
     daemon_usage();
@@ -677,6 +681,46 @@ table_print_csv__(const struct table *table)
 }
 
 static void
+table_print_json__(const struct table *table)
+{
+    struct json *json, *headings, *data;
+    size_t x, y;
+    char *s;
+
+    json = json_object_create();
+    if (table->caption) {
+        json_object_put_string(json, "caption", table->caption);
+    }
+
+    headings = json_array_create_empty();
+    for (x = 0; x < table->n_columns; x++) {
+        const struct column *column = &table->columns[x];
+        json_array_add(headings, json_string_create(column->heading));
+    }
+    json_object_put(json, "headings", headings);
+
+    data = json_array_create_empty();
+    for (y = 0; y < table->n_rows; y++) {
+        struct json *row = json_array_create_empty();
+        for (x = 0; x < table->n_columns; x++) {
+            const struct cell *cell = table_cell__(table, y, x);
+            if (cell->text) {
+                json_array_add(row, json_string_create(cell->text));
+            } else {
+                json_array_add(row, json_clone(cell->json));
+            }
+        }
+        json_array_add(data, row);
+    }
+    json_object_put(json, "data", data);
+
+    s = json_to_string(json, json_flags);
+    json_destroy(json);
+    puts(s);
+    free(s);
+}
+
+static void
 table_print(const struct table *table)
 {
     switch (output_format) {
@@ -691,6 +735,10 @@ table_print(const struct table *table)
     case FMT_CSV:
         table_print_csv__(table);
         break;
+
+    case FMT_JSON:
+        table_print_json__(table);
+        break;
     }
 }
 
-- 
1.7.1





More information about the dev mailing list