diff options
author | Stephen Dolan <mu@netsoc.tcd.ie> | 2012-09-11 15:51:12 +0100 |
---|---|---|
committer | Stephen Dolan <mu@netsoc.tcd.ie> | 2012-09-11 15:53:37 +0100 |
commit | d9d6f434079f842674becdea31521f54655d5bdd (patch) | |
tree | 727d7d0a71867ba0f2fb5852729038dbff18f413 | |
parent | 51a44edc63bf4f1749458faf5ae631e0d5023ba9 (diff) |
Pretty-printing of JSON values.
-rw-r--r-- | c/bytecode.c | 2 | ||||
-rw-r--r-- | c/jv.h | 4 | ||||
-rw-r--r-- | c/jv_print.c | 38 | ||||
-rw-r--r-- | c/main.c | 8 |
4 files changed, 37 insertions, 15 deletions
diff --git a/c/bytecode.c b/c/bytecode.c index 208e82f0..bafd0474 100644 --- a/c/bytecode.c +++ b/c/bytecode.c @@ -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); @@ -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); diff --git a/c/jv_print.c b/c/jv_print.c index e122a5c8..f1ec92fb 100644 --- a/c/jv_print.c +++ b/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: { + 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) printf(", "); - jv_dump(jv_array_get(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: { + if (jv_object_length(jv_copy(x)) == 0) { + printf("{}"); + break; + } printf("{"); + if (flags & JV_PRINT_PRETTY) printf("\n%*s", indent+INDENT, ""); 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(", "); + jv_object_foreach(i, x) { + if (!first) { + if (flags & JV_PRINT_PRETTY) printf(",\n%*s", indent+INDENT, ""); + else printf(", "); + } first = 0; - jv_dump(jv_object_iter_key(x, i)); + jv_dump_term(C, jv_object_iter_key(x, i), flags, indent + INDENT); printf(": "); - jv_dump(jv_object_iter_value(x, i)); + 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); } @@ -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); |