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;
int index;
#if JSONTREE_PRETTY
int indent;
#endif
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];
if(index == 0) {
js_ctx->putchar(v->type);
#if JSONTREE_PRETTY
js_ctx->putchar('\n');
#endif
}
if(index >= o->count) {
#if JSONTREE_PRETTY
js_ctx->putchar('\n');
indent = js_ctx->depth;
while (indent--) {
js_ctx->putchar(' ');
js_ctx->putchar(' ');
}
#endif
js_ctx->putchar(v->type + 2);
/* Default operation: back up one level! */
break;
@ -156,12 +168,26 @@ jsontree_print_next(struct jsontree_context *js_ctx)
if(index > 0) {
js_ctx->putchar(',');
#if JSONTREE_PRETTY
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) {
jsontree_write_string(js_ctx,
((struct jsontree_object *)o)->pairs[index].name);
js_ctx->putchar(':');
#if JSONTREE_PRETTY
js_ctx->putchar(' ');
#endif
ov = ((struct jsontree_object *)o)->pairs[index].value;
} else {
ov = o->values[index];

View file

@ -49,6 +49,12 @@
#define JSONTREE_MAX_DEPTH 10
#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_value *values[JSONTREE_MAX_DEPTH];
uint16_t index[JSONTREE_MAX_DEPTH];