jsontree: add uint type

This commit is contained in:
Jeff Kent 2015-12-28 09:45:23 -06:00
parent fcc87ddce8
commit bcc7d0a1eb
3 changed files with 24 additions and 6 deletions

View File

@ -45,6 +45,7 @@
#define JSON_TYPE_PAIR ':' #define JSON_TYPE_PAIR ':'
#define JSON_TYPE_PAIR_NAME 'N' /* for N:V pairs */ #define JSON_TYPE_PAIR_NAME 'N' /* for N:V pairs */
#define JSON_TYPE_STRING '"' #define JSON_TYPE_STRING '"'
#define JSON_TYPE_UINT 'U'
#define JSON_TYPE_INT 'I' #define JSON_TYPE_INT 'I'
#define JSON_TYPE_NUMBER '0' #define JSON_TYPE_NUMBER '0'
#define JSON_TYPE_ERROR 0 #define JSON_TYPE_ERROR 0

View File

@ -79,16 +79,11 @@ jsontree_write_string(const struct jsontree_context *js_ctx, const char *text)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void 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]; char buf[10];
int l; int l;
if(value < 0) {
js_ctx->putchar('-');
value = -value;
}
l = sizeof(buf) - 1; l = sizeof(buf) - 1;
do { do {
buf[l--] = '0' + (value % 10); buf[l--] = '0' + (value % 10);
@ -101,6 +96,17 @@ jsontree_write_int(const struct jsontree_context *js_ctx, int value)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void 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, jsontree_setup(struct jsontree_context *js_ctx, struct jsontree_value *root,
int (* putchar)(int)) 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); jsontree_write_string(js_ctx, ((struct jsontree_string *)v)->value);
/* Default operation: back up one level! */ /* Default operation: back up one level! */
break; 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: case JSON_TYPE_INT:
jsontree_write_int(js_ctx, ((struct jsontree_int *)v)->value); jsontree_write_int(js_ctx, ((struct jsontree_int *)v)->value);
/* Default operation: back up one level! */ /* Default operation: back up one level! */

View File

@ -74,6 +74,11 @@ struct jsontree_string {
const char *value; const char *value;
}; };
struct jsontree_uint {
uint8_t type;
unsigned int value;
};
struct jsontree_int { struct jsontree_int {
uint8_t type; uint8_t type;
int value; 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, const char *jsontree_path_name(const struct jsontree_context *js_ctx,
int depth); 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_int(const struct jsontree_context *js_ctx, int value);
void jsontree_write_atom(const struct jsontree_context *js_ctx, void jsontree_write_atom(const struct jsontree_context *js_ctx,
const char *text); const char *text);