yajl  2.1.0
About: YAJL is a small fast streaming JSON parsing library (written in C).
  Fossies Dox: yajl-2.1.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

yajl_tree.h File Reference
#include <yajl/yajl_common.h>
Include dependency graph for yajl_tree.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.


struct  yajl_val_s


#define YAJL_NUMBER_INT_VALID   0x01
#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)->u.number.flags & YAJL_NUMBER_INT_VALID))
#define YAJL_IS_DOUBLE(v)   (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_DOUBLE_VALID))
#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 ))
#define YAJL_GET_STRING(v)   (YAJL_IS_STRING(v) ? (v)->u.string : NULL)
#define YAJL_GET_NUMBER(v)   ((v)->u.number.r)
#define YAJL_GET_DOUBLE(v)   ((v)->u.number.d)
#define YAJL_GET_INTEGER(v)   ((v)->u.number.i)
#define YAJL_GET_OBJECT(v)   (YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL)
#define YAJL_GET_ARRAY(v)   (YAJL_IS_ARRAY(v) ? &(v)->u.array : NULL)


typedef struct yajl_val_syajl_val


enum  yajl_type {
  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_API yajl_val yajl_tree_parse (const char *input, char *error_buffer, size_t error_buffer_size)
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)

Detailed Description

Parses JSON data and returns the data in tree form.

Florian Forster
August 2010

This interface makes quick parsing and extraction of smallish JSON docs trivial:

Definition in file yajl_tree.h.

Macro Definition Documentation


#define YAJL_GET_ARRAY (   v)    (YAJL_IS_ARRAY(v) ? &(v)->u.array : NULL)

Get a pointer to a yajl_val_array or NULL if the value is not an object.

Definition at line 180 of file yajl_tree.h.


#define YAJL_GET_DOUBLE (   v)    ((v)->u.number.d)

Get the double representation of a number. You should check type first, perhaps using YAJL_IS_DOUBLE

Definition at line 170 of file yajl_tree.h.


#define YAJL_GET_INTEGER (   v)    ((v)->u.number.i)

Get the 64bit (long long) integer representation of a number. You should check type first, perhaps using YAJL_IS_INTEGER

Definition at line 174 of file yajl_tree.h.


#define YAJL_GET_NUMBER (   v)    ((v)->u.number.r)

Get the string representation of a number. You should check type first, perhaps using YAJL_IS_NUMBER

Definition at line 166 of file yajl_tree.h.


#define YAJL_GET_OBJECT (   v)    (YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL)

Get a pointer to a yajl_val_object or NULL if the value is not an object.

Definition at line 177 of file yajl_tree.h.

Referenced by yajl_tree_free().


#define YAJL_GET_STRING (   v)    (YAJL_IS_STRING(v) ? (v)->u.string : NULL)

Given a yajl_val_string return a ptr to the bare string it contains, or NULL if the value is not a string.

Definition at line 162 of file yajl_tree.h.


#define YAJL_IS_ARRAY (   v)    (((v) != NULL) && ((v)->type == yajl_t_array ))

Definition at line 155 of file yajl_tree.h.


#define YAJL_IS_DOUBLE (   v)    (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_DOUBLE_VALID))

Definition at line 153 of file yajl_tree.h.


#define YAJL_IS_FALSE (   v)    (((v) != NULL) && ((v)->type == yajl_t_false ))

Definition at line 157 of file yajl_tree.h.


#define YAJL_IS_INTEGER (   v)    (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_INT_VALID))

Definition at line 152 of file yajl_tree.h.


#define YAJL_IS_NULL (   v)    (((v) != NULL) && ((v)->type == yajl_t_null ))

Definition at line 158 of file yajl_tree.h.


#define YAJL_IS_NUMBER (   v)    (((v) != NULL) && ((v)->type == yajl_t_number))

Definition at line 151 of file yajl_tree.h.

Referenced by yajl_tree_free().


#define YAJL_IS_OBJECT (   v)    (((v) != NULL) && ((v)->type == yajl_t_object))

Definition at line 154 of file yajl_tree.h.


#define YAJL_IS_STRING (   v)    (((v) != NULL) && ((v)->type == yajl_t_string))

Definition at line 150 of file yajl_tree.h.

Referenced by yajl_tree_free().


#define YAJL_IS_TRUE (   v)    (((v) != NULL) && ((v)->type == yajl_t_true ))

Definition at line 156 of file yajl_tree.h.



Definition at line 56 of file yajl_tree.h.


#define YAJL_NUMBER_INT_VALID   0x01

Definition at line 55 of file yajl_tree.h.

Typedef Documentation

◆ yajl_val

typedef struct yajl_val_s* yajl_val

A pointer to a node in the parse tree

Definition at line 59 of file yajl_tree.h.

Enumeration Type Documentation

◆ yajl_type

enum yajl_type

possible data types that a yajl_val_s can hold


The any type isn't valid for yajl_val_s.type, but can be used as an argument to routines like yajl_tree_get().

Definition at line 41 of file yajl_tree.h.

Function Documentation

◆ yajl_tree_free()

YAJL_API void yajl_tree_free ( yajl_val  v)

Free a parse tree returned by "yajl_tree_parse".

vPointer to a JSON value returned by "yajl_tree_parse". Passing NULL is valid and results in a no-op.

Definition at line 477 of file yajl_tree.c.

References yajl_val_s::number, yajl_val_s::string, yajl_val_s::u, YAJL_GET_OBJECT, YAJL_IS_NUMBER, and YAJL_IS_STRING.

◆ yajl_tree_get()

YAJL_API yajl_val yajl_tree_get ( yajl_val  parent,
const char **  path,
yajl_type  type 

Access a nested value inside a tree.

parentthe node under which you'd like to extract values.
pathA null terminated array of strings, each the name of an object key
typethe yajl_type of the object you seek, or yajl_t_any if any will do.
a pointer to the found value, or NULL if we came up empty.

Future Ideas: it'd be nice to move path to a string and implement support for a teeny tiny micro language here, so you can extract array elements, do things like .first and .last, even .length. Inspiration from JSONPath and css selectors? No it wouldn't be fast, but that's not what this API is about.

Definition at line 455 of file yajl_tree.c.

References yajl_val_s::object, yajl_val_s::type, yajl_val_s::u, yajl_t_any, and yajl_t_object.

◆ yajl_tree_parse()

YAJL_API yajl_val yajl_tree_parse ( const char *  input,
char *  error_buffer,
size_t  error_buffer_size 

Parse a string.

Parses an null-terminated string containing JSON data and returns a pointer to the top-level value (root of the parse tree).

inputPointer to a null-terminated utf8 string containing JSON data.
error_bufferPointer to a buffer in which an error message will be stored if yajl_tree_parse fails, or NULL. The buffer will be initialized before parsing, so its content will be destroyed even if yajl_tree_parse succeeds.
error_buffer_sizeSize of the memory area pointed to by error_buffer_size. If error_buffer_size is NULL, this argument is ignored.
Pointer to the top-level value or NULL on error. The memory pointed to must be freed using yajl_tree_free. In case of an error, a null terminated message describing the error in more detail is stored in error_buffer if it is not NULL.

Definition at line 403 of file yajl_tree.c.