summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLloyd Hilaiel <lloyd@hilaiel.com>2011-04-22 19:17:44 -0600
committerLloyd Hilaiel <lloyd@hilaiel.com>2011-04-22 19:17:44 -0600
commit69a9c263b5e3c2019dc258d3e75c95ad7267ea16 (patch)
tree46f88aa049c3016e68a93463ff265fcdd38d879a
parentd24690ee5cc09ea2cf6bbd152f2627e8ee1986e7 (diff)
downloadyajl-69a9c263b5e3c2019dc258d3e75c95ad7267ea16.tar.gz
change types from preprocessor macros to an enum, add yajl_t_any for use with yajl_tree_get()
-rw-r--r--example/parse_config.c2
-rw-r--r--src/api/yajl_tree.h33
-rw-r--r--src/yajl_tree.c19
3 files changed, 29 insertions, 25 deletions
diff --git a/example/parse_config.c b/example/parse_config.c
index e5e32b4..acce032 100644
--- a/example/parse_config.c
+++ b/example/parse_config.c
@@ -59,7 +59,7 @@ main(void)
/* now extract a nested value from the config file */
{
const char * path[] = { "Logging", "timeFormat", (const char *) 0 };
- yajl_val v = yajl_tree_get(node, path, YAJL_TYPE_STRING);
+ yajl_val v = yajl_tree_get(node, path, yajl_t_string);
if (v) printf("Logging/timeFomat: %s\n", YAJL_GET_STRING(v));
else printf("no such node: %s/%s\n", path[0], path[1]);
}
diff --git a/src/api/yajl_tree.h b/src/api/yajl_tree.h
index 301c9b6..c926dec 100644
--- a/src/api/yajl_tree.h
+++ b/src/api/yajl_tree.h
@@ -79,13 +79,16 @@ typedef struct yajl_val_array_s
size_t len;
} yajl_val_array;
-#define YAJL_TYPE_STRING 1
-#define YAJL_TYPE_NUMBER 2
-#define YAJL_TYPE_OBJECT 3
-#define YAJL_TYPE_ARRAY 4
-#define YAJL_TYPE_TRUE 5
-#define YAJL_TYPE_FALSE 6
-#define YAJL_TYPE_NULL 7
+typedef enum {
+ yajl_t_string = 1,
+ yajl_t_number = 2,
+ yajl_t_object = 3,
+ yajl_t_array = 4,
+ yajl_t_true = 5,
+ yajl_t_false = 6,
+ yajl_t_null = 7,
+ yajl_t_any = 8
+} yajl_type;
/**
* Struct describing a general JSON value.
@@ -152,18 +155,18 @@ YAJL_API void yajl_tree_free (yajl_val v);
/**
* Access a nested value.
*/
-YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, int type);
+YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type type);
/* Various convenience macros to check the type of a `yajl_val` */
-#define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_STRING))
-#define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_NUMBER))
+#define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == yajl_t_string))
+#define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == yajl_t_number))
#define YAJL_IS_INTEGER(v) (YAJL_IS_NUMBER(v) && ((v)->data.flags & YAJL_NUMBER_INT_VALID))
#define YAJL_IS_DOUBLE(v) (YAJL_IS_NUMBER(v) && ((v)->data.flags & YAJL_NUMBER_DOUBLE_VALID))
-#define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_OBJECT))
-#define YAJL_IS_ARRAY(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_ARRAY ))
-#define YAJL_IS_TRUE(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_TRUE ))
-#define YAJL_IS_FALSE(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_FALSE ))
-#define YAJL_IS_NULL(v) (((v) != NULL) && ((v)->type == YAJL_TYPE_NULL ))
+#define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == yajl_t_object))
+#define YAJL_IS_ARRAY(v) (((v) != NULL) && ((v)->type == yajl_t_array ))
+#define YAJL_IS_TRUE(v) (((v) != NULL) && ((v)->type == yajl_t_true ))
+#define YAJL_IS_FALSE(v) (((v) != NULL) && ((v)->type == yajl_t_false ))
+#define YAJL_IS_NULL(v) (((v) != NULL) && ((v)->type == yajl_t_null ))
/** Given a yajl_val_string return a ptr to the bare string it contains,
* or NULL if the value is not a string. */
diff --git a/src/yajl_tree.c b/src/yajl_tree.c
index 68ee0cb..a917e4f 100644
--- a/src/yajl_tree.c
+++ b/src/yajl_tree.c
@@ -274,7 +274,7 @@ static int handle_string (void *ctx,
{
yajl_val v;
- v = value_alloc (YAJL_TYPE_STRING);
+ v = value_alloc (yajl_t_string);
if (v == NULL)
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory");
@@ -296,7 +296,7 @@ static int handle_number (void *ctx, const char *string, size_t string_length)
yajl_val_number *n;
char *endptr;
- v = value_alloc (YAJL_TYPE_NUMBER);
+ v = value_alloc (yajl_t_number);
if (v == NULL)
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory");
n = &(v->data.number);
@@ -332,7 +332,7 @@ static int handle_start_map (void *ctx)
yajl_val v;
yajl_val_object *o;
- v = value_alloc (YAJL_TYPE_OBJECT);
+ v = value_alloc (yajl_t_object);
if (v == NULL)
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory");
@@ -360,7 +360,7 @@ static int handle_start_array (void *ctx)
yajl_val v;
yajl_val_array *a;
- v = value_alloc (YAJL_TYPE_ARRAY);
+ v = value_alloc (yajl_t_array);
if (v == NULL)
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory");
@@ -386,7 +386,7 @@ static int handle_boolean (void *ctx, int boolean_value)
{
yajl_val v;
- v = value_alloc (boolean_value ? YAJL_TYPE_TRUE : YAJL_TYPE_FALSE);
+ v = value_alloc (boolean_value ? yajl_t_true : yajl_t_false);
if (v == NULL)
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory");
@@ -397,7 +397,7 @@ static int handle_null (void *ctx)
{
yajl_val v;
- v = value_alloc (YAJL_TYPE_NULL);
+ v = value_alloc (yajl_t_null);
if (v == NULL)
RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory");
@@ -462,13 +462,13 @@ yajl_val yajl_tree_parse (const char *input,
return (ctx.root);
}
-yajl_val yajl_tree_get(yajl_val n, const char ** path, int type)
+yajl_val yajl_tree_get(yajl_val n, const char ** path, yajl_type type)
{
if (!path) return NULL;
while (n && *path) {
unsigned int i;
- if (n->type != YAJL_TYPE_OBJECT) return NULL;
+ if (n->type != yajl_t_object) return NULL;
for (i = 0; i < n->data.object.len; i++) {
if (!strcmp(*path, n->data.object.keys[i])) {
n = n->data.object.values[i];
@@ -478,6 +478,7 @@ yajl_val yajl_tree_get(yajl_val n, const char ** path, int type)
if (i == n->data.object.len) return NULL;
path++;
}
+ if (n && type != yajl_t_any && type != n->type) n = NULL;
return n;
}
@@ -503,7 +504,7 @@ void yajl_tree_free (yajl_val v)
{
yajl_array_free(v);
}
- else /* if (YAJL_TYPE_TRUE or YAJL_TYPE_FALSE or YAJL_TYPE_NULL) */
+ else /* if (yajl_t_true or yajl_t_false or yajl_t_null) */
{
free(v);
}