git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC/PATCH 2/3] add a library of code for producing structured output

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 11, 2010, 18:16 UTC
Message-ID
<7vy6gtonwt.fsf@alter.siamese.dyndns.org>
In-Reply-To
<20100411113733.80010.3767.julian@quantumfyre.co.uk>
Julian Phillips <julian@quantumfyre.co.uk> writes:
Show 5 quoted lines
> Add a library that allows commands to produce structured output in any
> of a range of formats using a single API.
>
> The API includes an OPT_OUTPUT and handle_output_arg so that the
> option handling for different commands will be as similar as possible.

I was hoping that the existing low-level -z routines (e.g. "diff-* -z") follow similar enough patterns to have a corresponding output-z.c and be handled inside output.c library. But that is not a requirement, just "would have been nicer if the original were written that way".

Show 22 quoted lines
> diff --git a/output-json.c b/output-json.c
> new file mode 100644
> index 0000000..0eb66b2
> --- /dev/null
> +++ b/output-json.c
> @@ -0,0 +1,128 @@
> +#include "git-compat-util.h"
> +#include "output.h"
> +#include "strbuf.h"
> +
> +static char *json_quote(char *s)
> +{
> +	struct strbuf buf = STRBUF_INIT;
> +
> +	while (*s) {
> +		switch (*s) {
> +...
> +		default:
> +			/* All control characters must be encode, even if they
> +			 * don't have a specific escape character of their own */
> +			if (*s < 0x20)
> +				strbuf_addf(&buf, "\\u%04x", *s);

As you didn't say your "char" is either signed or unsigned upfront, this will behave differently when you are fed a UTF-8 string. If it is signed, you will end up showing bytes in a single letter separately at wrong codepoint, and if it is unsigned, you will give UTF-8 string unquoted, which probably is what you meant to do.

What is your design intention regarding legacy encoding? This code does not yet declare "dear user, if you plan to use json/xml output, your repository metadata (notably the pathnames) has to be in UTF-8", as the caller _could_ transliterate legacy data before feeding it to output.c layer. An alternative would be for the output.c layer to know about the encoding of incoming data and transliterate when the output format requires a particular encoding.

Show 15 quoted lines
> +static void json_obj_item_start(FILE *file, char *name, int first)
> +{
> +	char *quoted = json_quote(name);
> +	if (!first)
> +		fprintf(file, ",\n");
> +	fprintf(file, "\"%s\" : ", quoted);
> +	free(quoted);
> +}
> + ...
> +static void json_str(FILE *file, char *value)
> +{
> +	char *quoted = json_quote(value);
> +	fprintf(file, "\"%s\"", quoted);
> +	free(quoted);
> +}

An obvious improvement would be to make json_quote() to take FILE * to avoid wasteful allocation and copy, as it doesn't do anything but addstr and addch, and all of its callers don't do anything but spitting the result out to FILE *.

Show 8 quoted lines
> diff --git a/output-xml.c b/output-xml.c
> new file mode 100644
> index 0000000..50dd7d6
> --- /dev/null
> +++ b/output-xml.c
> @@ -0,0 +1,68 @@
> +#include "git-compat-util.h"
> +#include "output.h"

This seems to totally lack quoting of any metacharacters for "name" and string "value".

Previous: Jakub NarebskiNext: Sverre Rabbelier
Message 9 of 24 in “JSON/XML output for scripting interface”
  1. 0/3 JSON/XML output for scripting interfaceJulian Phillips, Apr 11, 2010
  2. 1/3 strbuf: Add strbuf_vaddf functionJulian Phillips, Apr 11, 2010
  3. Erik Faye-LundApr 11, 2010
  4. Julian PhillipsApr 11, 2010
  5. 2/3 add a library of code for producing structured outputJulian Phillips, Apr 11, 2010
  6. Erik Faye-LundApr 11, 2010
  7. Julian PhillipsApr 11, 2010
  8. Jakub NarebskiApr 11, 2010
  9. Junio C HamanoApr 11, 2010
  10. Sverre RabbelierApr 11, 2010
  11. Julian PhillipsApr 11, 2010
  12. Jakub NarebskiApr 11, 2010
  13. Julian PhillipsApr 11, 2010
  14. Eric RaymondApr 11, 2010
  15. 3/3 status: add support for structured outputJulian Phillips, Apr 11, 2010
  16. Sverre RabbelierApr 11, 2010
  17. Julian PhillipsApr 11, 2010
  18. Sverre RabbelierApr 11, 2010
  19. Julian PhillipsApr 11, 2010
  20. Sverre RabbelierApr 11, 2010
  21. Jon SeymourApr 11, 2010
  22. Eric RaymondApr 11, 2010
  23. Jon SeymourApr 11, 2010
  24. Julian PhillipsApr 11, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.