will result in more compact output by instead putting each
JSON object on a single line.
+ * `--tab`:
+
+ Use a tab for each indentation level instead of two spaces.
+
+ * `--indent n`:
+
+ Use the given number of spaces (no more than 8) for indentation.
+
* `--colour-output` / `-C` and `--monochrome-output` / `-M`:
By default, jq outputs colored JSON if writing to a
int jv_get_refcnt(jv);
-enum {
+enum jv_print_flags {
JV_PRINT_PRETTY = 1,
JV_PRINT_ASCII = 2,
JV_PRINT_COLOUR = 4,
JV_PRINT_SORTED = 8,
JV_PRINT_INVALID = 16,
JV_PRINT_REFCOUNT = 32,
+ JV_PRINT_TAB = 64,
+ JV_PRINT_SPACE0 = 256,
+ JV_PRINT_SPACE1 = 512,
+ JV_PRINT_SPACE2 = 1024,
};
+#define JV_PRINT_INDENT_FLAGS(n) \
+ ((n) < 0 || (n) > 7 ? JV_PRINT_TAB | JV_PRINT_PRETTY : (n) == 0 ? 0 : (n) << 8 | JV_PRINT_PRETTY)
void jv_dumpf(jv, FILE *f, int flags);
void jv_dump(jv, int flags);
void jv_show(jv, int flags);
put_buf(s, strlen(s), fout, strout);
}
-static void put_space(int n, FILE* fout, jv* strout) {
- while (n--) {
- put_char(' ', fout, strout);
+static void put_indent(int n, int flags, FILE* fout, jv* strout) {
+ if (flags & JV_PRINT_TAB) {
+ while (n--)
+ put_char('\t', fout, strout);
+ } else {
+ n *= ((flags & (JV_PRINT_SPACE0 | JV_PRINT_SPACE1 | JV_PRINT_SPACE2)) >> 8);
+ while (n--)
+ put_char(' ', fout, strout);
}
}
put_char('"', F, S);
}
-enum { INDENT = 2 };
-
static void put_refcnt(struct dtoa_context* C, int refcnt, FILE *F, jv* S){
char buf[JVP_DTOA_FMT_MAX_LEN];
put_char(' ', F, S);
put_str("[", F, S);
if (flags & JV_PRINT_PRETTY) {
put_char('\n', F, S);
- put_space(indent+INDENT, F, S);
+ put_indent(indent + 1, flags, F, S);
}
jv_array_foreach(x, i, elem) {
if (i!=0) {
if (flags & JV_PRINT_PRETTY) {
put_str(",\n", F, S);
- put_space(indent+INDENT, F, S);
+ put_indent(indent + 1, flags, F, S);
} else {
put_str(",", F, S);
}
}
- jv_dump_term(C, elem, flags, indent + INDENT, F, S);
+ jv_dump_term(C, elem, flags, indent + 1, F, S);
if (colour) put_str(colour, F, S);
}
if (flags & JV_PRINT_PRETTY) {
put_char('\n', F, S);
- put_space(indent, F, S);
+ put_indent(indent, flags, F, S);
}
if (colour) put_str(colour, F, S);
put_char(']', F, S);
put_char('{', F, S);
if (flags & JV_PRINT_PRETTY) {
put_char('\n', F, S);
- put_space(indent+INDENT, F, S);
+ put_indent(indent + 1, flags, F, S);
}
int first = 1;
int i = 0;
if (!first) {
if (flags & JV_PRINT_PRETTY){
put_str(",\n", F, S);
- put_space(indent+INDENT, F, S);
+ put_indent(indent + 1, flags, F, S);
} else {
put_str(",", F, S);
}
put_str((flags & JV_PRINT_PRETTY) ? ": " : ":", F, S);
if (colour) put_str(COLRESET, F, S);
- jv_dump_term(C, value, flags, indent + INDENT, F, S);
+ jv_dump_term(C, value, flags, indent + 1, F, S);
if (colour) put_str(colour, F, S);
}
if (flags & JV_PRINT_PRETTY) {
put_char('\n', F, S);
- put_space(indent, F, S);
+ put_indent(indent, flags, F, S);
}
if (colour) put_str(colour, F, S);
put_char('}', F, S);
/* This one is nice for use in debuggers */
void jv_show(jv x, int flags) {
if (flags == -1)
- flags = JV_PRINT_PRETTY | JV_PRINT_COLOUR;
+ flags = JV_PRINT_PRETTY | JV_PRINT_COLOUR | JV_PRINT_INDENT_FLAGS(2);
jv_dumpf(jv_copy(x), stderr, flags | JV_PRINT_INVALID);
fflush(stderr);
}
"\t -C\t\tcolorize JSON;\n"
"\t -M\t\tmonochrome (don't colorize JSON);\n"
"\t -S\t\tsort keys of objects on output;\n"
+ "\t --tab\tuse tabs for indentation;\n"
"\t --arg a v\tset variable $a to value <v>;\n"
"\t --argjson a v\tset variable $a to JSON value <v>;\n"
"\t --slurpfile a f\tset variable $a to an array of JSON texts read from <f>;\n"
RAW_INPUT = 2,
PROVIDE_NULL = 4,
RAW_OUTPUT = 8,
- COMPACT_OUTPUT = 16,
ASCII_OUTPUT = 32,
COLOUR_OUTPUT = 64,
NO_COLOUR_OUTPUT = 128,
goto out;
}
+ int dumpopts = JV_PRINT_INDENT_FLAGS(2);
const char* program = 0;
jq_util_input_state input_state = jq_util_input_init(NULL, NULL); // XXX add err_cb
if (!short_opts) continue;
}
if (isoption(argv[i], 'c', "compact-output", &short_opts)) {
- options |= COMPACT_OUTPUT;
+ dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
if (!short_opts) continue;
}
if (isoption(argv[i], 'C', "color-output", &short_opts)) {
options |= RAW_OUTPUT | RAW_NO_LF;
if (!short_opts) continue;
}
+ if (isoption(argv[i], 0, "tab", &short_opts)) {
+ dumpopts &= ~JV_PRINT_INDENT_FLAGS(7);
+ dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;
+ if (!short_opts) continue;
+ }
+ if (isoption(argv[i], 0, "indent", &short_opts)) {
+ if (i >= argc - 1) {
+ fprintf(stderr, "%s: --indent takes one parameter\n", progname);
+ die();
+ }
+ dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
+ int indent = atoi(argv[i+1]);
+ if (indent < -1 || indent > 7) {
+ fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname);
+ die();
+ }
+ dumpopts |= JV_PRINT_INDENT_FLAGS(indent);
+ i++;
+ if (!short_opts) continue;
+ }
if (isoption(argv[i], 0, "seq", &short_opts)) {
options |= SEQ;
if (!short_opts) continue;
// FIXME: For --arg* we should check that the varname is acceptable
if (isoption(argv[i], 0, "arg", &short_opts)) {
if (i >= argc - 2) {
- fprintf(stderr, "%s: --arg takes two parameters (e.g. -a varname value)\n", progname);
+ fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname);
die();
}
jv arg = jv_object();
}
if (isoption(argv[i], 0, "argjson", &short_opts)) {
if (i >= argc - 2) {
- fprintf(stderr, "%s: --argjson takes two parameters (e.g. -a varname text)\n", progname);
+ fprintf(stderr, "%s: --argjson takes two parameters (e.g. --argjson varname text)\n", progname);
die();
}
jv v = jv_parse(argv[i+2]);
else
which = "slurpfile";
if (i >= argc - 2) {
- fprintf(stderr, "%s: --%s takes two parameters (e.g. -a varname filename)\n", progname, which);
+ fprintf(stderr, "%s: --%s takes two parameters (e.g. --%s varname filename)\n", progname, which, which);
die();
}
jv arg = jv_object();
}
}
- int dumpopts = 0;
#ifndef WIN32
/* Disable colour by default on Windows builds as Windows
terminals tend not to display it correctly */
dumpopts |= JV_PRINT_COLOUR;
#endif
if (options & SORTED_OUTPUT) dumpopts |= JV_PRINT_SORTED;
- if (!(options & COMPACT_OUTPUT)) dumpopts |= JV_PRINT_PRETTY;
if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII;
if (options & COLOUR_OUTPUT) dumpopts |= JV_PRINT_COLOUR;
if (options & NO_COLOUR_OUTPUT) dumpopts &= ~JV_PRINT_COLOUR;