File src/yajl_tree.c

Parses JSON data and returns the data in tree form.
Writtan by Florian Forster
August 2010
This interface makes quick parsing and extraction of smallish JSON docs trivial, as shown in the following example:
example/parse_config.c


Included Files


Typedef stack_elem_t

typedef struct stack_elem_s stack_elem_t
See: Type struct stack_elem_s

Type struct stack_elem_s

struct stack_elem_s
struct stack_elem_s  
   {  
      char* key;  
      yajl_val value;  
      stack_elem_t* next;  
   }  

Type struct context_s

struct context_s
struct context_s  
   {  
      stack_elem_t* stack;  
      yajl_val root;  
      char* errbuf;  
      size_t errbuf_size;  
   }  

Typedef context_t

typedef struct context_s context_t
See: Type struct context_s

Global Function yajl_tree_free()

Free a parse tree returned by yajl_tree_parse().

void yajl_tree_free ( yajl_val v )

yajl_val v
Pointer to a JSON value returned by "yajl_tree_parse". Passing NULL is valid and results in a no-op.
Prototyped in: src/yajl/yajl_tree.h
Calls: yajl_array_free() src/yajl_tree.c
  yajl_object_free() src/yajl_tree.c
  free()
Called by: main() example/parse_config.c
  yajl_array_free() src/yajl_tree.c
  yajl_object_free() src/yajl_tree.c
  yajl_tree_parse() src/yajl_tree.c

Global Function yajl_tree_get()

Access a nested value inside a tree.

yajl_val yajl_tree_get ( yajl_val n, const char** path, yajl_type type )

yajl_val n
the node under which you'd like to extract values.
const char** path
A null terminated array of strings, each the name of an object key
yajl_type 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.

Prototyped in: src/yajl/yajl_tree.h
Calls: strcmp()
Called by: main() example/parse_config.c

Global Function yajl_tree_parse()

Parse a string.

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

const char* input
Pointer to a null-terminated utf8 string containing JSON data.
char* error_buffer
Pointer 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.
size_t error_buffer_size
Size of the memory area pointed to by error_buffer_size. If error_buffer_size is NULL, this argument is ignored.
Parses a null-terminated string containing JSON data.
Returns a pointer to a yajl_val object which is the top-level value (root of the parse tree) 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.

Prototyped in: src/yajl/yajl_tree.h
Calls: context_pop() src/yajl_tree.c
  yajl_alloc() src/yajl.c
  yajl_complete_parse() src/yajl.c
  yajl_config() src/yajl.c
  yajl_free() src/yajl.c
  yajl_get_error() src/yajl.c
  yajl_parse() src/yajl.c
  yajl_tree_free() src/yajl_tree.c
  memset(), snprintf(), strlen()
Called by: main() example/parse_config.c
References Functions: handle_boolean() src/yajl_tree.c
  handle_end_array() src/yajl_tree.c
  handle_end_map() src/yajl_tree.c
  handle_null() src/yajl_tree.c
  handle_number() src/yajl_tree.c
  handle_start_array() src/yajl_tree.c
  handle_start_map() src/yajl_tree.c
  handle_string() src/yajl_tree.c

Local Function array_add_value()

static int array_add_value ( context_t* ctx, yajl_val array, yajl_val value )
Calls: __assert13(), realloc(), snprintf()
Called by: context_add_value() src/yajl_tree.c

Local Function context_add_value()

static int context_add_value ( context_t* ctx, yajl_val v )
Calls: array_add_value() src/yajl_tree.c
  object_add_keyval() src/yajl_tree.c
  __assert13(), free(), snprintf()
Called by: handle_boolean() src/yajl_tree.c
  handle_end_array() src/yajl_tree.c
  handle_end_map() src/yajl_tree.c
  handle_null() src/yajl_tree.c
  handle_number() src/yajl_tree.c
  handle_string() src/yajl_tree.c

Local Function context_pop()

static yajl_val context_pop ( context_t* ctx )
Calls: free(), snprintf()
Called by: handle_end_array() src/yajl_tree.c
  handle_end_map() src/yajl_tree.c
  yajl_tree_parse() src/yajl_tree.c

Local Function context_push()

static int context_push ( context_t* ctx, yajl_val v )
Calls: __assert13(), malloc(), memset(), snprintf()
Called by: handle_start_array() src/yajl_tree.c
  handle_start_map() src/yajl_tree.c

Local Function handle_boolean()

static int handle_boolean ( void* ctx, int boolean_value )
Calls: context_add_value() src/yajl_tree.c
  value_alloc() src/yajl_tree.c
  snprintf()
Used in: yajl_tree_parse() src/yajl_tree.c

Local Function handle_end_array()

static int handle_end_array ( void* ctx )
Calls: context_add_value() src/yajl_tree.c
  context_pop() src/yajl_tree.c
Used in: yajl_tree_parse() src/yajl_tree.c

Local Function handle_end_map()

static int handle_end_map ( void* ctx )
Calls: context_add_value() src/yajl_tree.c
  context_pop() src/yajl_tree.c
Used in: yajl_tree_parse() src/yajl_tree.c

Local Function handle_null()

static int handle_null ( void* ctx )
Calls: context_add_value() src/yajl_tree.c
  value_alloc() src/yajl_tree.c
  snprintf()
Used in: yajl_tree_parse() src/yajl_tree.c

Local Function handle_number()

static int handle_number ( void* ctx, const char* string, size_t string_length )
Calls: context_add_value() src/yajl_tree.c
  value_alloc() src/yajl_tree.c
  yajl_parse_integer() src/yajl_parser.c
  __errno(), free(), malloc(), memcpy(), snprintf(), strlen(), strtod()
Used in: yajl_tree_parse() src/yajl_tree.c

Local Function handle_start_array()

static int handle_start_array ( void* ctx )
Calls: context_push() src/yajl_tree.c
  value_alloc() src/yajl_tree.c
  snprintf()
Used in: yajl_tree_parse() src/yajl_tree.c

Local Function handle_start_map()

static int handle_start_map ( void* ctx )
Calls: context_push() src/yajl_tree.c
  value_alloc() src/yajl_tree.c
  snprintf()
Used in: yajl_tree_parse() src/yajl_tree.c

Local Function handle_string()

static int handle_string ( void* ctx, const unsigned char* string, size_t string_length )
Calls: context_add_value() src/yajl_tree.c
  value_alloc() src/yajl_tree.c
  free(), malloc(), memcpy(), snprintf()
Used in: yajl_tree_parse() src/yajl_tree.c

Local Function object_add_keyval()

static int object_add_keyval ( context_t* ctx, yajl_val obj, char* key, yajl_val value )
Calls: __assert13(), realloc(), snprintf()
Called by: context_add_value() src/yajl_tree.c

Local Function value_alloc()

static yajl_val value_alloc ( yajl_type type )
Calls: malloc(), memset()
Called by: handle_boolean() src/yajl_tree.c
  handle_null() src/yajl_tree.c
  handle_number() src/yajl_tree.c
  handle_start_array() src/yajl_tree.c
  handle_start_map() src/yajl_tree.c
  handle_string() src/yajl_tree.c

Local Function yajl_array_free()

static void yajl_array_free ( yajl_val v )
Calls: yajl_tree_free() src/yajl_tree.c
  free()
Called by: yajl_tree_free() src/yajl_tree.c

Local Function yajl_object_free()

static void yajl_object_free ( yajl_val v )
Calls: yajl_tree_free() src/yajl_tree.c
  free()
Called by: yajl_tree_free() src/yajl_tree.c