From fcc87ddce83ce7a78494457f0128b07812fe0ca2 Mon Sep 17 00:00:00 2001 From: Jeff Kent Date: Mon, 28 Dec 2015 09:45:09 -0600 Subject: [PATCH 1/3] jsontree: add JSONTREE_CONF_PRETTY option --- apps/json/jsontree.c | 26 ++++++++++++++++++++++++++ apps/json/jsontree.h | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/apps/json/jsontree.c b/apps/json/jsontree.c index 45f305869..e397ae47b 100644 --- a/apps/json/jsontree.c +++ b/apps/json/jsontree.c @@ -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]; diff --git a/apps/json/jsontree.h b/apps/json/jsontree.h index 491e29ac9..b378d36b5 100644 --- a/apps/json/jsontree.h +++ b/apps/json/jsontree.h @@ -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]; From bcc7d0a1eb7b1ac87ba5f0e0707bdcb6391adfac Mon Sep 17 00:00:00 2001 From: Jeff Kent Date: Mon, 28 Dec 2015 09:45:23 -0600 Subject: [PATCH 2/3] jsontree: add uint type --- apps/json/json.h | 1 + apps/json/jsontree.c | 22 ++++++++++++++++------ apps/json/jsontree.h | 7 +++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/apps/json/json.h b/apps/json/json.h index a698464a5..d91cf93ef 100644 --- a/apps/json/json.h +++ b/apps/json/json.h @@ -45,6 +45,7 @@ #define JSON_TYPE_PAIR ':' #define JSON_TYPE_PAIR_NAME 'N' /* for N:V pairs */ #define JSON_TYPE_STRING '"' +#define JSON_TYPE_UINT 'U' #define JSON_TYPE_INT 'I' #define JSON_TYPE_NUMBER '0' #define JSON_TYPE_ERROR 0 diff --git a/apps/json/jsontree.c b/apps/json/jsontree.c index e397ae47b..4ffe68161 100644 --- a/apps/json/jsontree.c +++ b/apps/json/jsontree.c @@ -79,16 +79,11 @@ jsontree_write_string(const struct jsontree_context *js_ctx, const char *text) } /*---------------------------------------------------------------------------*/ void -jsontree_write_int(const struct jsontree_context *js_ctx, int value) +jsontree_write_uint(const struct jsontree_context *js_ctx, unsigned int value) { char buf[10]; int l; - if(value < 0) { - js_ctx->putchar('-'); - value = -value; - } - l = sizeof(buf) - 1; do { buf[l--] = '0' + (value % 10); @@ -101,6 +96,17 @@ jsontree_write_int(const struct jsontree_context *js_ctx, int value) } /*---------------------------------------------------------------------------*/ void +jsontree_write_int(const struct jsontree_context *js_ctx, int value) +{ + if(value < 0) { + js_ctx->putchar('-'); + value = -value; + } + + jsontree_write_uint(js_ctx, value); +} +/*---------------------------------------------------------------------------*/ +void jsontree_setup(struct jsontree_context *js_ctx, struct jsontree_value *root, int (* putchar)(int)) { @@ -203,6 +209,10 @@ jsontree_print_next(struct jsontree_context *js_ctx) jsontree_write_string(js_ctx, ((struct jsontree_string *)v)->value); /* Default operation: back up one level! */ break; + case JSON_TYPE_UINT: + jsontree_write_uint(js_ctx, ((struct jsontree_uint *)v)->value); + /* Default operation: back up one level! */ + break; case JSON_TYPE_INT: jsontree_write_int(js_ctx, ((struct jsontree_int *)v)->value); /* Default operation: back up one level! */ diff --git a/apps/json/jsontree.h b/apps/json/jsontree.h index b378d36b5..3434565a2 100644 --- a/apps/json/jsontree.h +++ b/apps/json/jsontree.h @@ -74,6 +74,11 @@ struct jsontree_string { const char *value; }; +struct jsontree_uint { + uint8_t type; + unsigned int value; +}; + struct jsontree_int { uint8_t type; int value; @@ -136,6 +141,8 @@ void jsontree_reset(struct jsontree_context *js_ctx); const char *jsontree_path_name(const struct jsontree_context *js_ctx, int depth); +void jsontree_write_uint(const struct jsontree_context *js_ctx, + unsigned int value); void jsontree_write_int(const struct jsontree_context *js_ctx, int value); void jsontree_write_atom(const struct jsontree_context *js_ctx, const char *text); From a65d566dee9b4a82dff0969d9e6e7cd91e11183d Mon Sep 17 00:00:00 2001 From: Jeff Kent Date: Mon, 28 Dec 2015 09:45:29 -0600 Subject: [PATCH 3/3] jsontree: add int pointer types --- apps/json/json.h | 8 ++++++++ apps/json/jsontree.c | 24 ++++++++++++++++++++++++ apps/json/jsontree.h | 5 +++++ 3 files changed, 37 insertions(+) diff --git a/apps/json/json.h b/apps/json/json.h index d91cf93ef..51ef652f8 100644 --- a/apps/json/json.h +++ b/apps/json/json.h @@ -57,6 +57,14 @@ #define JSON_TYPE_CALLBACK 'C' +/* integer pointer types */ +#define JSON_TYPE_S8PTR 'b' +#define JSON_TYPE_U8PTR 'B' +#define JSON_TYPE_S16PTR 'w' +#define JSON_TYPE_U16PTR 'W' +#define JSON_TYPE_S32PTR 'd' +#define JSON_TYPE_U32PTR 'D' + enum { JSON_ERROR_OK, JSON_ERROR_SYNTAX, diff --git a/apps/json/jsontree.c b/apps/json/jsontree.c index 4ffe68161..13d7d8604 100644 --- a/apps/json/jsontree.c +++ b/apps/json/jsontree.c @@ -234,6 +234,30 @@ jsontree_print_next(struct jsontree_context *js_ctx) } /* Default operation: back up one level! */ break; + case JSON_TYPE_S8PTR: + jsontree_write_int(js_ctx, *((int8_t *)((struct jsontree_ptr *)v)->value)); + /* Default operation: back up one level! */ + break; + case JSON_TYPE_U8PTR: + jsontree_write_uint(js_ctx, *((uint8_t *)((struct jsontree_ptr *)v)->value)); + /* Default operation: back up one level! */ + break; + case JSON_TYPE_S16PTR: + jsontree_write_int(js_ctx, *((int16_t *)((struct jsontree_ptr *)v)->value)); + /* Default operation: back up one level! */ + break; + case JSON_TYPE_U16PTR: + jsontree_write_uint(js_ctx, *((uint16_t *)((struct jsontree_ptr *)v)->value)); + /* Default operation: back up one level! */ + break; + case JSON_TYPE_S32PTR: + jsontree_write_int(js_ctx, *((int32_t *)((struct jsontree_ptr *)v)->value)); + /* Default operation: back up one level! */ + break; + case JSON_TYPE_U32PTR: + jsontree_write_uint(js_ctx, *((uint32_t *)((struct jsontree_ptr *)v)->value)); + /* Default operation: back up one level! */ + break; } default: PRINTF("\nError: Illegal json type:'%c'\n", v->type); diff --git a/apps/json/jsontree.h b/apps/json/jsontree.h index 3434565a2..a6c349d80 100644 --- a/apps/json/jsontree.h +++ b/apps/json/jsontree.h @@ -109,6 +109,11 @@ struct jsontree_array { struct jsontree_value **values; }; +struct jsontree_ptr { + uint8_t type; + const void *value; +}; + #define JSONTREE_STRING(text) {JSON_TYPE_STRING, (text)} #define JSONTREE_PAIR(name, value) {(name), (struct jsontree_value *)(value)} #define JSONTREE_CALLBACK(output, set) {JSON_TYPE_CALLBACK, (output), (set)}