summaryrefslogtreecommitdiff
path: root/lib/termtable.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/termtable.c')
-rw-r--r--lib/termtable.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/termtable.c b/lib/termtable.c
index 30f1ab74c6..9b36d5ebfa 100644
--- a/lib/termtable.c
+++ b/lib/termtable.c
@@ -7,6 +7,7 @@
#include <zebra.h>
#include <stdio.h>
+#include "lib/json.h"
#include "printfrr.h"
#include "memory.h"
#include "termtable.h"
@@ -485,3 +486,45 @@ char *ttable_dump(struct ttable *tt, const char *newline)
return buf;
}
+
+/* Crude conversion from ttable to json array.
+ * Assume that the first row has column headings.
+ *
+ * Formats are:
+ * d int32
+ * f double
+ * l int64
+ * s string (default)
+ */
+json_object *ttable_json(struct ttable *tt, const char *const formats)
+{
+ struct ttable_cell *row; /* iteration pointers */
+ json_object *json = NULL;
+
+ json = json_object_new_array();
+
+ for (int i = 1; i < tt->nrows; i++) {
+ json_object *jobj;
+ json_object *val;
+
+ row = tt->table[i];
+ jobj = json_object_new_object();
+ json_object_array_add(json, jobj);
+ for (int j = 0; j < tt->ncols; j++) {
+ switch (formats[j]) {
+ case 'd':
+ case 'l':
+ val = json_object_new_int64(atol(row[j].text));
+ break;
+ case 'f':
+ val = json_object_new_double(atof(row[j].text));
+ break;
+ default:
+ val = json_object_new_string(row[j].text);
+ }
+ json_object_object_add(jobj, tt->table[0][j].text, val);
+ }
+ }
+
+ return json;
+}