YAJL  2.0.4
yajl_tree.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010-2011  Florian Forster  <ff at octo.it>
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00031 #ifndef YAJL_TREE_H
00032 #define YAJL_TREE_H 1
00033 
00034 #include <yajl/yajl_common.h>
00035 
00036 #ifdef __cplusplus
00037 extern "C" {
00038 #endif
00039 
00041 typedef enum {
00042     yajl_t_string = 1,
00043     yajl_t_number = 2,
00044     yajl_t_object = 3,
00045     yajl_t_array = 4,
00046     yajl_t_true = 5,
00047     yajl_t_false = 6,
00048     yajl_t_null = 7,
00052     yajl_t_any = 8
00053 } yajl_type;
00054 
00055 #define YAJL_NUMBER_INT_VALID    0x01
00056 #define YAJL_NUMBER_DOUBLE_VALID 0x02
00057 
00059 typedef struct yajl_val_s * yajl_val;
00060 
00068 struct yajl_val_s
00069 {
00072     yajl_type type;
00075     union
00076     {
00077         char * string;
00078         struct {
00079             long long i; /*< integer value, if representable. */
00080             double  d;   /*< double value, if representable. */
00084             char   *r;   /*< unparsed number in string form. */
00085             unsigned int flags;
00086         } number;
00087         struct {
00088             const char **keys; /*< Array of keys */
00089             yajl_val *values; /*< Array of values. */
00090             size_t len; /*< Number of key-value-pairs. */
00091         } object;
00092         struct {
00093             yajl_val *values; /*< Array of elements. */
00094             size_t len; /*< Number of elements. */
00095         } array;
00096     } u;
00097 };
00098 
00121 YAJL_API yajl_val yajl_tree_parse (const char *input,
00122                                    char *error_buffer, size_t error_buffer_size);
00123 
00130 YAJL_API void yajl_tree_free (yajl_val v);
00131 
00146 YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type type);
00147 
00148 /* Various convenience macros to check the type of a `yajl_val` */
00149 #define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == yajl_t_string))
00150 #define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == yajl_t_number))
00151 #define YAJL_IS_INTEGER(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_INT_VALID))
00152 #define YAJL_IS_DOUBLE(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_DOUBLE_VALID))
00153 #define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == yajl_t_object))
00154 #define YAJL_IS_ARRAY(v)  (((v) != NULL) && ((v)->type == yajl_t_array ))
00155 #define YAJL_IS_TRUE(v)   (((v) != NULL) && ((v)->type == yajl_t_true  ))
00156 #define YAJL_IS_FALSE(v)  (((v) != NULL) && ((v)->type == yajl_t_false ))
00157 #define YAJL_IS_NULL(v)   (((v) != NULL) && ((v)->type == yajl_t_null  ))
00158 
00161 #define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->u.string : NULL)
00162 
00165 #define YAJL_GET_NUMBER(v) ((v)->u.number.r)
00166 
00169 #define YAJL_GET_DOUBLE(v) ((v)->u.number.d)
00170 
00173 #define YAJL_GET_INTEGER(v) ((v)->u.number.i)
00174 
00176 #define YAJL_GET_OBJECT(v) (YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL)
00177 
00179 #define YAJL_GET_ARRAY(v)  (YAJL_IS_ARRAY(v)  ? &(v)->u.array  : NULL)
00180 
00181 #ifdef __cplusplus
00182 }
00183 #endif
00184 
00185 #endif /* YAJL_TREE_H */