Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience wrapper for yajl_tree_get #70

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/api/yajl_tree.h
Expand Up @@ -145,6 +145,18 @@ YAJL_API void yajl_tree_free (yajl_val v);
*/
YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type type);

/**
* Access a value inside an object.
*
* \param object the object from which you'd like to extract values.
* \param key the key of the value in the object
* \param type the yajl_type of the object you seek, or yajl_t_any if any will do.
*
* \returns a pointer to the found value, or NULL if we came up empty.
*
*/
YAJL_API yajl_val yajl_object_get(yajl_val object, const char * key, yajl_type type);

/* Various convenience macros to check the type of a `yajl_val` */
#define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == yajl_t_string))
#define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == yajl_t_number))
Expand Down
7 changes: 7 additions & 0 deletions src/yajl_tree.c
Expand Up @@ -452,6 +452,13 @@ yajl_val yajl_tree_parse (const char *input,
return (ctx.root);
}

yajl_val yajl_object_get(yajl_val n, const char * key, yajl_type type)
{
const char * path[2] = {key, 0};
yajl_val v = yajl_tree_get(n, path, type);
return v;
}

yajl_val yajl_tree_get(yajl_val n, const char ** path, yajl_type type)
{
if (!path) return NULL;
Expand Down