yajl_tree.h File Reference
`#include <yajl/yajl_common.h>`

Go to the source code of this file.

## Data Structures

struct | yajl_val_s

## Macros

#define | YAJL_NUMBER_INT_VALID 0x01
#define | YAJL_NUMBER_DOUBLE_VALID 0x02
#define | YAJL_IS_STRING(v)
#define | YAJL_IS_NUMBER(v)
#define | YAJL_IS_INTEGER(v)
#define | YAJL_IS_DOUBLE(v)
#define | YAJL_IS_OBJECT(v)
#define | YAJL_IS_ARRAY(v)
#define | YAJL_IS_TRUE(v)
#define | YAJL_IS_FALSE(v)
#define | YAJL_IS_NULL(v)
#define | YAJL_GET_STRING(v)
#define | YAJL_GET_NUMBER(v)
#define | YAJL_GET_DOUBLE(v)
#define | YAJL_GET_INTEGER(v)
#define | YAJL_GET_OBJECT(v)
#define | YAJL_GET_ARRAY(v)

## Typedefs

typedef struct yajl_val_s * | yajl_val

## Enumerations

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 }

## Functions

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.

Author
  Florian Forster

Date
  August 2010

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

/*
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "yajl/yajl_tree.h"
static unsigned char fileData[65536];
int
main(void)
{
size_t rd;
yajl_val node;
char errbuf[1024];
/* null plug buffers */
fileData[0] = errbuf[0] = 0;
/* read the entire config file */
rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
/* file read error handling */
if (rd == 0 && !feof(stdin)) {
fprintf(stderr, "error encountered on file read\n");
return 1;
} else if (rd >= sizeof(fileData) - 1) {
fprintf(stderr, "config file too big\n");
return 1;
}
/* we have the whole config file in memory. let's parse it ... */
node = yajl_tree_parse((const char *) fileData, errbuf, sizeof(errbuf));
/* parse error handling */
if (node == NULL) {
fprintf(stderr, "parse_error: ");
if (strlen(errbuf)) fprintf(stderr, " %s", errbuf);
else fprintf(stderr, "unknown error");
fprintf(stderr, "\n");
return 1;
}
/* ... and 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_t_string);
if (v) printf("%s/%s: %s\n", path[0], path[1], YAJL_GET_STRING(v));
else printf("no such node: %s/%s\n", path[0], path[1]);
}
yajl_tree_free(node);
return 0;
}
YAJL_GET_STRING
#define YAJL_GET_STRING(v)
Definition yajl_tree.h:162
yajl_tree_get
YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char **path, yajl_type type)
yajl_tree_parse
YAJL_API yajl_val yajl_tree_parse(const char *input, char *error_buffer, size_t error_buffer_size)
yajl_tree_free
YAJL_API void yajl_tree_free(yajl_val v)
yajl_t_string
@ yajl_t_string
Definition yajl_tree.h:42
yajl_val
struct yajl_val_s * yajl_val
Definition yajl_tree.h:59

## Macro Definition Documentation

## YAJL_GET_ARRAY

#define YAJL_GET_ARRAY | ( |  | v | ) |

Value:
(YAJL_IS_ARRAY(v) ? &(v)->u.array : NULL)
YAJL_IS_ARRAY
#define YAJL_IS_ARRAY(v)
Definition yajl_tree.h:155

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

## YAJL_GET_DOUBLE

#define YAJL_GET_DOUBLE | ( |  | v | ) |

Value:
((v)->u.number.d)

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

## YAJL_GET_INTEGER

#define YAJL_GET_INTEGER | ( |  | v | ) |

Value:
((v)->u.number.i)

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

## YAJL_GET_NUMBER

#define YAJL_GET_NUMBER | ( |  | v | ) |

Value:
((v)->u.number.r)

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

## YAJL_GET_OBJECT

#define YAJL_GET_OBJECT | ( |  | v | ) |

Value:
(YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL)
YAJL_IS_OBJECT
#define YAJL_IS_OBJECT(v)
Definition yajl_tree.h:154

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

## YAJL_GET_STRING

#define YAJL_GET_STRING | ( |  | v | ) |

Value:
(YAJL_IS_STRING(v) ? (v)->u.string : NULL)
YAJL_IS_STRING
#define YAJL_IS_STRING(v)
Definition yajl_tree.h:150

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

## YAJL_IS_ARRAY

#define YAJL_IS_ARRAY | ( |  | v | ) |

Value:
(((v) != NULL) && ((v)->type == yajl_t_array ))
yajl_t_array
@ yajl_t_array
Definition yajl_tree.h:45

## YAJL_IS_DOUBLE

#define YAJL_IS_DOUBLE | ( |  | v | ) |

Value:
(YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_DOUBLE_VALID))
YAJL_NUMBER_DOUBLE_VALID
#define YAJL_NUMBER_DOUBLE_VALID
Definition yajl_tree.h:56
YAJL_IS_NUMBER
#define YAJL_IS_NUMBER(v)
Definition yajl_tree.h:151

## YAJL_IS_FALSE

#define YAJL_IS_FALSE | ( |  | v | ) |

Value:
(((v) != NULL) && ((v)->type == yajl_t_false ))
yajl_t_false
@ yajl_t_false
Definition yajl_tree.h:47

## YAJL_IS_INTEGER

#define YAJL_IS_INTEGER | ( |  | v | ) |

Value:
(YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_INT_VALID))
YAJL_NUMBER_INT_VALID
#define YAJL_NUMBER_INT_VALID
Definition yajl_tree.h:55

## YAJL_IS_NULL

#define YAJL_IS_NULL | ( |  | v | ) |

Value:
(((v) != NULL) && ((v)->type == yajl_t_null ))
yajl_t_null
@ yajl_t_null
Definition yajl_tree.h:48

## YAJL_IS_NUMBER

#define YAJL_IS_NUMBER | ( |  | v | ) |

Value:
(((v) != NULL) && ((v)->type == yajl_t_number))
yajl_t_number
@ yajl_t_number
Definition yajl_tree.h:43

## YAJL_IS_OBJECT

#define YAJL_IS_OBJECT | ( |  | v | ) |

Value:
(((v) != NULL) && ((v)->type == yajl_t_object))
yajl_t_object
@ yajl_t_object
Definition yajl_tree.h:44

## YAJL_IS_STRING

#define YAJL_IS_STRING | ( |  | v | ) |

Value:
(((v) != NULL) && ((v)->type == yajl_t_string))

## YAJL_IS_TRUE

#define YAJL_IS_TRUE | ( |  | v | ) |

Value:
(((v) != NULL) && ((v)->type == yajl_t_true ))
yajl_t_true
@ yajl_t_true
Definition yajl_tree.h:46

## YAJL_NUMBER_DOUBLE_VALID

#define YAJL_NUMBER_DOUBLE_VALID 0x02

## YAJL_NUMBER_INT_VALID

#define YAJL_NUMBER_INT_VALID 0x01

## Typedef Documentation

## yajl_val

typedef struct yajl_val_s* yajl_val

A pointer to a node in the parse tree

## Enumeration Type Documentation

## yajl_type

enum yajl_type

possible data types that a yajl_val_s can hold

Enumerator
yajl_t_string |
yajl_t_number |
yajl_t_object |
yajl_t_array |
yajl_t_true |
yajl_t_false |
yajl_t_null |
yajl_t_any | The any type isn't valid for yajl_val_s.type, but can be used as an argument to routines like yajl_tree_get().

## Function Documentation

## yajl_tree_free()

YAJL_API void yajl_tree_free | ( | yajl_val | v | ) |

Free a parse tree returned by "yajl_tree_parse".

Parameters

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

References YAJL_API.

## 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.

Parameters

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

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.

References YAJL_API.

## 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).

Parameters

  input | Pointer to a null-terminated utf8 string containing JSON data.
  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.
  error_buffer_size | Size of the memory area pointed to by error_buffer_size. If error_buffer_size is `NULL`, this argument is ignored.

Returns
  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`.

References YAJL_API.