diff options
author | Stephen Dolan <mu@netsoc.tcd.ie> | 2013-05-29 11:43:23 +0100 |
---|---|---|
committer | Stephen Dolan <mu@netsoc.tcd.ie> | 2013-05-29 11:47:12 +0100 |
commit | 73446a9cce6ba456800b15287b2425a7e4a3c409 (patch) | |
tree | fb0c69590ff02f08e309b4993de4af0da41b519d | |
parent | dcf1ac0d1f7d102ee583d029bf88aec7c83d7f35 (diff) |
Support for printing object keys in sorted order.
No command-line option to enable this yet. See #79.
-rw-r--r-- | jv.h | 2 | ||||
-rw-r--r-- | jv_print.c | 30 |
2 files changed, 30 insertions, 2 deletions
@@ -117,7 +117,7 @@ jv jv_object_iter_value(jv, int); int jv_get_refcnt(jv); -enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2, JV_PRINT_COLOUR = 4 }; +enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2, JV_PRINT_COLOUR = 4, JV_PRINT_SORTED = 8 }; void jv_dump(jv, int flags); jv jv_dump_string(jv, int flags); @@ -5,6 +5,7 @@ #include "jv_dtoa.h" #include "jv_unicode.h" +#include "jv_aux.h" #define ESC "\033" #define COL(c) (ESC "[" c "m") @@ -194,7 +195,34 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI put_space(indent+INDENT, F, S); } int first = 1; - jv_object_foreach(x, key, value) { + int i; + jv keyset = jv_null(); + while (1) { + jv key, value; + if (flags & JV_PRINT_SORTED) { + if (first) { + keyset = jv_keys(jv_copy(x)); + i = 0; + } else { + i++; + } + if (i >= jv_array_length(jv_copy(keyset))) { + jv_free(keyset); + break; + } + key = jv_array_get(jv_copy(keyset), i); + value = jv_object_get(jv_copy(x), jv_copy(key)); + } else { + if (first) { + i = jv_object_iter(x); + } else { + i = jv_object_iter_next(x, i); + } + if (!jv_object_iter_valid(x, i)) break; + key = jv_object_iter_key(x, i); + value = jv_object_iter_value(x, i); + } + if (!first) { if (flags & JV_PRINT_PRETTY){ put_str(",\n", F, S); |