jsontree: add JSONTREE_CONF_PRETTY option

This commit is contained in:
Jeff Kent 2015-12-28 09:45:09 -06:00
parent ca919ab0b0
commit fcc87ddce8
2 changed files with 32 additions and 0 deletions

View file

@ -132,6 +132,9 @@ jsontree_print_next(struct jsontree_context *js_ctx)
{ {
struct jsontree_value *v; struct jsontree_value *v;
int index; int index;
#if JSONTREE_PRETTY
int indent;
#endif
v = js_ctx->values[js_ctx->depth]; v = js_ctx->values[js_ctx->depth];
@ -145,10 +148,19 @@ jsontree_print_next(struct jsontree_context *js_ctx)
index = js_ctx->index[js_ctx->depth]; index = js_ctx->index[js_ctx->depth];
if(index == 0) { if(index == 0) {
js_ctx->putchar(v->type); js_ctx->putchar(v->type);
#if JSONTREE_PRETTY
js_ctx->putchar('\n'); js_ctx->putchar('\n');
#endif
} }
if(index >= o->count) { if(index >= o->count) {
#if JSONTREE_PRETTY
js_ctx->putchar('\n'); js_ctx->putchar('\n');
indent = js_ctx->depth;
while (indent--) {
js_ctx->putchar(' ');
js_ctx->putchar(' ');
}
#endif
js_ctx->putchar(v->type + 2); js_ctx->putchar(v->type + 2);
/* Default operation: back up one level! */ /* Default operation: back up one level! */
break; break;
@ -156,12 +168,26 @@ jsontree_print_next(struct jsontree_context *js_ctx)
if(index > 0) { if(index > 0) {
js_ctx->putchar(','); js_ctx->putchar(',');
#if JSONTREE_PRETTY
js_ctx->putchar('\n'); js_ctx->putchar('\n');
#endif
} }
#if JSONTREE_PRETTY
indent = js_ctx->depth + 1;
while (indent--) {
js_ctx->putchar(' ');
js_ctx->putchar(' ');
}
#endif
if(v->type == JSON_TYPE_OBJECT) { if(v->type == JSON_TYPE_OBJECT) {
jsontree_write_string(js_ctx, jsontree_write_string(js_ctx,
((struct jsontree_object *)o)->pairs[index].name); ((struct jsontree_object *)o)->pairs[index].name);
js_ctx->putchar(':'); js_ctx->putchar(':');
#if JSONTREE_PRETTY
js_ctx->putchar(' ');
#endif
ov = ((struct jsontree_object *)o)->pairs[index].value; ov = ((struct jsontree_object *)o)->pairs[index].value;
} else { } else {
ov = o->values[index]; ov = o->values[index];

View file

@ -49,6 +49,12 @@
#define JSONTREE_MAX_DEPTH 10 #define JSONTREE_MAX_DEPTH 10
#endif /* JSONTREE_CONF_MAX_DEPTH */ #endif /* JSONTREE_CONF_MAX_DEPTH */
#ifdef JSONTREE_CONF_PRETTY
#define JSONTREE_PRETTY JSONTREE_CONF_PRETTY
#else
#define JSONTREE_PRETTY 0
#endif /* JSONTREE_CONF_PRETTY */
struct jsontree_context { struct jsontree_context {
struct jsontree_value *values[JSONTREE_MAX_DEPTH]; struct jsontree_value *values[JSONTREE_MAX_DEPTH];
uint16_t index[JSONTREE_MAX_DEPTH]; uint16_t index[JSONTREE_MAX_DEPTH];