Module Name:    othersrc
Committed By:   agc
Date:           Tue Feb 25 08:54:43 UTC 2014

Modified Files:
        othersrc/external/bsd/transit/bin: Makefile
        othersrc/external/bsd/transit/dist: main.c transit.c
Added Files:
        othersrc/external/bsd/transit/bin: 13.expected 14.expected 15.expected

Log Message:
Add the ability to convert back to JSON in transit(1) - so, if desired,
JSON could be transported safely via the transit protocol.

Small fix for dictionary parsing.

Add more tests.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 othersrc/external/bsd/transit/bin/13.expected \
    othersrc/external/bsd/transit/bin/14.expected \
    othersrc/external/bsd/transit/bin/15.expected
cvs rdiff -u -r1.2 -r1.3 othersrc/external/bsd/transit/bin/Makefile
cvs rdiff -u -r1.2 -r1.3 othersrc/external/bsd/transit/dist/main.c \
    othersrc/external/bsd/transit/dist/transit.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/external/bsd/transit/bin/Makefile
diff -u othersrc/external/bsd/transit/bin/Makefile:1.2 othersrc/external/bsd/transit/bin/Makefile:1.3
--- othersrc/external/bsd/transit/bin/Makefile:1.2	Mon Feb 24 21:05:18 2014
+++ othersrc/external/bsd/transit/bin/Makefile	Tue Feb 25 08:54:43 2014
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.2 2014/02/24 21:05:18 agc Exp $
+# $NetBSD: Makefile,v 1.3 2014/02/25 08:54:43 agc Exp $
 
 .include <bsd.own.mk>
 
@@ -78,13 +78,18 @@ t: ${PROG}
 	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -d 12.enc > 12.out
 	diff 12.expected 12.out
 	rm -f 12.enc 12.out
-	@echo "12. json structured data"
-	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -j '{"field1":["potatoes","tomatoes","anykindoftoes",[1,2,3,4,"sublist",{"field1.1":11,"field1.2":"twotwo"}]],"field2":["quayle","bush","shrub","reagan",[3,"nothing"],[4,4,4,4,4,5,6]]}' > 12.enc
-	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -d 12.enc > 12.out
-	diff 12.expected 12.out
-	rm -f 12.enc 12.out
 	@echo "13. json structured data"
-	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -j '{"field1":["potatoes\"\"","tomatoes","anykindoftoes",[1,2,3,4,"sublist",{"field1.1":11,"field1.2":"twotwo"}]],"field2":["quayle","bush","shrub","reagan",[3,"nothing"],[4,4,4,4,4,5,6]]}' > 13.enc
+	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -j '{"field1":["potatoes","tomatoes","anykindoftoes",[1,2,3,4,"sublist",{"field1.1":11,"field1.2":"twotwo"}]],"field2":["quayle","bush","shrub","reagan",[3,"nothing"],[4,4,4,4,4,5,6]]}' > 13.enc
 	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -d 13.enc > 13.out
 	diff 13.expected 13.out
 	rm -f 13.enc 13.out
+	@echo "14. json structured data"
+	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -j '{"field1":["potatoes\"\"","tomatoes","anykindoftoes",[1,2,3,4,"sublist",{"field1.1":11,"field1.2":"twotwo"}]],"field2":["quayle","bush","shrub","reagan",[3,"nothing"],[4,4,4,4,4,5,6]]}' > 14.enc
+	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -d 14.enc > 14.out
+	diff 14.expected 14.out
+	rm -f 14.enc 14.out
+	@echo "15. json structured data as output"
+	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -j '{"field1":["potatoes\"\"","tomatoes","anykindoftoes",[1,2,3,4,"sublist",{"field1.1":11,"field1.2":"twotwo"}]],"field2":["quayle","bush","shrub","reagan",[3,"nothing"],[4,4,4,4,4,5,6]]}' > 15.enc
+	env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -d -j 15.enc > 15.out
+	diff 15.expected 15.out
+	rm -f 15.enc 15.out

Index: othersrc/external/bsd/transit/dist/main.c
diff -u othersrc/external/bsd/transit/dist/main.c:1.2 othersrc/external/bsd/transit/dist/main.c:1.3
--- othersrc/external/bsd/transit/dist/main.c:1.2	Mon Feb 24 21:05:18 2014
+++ othersrc/external/bsd/transit/dist/main.c	Tue Feb 25 08:54:43 2014
@@ -172,6 +172,55 @@ walk(transit_t *t, FILE *fp, int recursi
 	return 1;
 }
 
+static int
+output_json(transit_t *t, uint64_t a, FILE *fp)
+{
+	transit_atom_t	*atom;
+	uint64_t	*v;
+	unsigned	 i;
+
+	atom = transit_atom(t, a);
+	switch(transit_atom_type(atom)) {
+	case TRANSIT_NUMBER:
+		fprintf(fp, "%" PRIu64, transit_atom_size(atom));
+		return 1;
+	case TRANSIT_STRING:
+		fputc('"', fp);
+		fwrite(transit_atom_ptr(atom), 1, transit_atom_size(atom), fp);
+		fputc('"', fp);
+		return 1;
+	case TRANSIT_LIST:
+		fprintf(fp, "[");
+		v = transit_atom_ptr(atom);
+		for (i = 0 ; i < transit_atom_size(atom) ; i++) {
+			output_json(t, v[i], fp);
+			if (i < transit_atom_size(atom) - 2) {
+				fprintf(fp, ",");
+			}
+		}
+		fprintf(fp, "]");
+		return 1;
+	case TRANSIT_DICT:
+		fprintf(fp, "{");
+		v = transit_atom_ptr(atom);
+		for (i = 0 ; i < transit_atom_size(atom) - 1 ; i += 2) {
+			output_json(t, v[i], fp);
+			fprintf(fp, ":");
+			output_json(t, v[i + 1], fp);
+			if (i < transit_atom_size(atom) - 3) {
+				fprintf(fp, ",");
+			}
+		}
+		fprintf(fp, "}");
+		return 1;
+	case TRANSIT_END:
+		return 1;
+	default:
+		fprintf(stderr, "unrecognised atom: %d\n", transit_atom_type(atom));
+		return t->c;
+	}
+}
+
 
 int
 main(int argc, char **argv)
@@ -208,19 +257,23 @@ main(int argc, char **argv)
 		}
 	}
 	if (decoding) {
+		in = NULL;
 		if (optind == argc) {
 			in = dostream(stdin, &size);
 			cc = transit_decode(&t, in, size);
-			walk(&t, stdout, recursive);
-			free(in);
 		} else {
 			for (i = optind ; i < argc ; i++) {
 				in = dofile(argv[i], &size);
 				cc = transit_decode(&t, in, size);
-				walk(&t, stdout, recursive);
-				free(in);
 			}
 		}
+		if (json) {
+			output_json(&t, 0, stdout);
+			fprintf(stdout, "\n");
+		} else {
+			walk(&t, stdout, recursive);
+		}
+		free(in);
 		transit_free(&t);
 	} else {
 		if (json) {
Index: othersrc/external/bsd/transit/dist/transit.c
diff -u othersrc/external/bsd/transit/dist/transit.c:1.2 othersrc/external/bsd/transit/dist/transit.c:1.3
--- othersrc/external/bsd/transit/dist/transit.c:1.2	Mon Feb 24 21:05:18 2014
+++ othersrc/external/bsd/transit/dist/transit.c	Tue Feb 25 08:54:43 2014
@@ -311,13 +311,11 @@ transit_read_json(transit_t *t, const ch
 					i += 1;
 				} else {
 					i = jsize;
-					continue;
+					break;
 				}
 				i += transit_read_json(t, &json[i], jsize - i);
-				if (json[i] == '}' || json[i] == ',') {
+				if (json[i] == ',') {
 					i += 1;
-				} else {
-					i = jsize;
 				}
 			}
 			transit_encode_special(t, TRANSIT_END);

Added files:

Index: othersrc/external/bsd/transit/bin/13.expected
diff -u /dev/null othersrc/external/bsd/transit/bin/13.expected:1.1
--- /dev/null	Tue Feb 25 08:54:43 2014
+++ othersrc/external/bsd/transit/bin/13.expected	Tue Feb 25 08:54:43 2014
@@ -0,0 +1,30 @@
+dict
+	string	field1		list
+		string	potatoes
+		string	tomatoes
+		string	anykindoftoes
+		list
+			number	1
+			number	2
+			number	3
+			number	4
+			string	sublist
+			dict
+				string	field1.1		number	11
+				string	field1.2		string	twotwo
+	string	field2		list
+		string	quayle
+		string	bush
+		string	shrub
+		string	reagan
+		list
+			number	3
+			string	nothing
+		list
+			number	4
+			number	4
+			number	4
+			number	4
+			number	4
+			number	5
+			number	6
Index: othersrc/external/bsd/transit/bin/14.expected
diff -u /dev/null othersrc/external/bsd/transit/bin/14.expected:1.1
--- /dev/null	Tue Feb 25 08:54:43 2014
+++ othersrc/external/bsd/transit/bin/14.expected	Tue Feb 25 08:54:43 2014
@@ -0,0 +1,30 @@
+dict
+	string	field1		list
+		string	potatoes\"\"
+		string	tomatoes
+		string	anykindoftoes
+		list
+			number	1
+			number	2
+			number	3
+			number	4
+			string	sublist
+			dict
+				string	field1.1		number	11
+				string	field1.2		string	twotwo
+	string	field2		list
+		string	quayle
+		string	bush
+		string	shrub
+		string	reagan
+		list
+			number	3
+			string	nothing
+		list
+			number	4
+			number	4
+			number	4
+			number	4
+			number	4
+			number	5
+			number	6
Index: othersrc/external/bsd/transit/bin/15.expected
diff -u /dev/null othersrc/external/bsd/transit/bin/15.expected:1.1
--- /dev/null	Tue Feb 25 08:54:43 2014
+++ othersrc/external/bsd/transit/bin/15.expected	Tue Feb 25 08:54:43 2014
@@ -0,0 +1 @@
+{"field1":["potatoes\"\"","tomatoes","anykindoftoes",[1,2,3,4,"sublist",{"field1.1":11,"field1.2":"twotwo"}]],"field2":["quayle","bush","shrub","reagan",[3,"nothing"],[4,4,4,4,4,5,6]]}

Reply via email to