Skip to contentSkip to conversation

json_writer V9

PATCH New [email protected] 3 participants 4 messages Jun 12, 2018 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v9 1/1 json_writer V9

From: Jeff Hostetler <[email protected]>

Here is V9 of my json-writer patches. Please replace the existing V5..V8 versions with this one.

This version has been rebased onto v2.18.0-rc1 rather than 2.17 because of changes to the test-tool setup.

I've incorporated all of the suggestions on the V8 version, including Eric's suggestion to make the test tool read test data from stdin rather than using command line arguments.

I also incorporated Eric's PERLJSON lazy prereq suggestion and squashed the perl unit test into the main commit.

Jeff Hostetler (1):
json_writer: new routines to create JSON data

 Makefile                    |   2 +
 json-writer.c               | 414 ++++++++++++++++++++++++++++++++
 json-writer.h               | 105 +++++++++
 t/helper/test-json-writer.c | 564 ++++++++++++++++++++++++++++++++++++++++++++
 t/helper/test-tool.c        |   1 +
 t/helper/test-tool.h        |   1 +
 t/t0019-json-writer.sh      | 331 ++++++++++++++++++++++++++
 t/t0019/parse_json.perl     |  52 ++++
 8 files changed, 1470 insertions(+)
 create mode 100644 json-writer.c
 create mode 100644 json-writer.h
 create mode 100644 t/helper/test-json-writer.c
 create mode 100755 t/t0019-json-writer.sh
 create mode 100644 t/t0019/parse_json.perl
Signature · 1 line
2.9.3
#1 lore raw

PATCH v9 1/1 json_writer: new routines to create JSON data

From: Jeff Hostetler <[email protected]>

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 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.

---
 Makefile                    |   2 +
 json-writer.c               | 414 ++++++++++++++++++++++++++++++++
 json-writer.h               | 105 +++++++++
 t/helper/test-json-writer.c | 564 ++++++++++++++++++++++++++++++++++++++++++++
 t/helper/test-tool.c        |   1 +
 t/helper/test-tool.h        |   1 +
 t/t0019-json-writer.sh      | 331 ++++++++++++++++++++++++++
 t/t0019/parse_json.perl     |  52 ++++
 8 files changed, 1470 insertions(+)
 create mode 100644 json-writer.c
 create mode 100644 json-writer.h
 create mode 100644 t/helper/test-json-writer.c
 create mode 100755 t/t0019-json-writer.sh
 create mode 100644 t/t0019/parse_json.perl
Makefile +2 -0
index 1d27f36..5a781e2 100644
@@ -709,6 +709,7 @@ TEST_BUILTINS_OBJS += test-example-decorate.o
 TEST_BUILTINS_OBJS += test-genrandom.o
 TEST_BUILTINS_OBJS += test-hashmap.o
 TEST_BUILTINS_OBJS += test-index-version.o
+TEST_BUILTINS_OBJS += test-json-writer.o
 TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
 TEST_BUILTINS_OBJS += test-match-trees.o
 TEST_BUILTINS_OBJS += test-mergesort.o
@@ -871,6 +872,7 @@ LIB_OBJS += hashmap.o
 LIB_OBJS += help.o
 LIB_OBJS += hex.o
 LIB_OBJS += ident.o
+LIB_OBJS += json-writer.o
 LIB_OBJS += kwset.o
 LIB_OBJS += levenshtein.o
 LIB_OBJS += line-log.o
json-writer.c new file +414 -0

414 lines not on this page · raw message

json-writer.h new file +105 -0

105 lines not on this page · raw message

t/helper/test-json-writer.c new file +564 -0

564 lines not on this page · raw message

t/helper/test-tool.c +1 -0
index 805a45d..707499e 100644
@@ -19,6 +19,7 @@ static struct test_cmd cmds[] = {
 	{ "genrandom", cmd__genrandom },
 	{ "hashmap", cmd__hashmap },
 	{ "index-version", cmd__index_version },
+	{ "json-writer", cmd__json_writer },
 	{ "lazy-init-name-hash", cmd__lazy_init_name_hash },
 	{ "match-trees", cmd__match_trees },
 	{ "mergesort", cmd__mergesort },
t/helper/test-tool.h +1 -0
index 7116ddf..a058c31 100644
@@ -13,6 +13,7 @@ int cmd__example_decorate(int argc, const char **argv);
 int cmd__genrandom(int argc, const char **argv);
 int cmd__hashmap(int argc, const char **argv);
 int cmd__index_version(int argc, const char **argv);
+int cmd__json_writer(int argc, const char **argv);
 int cmd__lazy_init_name_hash(int argc, const char **argv);
 int cmd__match_trees(int argc, const char **argv);
 int cmd__mergesort(int argc, const char **argv);
t/t0019-json-writer.sh new file +331 -0

331 lines not on this page · raw message

t/t0019/parse_json.perl new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH v9] json_writer: new routines to create JSON data

Quoted 11 lines · On Tue, Jun 12, 2018 at 4:22 PM, <[email protected]> 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/

t/helper/test-json-writer.c line 1

Quoted 9 lines

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.

---

t/helper/test-json-writer.c +1 -0
@@ -0,0 +1,564 @@
+void get_s(int line_nr, char **s_in)

s/^/static/

t/helper/test-json-writer.c line 396

Quoted 7 lines
t/helper/test-json-writer.c +7 -0
+{
+       *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/

t/helper/test-json-writer.c line 408

Quoted 12 lines
t/helper/test-json-writer.c +12 -0
+{
+       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/

Quoted 10 lines
t/helper/test-json-writer.c +10 -0
+{
+       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);
+}
#3 lore raw

Re: [PATCH v9] json_writer: new routines to create JSON data

In reply to [email protected]
Quoted 9 lines · [email protected] writes:
json-writer.c new file +9 -0
+static void indent_pretty(struct json_writer *jw)
+{
+	int k;
+
+	if (!jw->pretty)
+		return;
+
+	for (k = 0; k < jw->open_stack.len; k++)
+		strbuf_addstr(&jw->json, "  ");

json-writer.c line 127

Quoted 10 lines
json-writer.c new file +9 -0
+}
 
+static void array_common(struct json_writer *jw)
+{
+	assert_in_array(jw);
+	maybe_add_comma(jw);
+
+	if (jw->pretty)
+		strbuf_addch(&jw->json, '\n');
+	indent_pretty(jw);

Make it

	if (jw->pretty) {
		strbuf_addch(&jw->json, '\n');
		indent_pretty(jw);
	}

and lose "be noop unless jw->pretty" check from the indent function.

Most other codepaths that cares about pretty printing state use that "I check and decide to (or not to) call helpers that unconditionally do" pattern, and the way indent_pretty() is used in the patch stands out like a sore thumb.