mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
Pretty-printing of JSON values.
This commit is contained in:
@@ -58,7 +58,7 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
|
||||
printf(" %04d", pc + imm);
|
||||
} else if (op->flags & OP_HAS_CONSTANT) {
|
||||
printf(" ");
|
||||
jv_dump(jv_array_get(jv_copy(bc->constants), imm));
|
||||
jv_dump(jv_array_get(jv_copy(bc->constants), imm), 0);
|
||||
} else if (op->flags & OP_HAS_VARIABLE) {
|
||||
uint16_t v = bc->code[pc++];
|
||||
printf(" v%d", v);
|
||||
|
||||
4
c/jv.h
4
c/jv.h
@@ -102,7 +102,9 @@ jv jv_object_iter_value(jv, int);
|
||||
|
||||
int jv_get_refcnt(jv);
|
||||
|
||||
void jv_dump(jv);
|
||||
enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2 };
|
||||
void jv_dump(jv, int);
|
||||
|
||||
jv jv_parse(const char* string);
|
||||
jv jv_parse_sized(const char* string, int length);
|
||||
|
||||
|
||||
50
c/jv_print.c
50
c/jv_print.c
@@ -64,7 +64,9 @@ static void jv_dump_string(jv str, int ascii_only) {
|
||||
assert(c != -1);
|
||||
}
|
||||
|
||||
static void jv_dump_term(struct dtoa_context* C, jv x) {
|
||||
enum { INDENT = 2 };
|
||||
|
||||
static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent) {
|
||||
char buf[JVP_DTOA_FMT_MAX_LEN];
|
||||
switch (jv_get_kind(x)) {
|
||||
case JV_KIND_INVALID:
|
||||
@@ -99,33 +101,51 @@ static void jv_dump_term(struct dtoa_context* C, jv x) {
|
||||
putchar('"');
|
||||
break;
|
||||
case JV_KIND_ARRAY: {
|
||||
printf("[");
|
||||
for (int i=0; i<jv_array_length(jv_copy(x)); i++) {
|
||||
if (i!=0) printf(", ");
|
||||
jv_dump(jv_array_get(jv_copy(x), i));
|
||||
if (jv_array_length(jv_copy(x)) == 0) {
|
||||
printf("[]");
|
||||
break;
|
||||
}
|
||||
printf("[");
|
||||
if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent+INDENT, "");
|
||||
for (int i=0; i<jv_array_length(jv_copy(x)); i++) {
|
||||
if (i!=0) {
|
||||
if (flags & JV_PRINT_PRETTY) printf(",\n%*s", indent+INDENT, "");
|
||||
else printf(", ");
|
||||
}
|
||||
jv_dump_term(C, jv_array_get(jv_copy(x), i), flags, indent + INDENT);
|
||||
}
|
||||
if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent, "");
|
||||
printf("]");
|
||||
break;
|
||||
}
|
||||
case JV_KIND_OBJECT: {
|
||||
printf("{");
|
||||
int first = 1;
|
||||
for (int i = jv_object_iter(x); jv_object_iter_valid(x,i); i = jv_object_iter_next(x,i)) {
|
||||
if (!first) printf(", ");
|
||||
first = 0;
|
||||
jv_dump(jv_object_iter_key(x, i));
|
||||
printf(": ");
|
||||
jv_dump(jv_object_iter_value(x, i));
|
||||
if (jv_object_length(jv_copy(x)) == 0) {
|
||||
printf("{}");
|
||||
break;
|
||||
}
|
||||
printf("{");
|
||||
if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent+INDENT, "");
|
||||
int first = 1;
|
||||
jv_object_foreach(i, x) {
|
||||
if (!first) {
|
||||
if (flags & JV_PRINT_PRETTY) printf(",\n%*s", indent+INDENT, "");
|
||||
else printf(", ");
|
||||
}
|
||||
first = 0;
|
||||
jv_dump_term(C, jv_object_iter_key(x, i), flags, indent + INDENT);
|
||||
printf(": ");
|
||||
jv_dump_term(C, jv_object_iter_value(x, i), flags, indent + INDENT);
|
||||
}
|
||||
if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent, "");
|
||||
printf("}");
|
||||
}
|
||||
}
|
||||
jv_free(x);
|
||||
}
|
||||
|
||||
void jv_dump(jv x) {
|
||||
void jv_dump(jv x, int flags) {
|
||||
struct dtoa_context C;
|
||||
jvp_dtoa_context_init(&C);
|
||||
jv_dump_term(&C, x);
|
||||
jv_dump_term(&C, x, flags, 0);
|
||||
jvp_dtoa_context_free(&C);
|
||||
}
|
||||
|
||||
8
c/main.c
8
c/main.c
@@ -71,9 +71,9 @@ void run_tests() {
|
||||
break;
|
||||
} else if (!jv_equal(jv_copy(expected), jv_copy(actual))) {
|
||||
printf("Expected ");
|
||||
jv_dump(jv_copy(expected));
|
||||
jv_dump(jv_copy(expected), 0);
|
||||
printf(", but got ");
|
||||
jv_dump(jv_copy(actual));
|
||||
jv_dump(jv_copy(actual), 0);
|
||||
printf("\n");
|
||||
pass = 0;
|
||||
}
|
||||
@@ -84,7 +84,7 @@ void run_tests() {
|
||||
jv extra = jq_next();
|
||||
if (jv_is_valid(extra)) {
|
||||
printf("Superfluous result: ");
|
||||
jv_dump(extra);
|
||||
jv_dump(extra, 0);
|
||||
printf("\n");
|
||||
pass = 0;
|
||||
} else {
|
||||
@@ -121,7 +121,7 @@ int main(int argc, char* argv[]) {
|
||||
jq_init(bc, value);
|
||||
jv result;
|
||||
while (jv_is_valid(result = jq_next())) {
|
||||
jv_dump(result);
|
||||
jv_dump(result, JV_PRINT_PRETTY);
|
||||
printf("\n");
|
||||
}
|
||||
jv_free(result);
|
||||
|
||||
Reference in New Issue
Block a user