diff options
author | Petri Lehtinen <petri@digip.org> | 2009-11-04 21:24:22 +0200 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2009-11-04 22:09:40 +0200 |
commit | f236c14dc5457a41119492ebce41b8920fd31f65 (patch) | |
tree | 81421d7cf8ce211eed575a4ff74825a48624db8b /src | |
parent | bf01067a8a73b1a69301c99adbfa76ca836cedb7 (diff) | |
download | jansson-f236c14dc5457a41119492ebce41b8920fd31f65.zip jansson-f236c14dc5457a41119492ebce41b8920fd31f65.tar.gz jansson-f236c14dc5457a41119492ebce41b8920fd31f65.tar.bz2 |
dump: Revise whitespace usage
- Never append newline to output
- By default, add spaces between array and object items for more
readable output
- Introduce the flag JSON_COMPACT to not add the aforementioned spaces
Diffstat (limited to 'src')
-rw-r--r-- | src/dump.c | 40 | ||||
-rw-r--r-- | src/jansson.h | 1 |
2 files changed, 25 insertions, 16 deletions
@@ -42,7 +42,7 @@ static int dump_to_file(const char *buffer, int size, void *data) /* 256 spaces (the maximum indentation size) */ static char whitespace[] = " "; -static int dump_indent(unsigned long flags, int depth, dump_func dump, void *data) +static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data) { if(JSON_INDENT(flags) > 0) { @@ -57,6 +57,10 @@ static int dump_indent(unsigned long flags, int depth, dump_func dump, void *dat return -1; } } + else if(space && !(flags & JSON_COMPACT)) + { + return dump(" ", 1, data); + } return 0; } @@ -173,7 +177,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, return -1; if(n == 0) return dump("]", 1, data); - if(dump_indent(flags, depth + 1, dump, data)) + if(dump_indent(flags, depth + 1, 0, dump, data)) return -1; for(i = 0; i < n; ++i) { @@ -184,12 +188,12 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, if(i < n - 1) { if(dump(",", 1, data) || - dump_indent(flags, depth + 1, dump, data)) + dump_indent(flags, depth + 1, 1, dump, data)) return -1; } else { - if(dump_indent(flags, depth, dump, data)) + if(dump_indent(flags, depth, 0, dump, data)) return -1; } } @@ -202,6 +206,17 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, { json_object_t *object; void *iter; + const char *separator; + int separator_length; + + if(flags & JSON_COMPACT) { + separator = ":"; + separator_length = 1; + } + else { + separator = ": "; + separator_length = 2; + } /* detect circular references */ object = json_to_object(json); @@ -215,7 +230,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, return -1; if(!iter) return dump("}", 1, data); - if(dump_indent(flags, depth + 1, dump, data)) + if(dump_indent(flags, depth + 1, 0, dump, data)) return -1; while(iter) @@ -223,7 +238,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, void *next = json_object_iter_next((json_t *)json, iter); dump_string(json_object_iter_key(iter), dump, data); - if(dump(": ", 2, data) || + if(dump(separator, separator_length, data) || do_dump(json_object_iter_value(iter), flags, depth + 1, dump, data)) return -1; @@ -231,12 +246,12 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, if(next) { if(dump(",", 1, data) || - dump_indent(flags, depth + 1, dump, data)) + dump_indent(flags, depth + 1, 1, dump, data)) return -1; } else { - if(dump_indent(flags, depth, dump, data)) + if(dump_indent(flags, depth, 0, dump, data)) return -1; } @@ -270,11 +285,6 @@ char *json_dumps(const json_t *json, unsigned long flags) return NULL; } - if(dump_to_strbuffer("\n", 1, (void *)&strbuff)) { - strbuffer_close(&strbuff); - return NULL; - } - result = strdup(strbuffer_value(&strbuff)); strbuffer_close(&strbuff); @@ -286,9 +296,7 @@ int json_dumpf(const json_t *json, FILE *output, unsigned long flags) if(!json_is_array(json) && !json_is_object(json)) return -1; - if(do_dump(json, flags, 0, dump_to_file, (void *)output)) - return -1; - return dump_to_file("\n", 1, (void *)output); + return do_dump(json, flags, 0, dump_to_file, (void *)output); } int json_dump_file(const json_t *json, const char *path, unsigned long flags) diff --git a/src/jansson.h b/src/jansson.h index 50073d2..a3a97e0 100644 --- a/src/jansson.h +++ b/src/jansson.h @@ -143,6 +143,7 @@ json_t *json_loadf(FILE *input, json_error_t *error); json_t *json_load_file(const char *path, json_error_t *error); #define JSON_INDENT(n) (n & 0xFF) +#define JSON_COMPACT 0x100 char *json_dumps(const json_t *json, unsigned long flags); int json_dumpf(const json_t *json, FILE *output, unsigned long flags); |