On Tue, Jun 12, 2018 at 4:22 PM, <git@xxxxxxxxxxxxxxxxx> wrote: > Add "struct json_writer" and a series of jw_ routines to compose JSON > data into a string buffer. The resulting string may then be printed by > commands wanting to support a JSON-like output format. > > The json_writer is limited to correctly formatting structured data for > output. It does not attempt to build an object model of the JSON data. > > We say "JSON-like" because we do not enforce the Unicode (usually UTF-8) > requirement on string fields. Internally, Git does not necessarily have > Unicode/UTF-8 data for most fields, so it is currently unclear the best > way to enforce that requirement. For example, on Linx pathnames can s/Linx/Linux/ > contain arbitrary 8-bit character data, so a command like "status" would > not know how to encode the reported pathnames. We may want to revisit > this (or double encode such strings) in the future. > > Signed-off-by: Jeff Hostetler <jeffhost@xxxxxxxxxxxxx> > --- > diff --git a/t/helper/test-json-writer.c b/t/helper/test-json-writer.c > @@ -0,0 +1,564 @@ > +void get_s(int line_nr, char **s_in) s/^/static/ > +{ > + *s_in = strtok(NULL, " "); > + if (!*s_in) > + die("line[%d]: expected: <s>", line_nr); > +} > + > +void get_i(int line_nr, intmax_t *s_in) s/^/static/ > +{ > + char *s; > + char *endptr; > + > + get_s(line_nr, &s); > + > + *s_in = strtol(s, &endptr, 10); > + if (*endptr || errno == ERANGE) > + die("line[%d]: invalid integer value", line_nr); > +} > + > +void get_d(int line_nr, double *s_in) s/^/static/ > +{ > + char *s; > + char *endptr; > + > + get_s(line_nr, &s); > + > + *s_in = strtod(s, &endptr); > + if (*endptr || errno == ERANGE) > + die("line[%d]: invalid float value", line_nr); > +}