Support for printing object keys in sorted order.
No command-line option to enable this yet. See #79.
This commit is contained in:
1 parent
dcf1ac0d1f
commit
73446a9cce
2 files changed
+30
-2
No files matched your search
@@ -117,7 +117,7 @@ jv jv_object_iter_value(jv, int);
|
|||||||
|
|
||||||
int jv_get_refcnt(jv);
|
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);
|
void jv_dump(jv, int flags);
|
||||||
jv jv_dump_string(jv, int flags);
|
jv jv_dump_string(jv, int flags);
|
||||||
|
|
||||||
|
|||||||
+29
-1
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include "jv_dtoa.h"
|
#include "jv_dtoa.h"
|
||||||
#include "jv_unicode.h"
|
#include "jv_unicode.h"
|
||||||
|
#include "jv_aux.h"
|
||||||
|
|
||||||
#define ESC "\033"
|
#define ESC "\033"
|
||||||
#define COL(c) (ESC "[" c "m")
|
#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);
|
put_space(indent+INDENT, F, S);
|
||||||
}
|
}
|
||||||
int first = 1;
|
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 (!first) {
|
||||||
if (flags & JV_PRINT_PRETTY){
|
if (flags & JV_PRINT_PRETTY){
|
||||||
put_str(",\n", F, S);
|
put_str(",\n", F, S);
|
||||||
|
|||||||
Reference in new issue
Block a user