json_writer: Removed automatic json-object type from the constructor

Top level can be any json type and can be created using
jsonw_start_object/jsonw_end_object etc.

Signed-off-by: Anuradha Karuppiah <anuradhak@cumulusnetworks.com>
This commit is contained in:
Anuradha Karuppiah 2016-06-22 06:45:51 -07:00 committed by Stephen Hemminger
parent 7a4559f67c
commit d721a14590
3 changed files with 17 additions and 4 deletions

View File

@ -33,7 +33,7 @@ struct json_writer {
static void jsonw_indent(json_writer_t *self) static void jsonw_indent(json_writer_t *self)
{ {
unsigned i; unsigned i;
for (i = 0; i <= self->depth; ++i) for (i = 0; i < self->depth; ++i)
fputs(" ", self->out); fputs(" ", self->out);
} }
@ -102,7 +102,6 @@ json_writer_t *jsonw_new(FILE *f)
self->depth = 0; self->depth = 0;
self->pretty = false; self->pretty = false;
self->sep = '\0'; self->sep = '\0';
putc('{', self->out);
} }
return self; return self;
} }
@ -113,8 +112,7 @@ void jsonw_destroy(json_writer_t **self_p)
json_writer_t *self = *self_p; json_writer_t *self = *self_p;
assert(self->depth == 0); assert(self->depth == 0);
jsonw_eol(self); fputs("\n", self->out);
fputs("}\n", self->out);
fflush(self->out); fflush(self->out);
free(self); free(self);
*self_p = NULL; *self_p = NULL;
@ -269,6 +267,7 @@ int main(int argc, char **argv)
{ {
json_writer_t *wr = jsonw_new(stdout); json_writer_t *wr = jsonw_new(stdout);
jsonw_start_object(wr);
jsonw_pretty(wr, true); jsonw_pretty(wr, true);
jsonw_name(wr, "Vyatta"); jsonw_name(wr, "Vyatta");
jsonw_start_object(wr); jsonw_start_object(wr);
@ -305,6 +304,7 @@ int main(int argc, char **argv)
jsonw_end_object(wr); jsonw_end_object(wr);
jsonw_end_object(wr);
jsonw_destroy(&wr); jsonw_destroy(&wr);
return 0; return 0;
} }

View File

@ -245,6 +245,7 @@ static void dump_raw_db(FILE *fp, int to_hist)
h = hist_db; h = hist_db;
if (jw) { if (jw) {
jsonw_start_object(jw);
jsonw_pretty(jw, pretty); jsonw_pretty(jw, pretty);
jsonw_name(jw, info_source); jsonw_name(jw, info_source);
jsonw_start_object(jw); jsonw_start_object(jw);
@ -287,6 +288,8 @@ static void dump_raw_db(FILE *fp, int to_hist)
} }
} }
if (jw) { if (jw) {
jsonw_end_object(jw);
jsonw_end_object(jw); jsonw_end_object(jw);
jsonw_destroy(&jw); jsonw_destroy(&jw);
} }
@ -451,6 +454,7 @@ static void dump_kern_db(FILE *fp)
struct ifstat_ent *n; struct ifstat_ent *n;
if (jw) { if (jw) {
jsonw_start_object(jw);
jsonw_pretty(jw, pretty); jsonw_pretty(jw, pretty);
jsonw_name(jw, info_source); jsonw_name(jw, info_source);
jsonw_start_object(jw); jsonw_start_object(jw);
@ -477,6 +481,7 @@ static void dump_incr_db(FILE *fp)
h = hist_db; h = hist_db;
if (jw) { if (jw) {
jsonw_start_object(jw);
jsonw_pretty(jw, pretty); jsonw_pretty(jw, pretty);
jsonw_name(jw, info_source); jsonw_name(jw, info_source);
jsonw_start_object(jw); jsonw_start_object(jw);
@ -508,6 +513,8 @@ static void dump_incr_db(FILE *fp)
} }
if (jw) { if (jw) {
jsonw_end_object(jw);
jsonw_end_object(jw); jsonw_end_object(jw);
jsonw_destroy(&jw); jsonw_destroy(&jw);
} }

View File

@ -285,6 +285,7 @@ static void dump_kern_db(FILE *fp, int to_hist)
h = hist_db; h = hist_db;
if (jw) { if (jw) {
jsonw_start_object(jw);
jsonw_pretty(jw, pretty); jsonw_pretty(jw, pretty);
jsonw_name(jw, info_source); jsonw_name(jw, info_source);
jsonw_start_object(jw); jsonw_start_object(jw);
@ -317,6 +318,8 @@ static void dump_kern_db(FILE *fp, int to_hist)
} }
if (jw) { if (jw) {
jsonw_end_object(jw);
jsonw_end_object(jw); jsonw_end_object(jw);
jsonw_destroy(&jw); jsonw_destroy(&jw);
} }
@ -329,6 +332,7 @@ static void dump_incr_db(FILE *fp)
h = hist_db; h = hist_db;
if (jw) { if (jw) {
jsonw_start_object(jw);
jsonw_pretty(jw, pretty); jsonw_pretty(jw, pretty);
jsonw_name(jw, info_source); jsonw_name(jw, info_source);
jsonw_start_object(jw); jsonw_start_object(jw);
@ -364,6 +368,8 @@ static void dump_incr_db(FILE *fp)
} }
if (jw) { if (jw) {
jsonw_end_object(jw);
jsonw_end_object(jw); jsonw_end_object(jw);
jsonw_destroy(&jw); jsonw_destroy(&jw);
} }