commit
b53a1569d9
|
@ -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
|
||||
|
@ -56,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,
|
||||
|
|
|
@ -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))
|
||||
{
|
||||
|
@ -132,6 +138,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 +154,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 +174,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];
|
||||
|
@ -177,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! */
|
||||
|
@ -198,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);
|
||||
|
|
|
@ -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];
|
||||
|
@ -68,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;
|
||||
|
@ -98,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)}
|
||||
|
@ -130,6 +146,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);
|
||||
|
|
Loading…
Reference in a new issue