YAJL 2.0.0
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 
00037 typedef enum {
00038     yajl_t_string = 1,
00039     yajl_t_number = 2,
00040     yajl_t_object = 3,
00041     yajl_t_array = 4,
00042     yajl_t_true = 5,
00043     yajl_t_false = 6,
00044     yajl_t_null = 7,
00048     yajl_t_any = 8
00049 } yajl_type;
00050 
00051 #define YAJL_NUMBER_INT_VALID    0x01
00052 #define YAJL_NUMBER_DOUBLE_VALID 0x02
00053 
00055 typedef struct yajl_val_s * yajl_val;
00056 
00064 struct yajl_val_s
00065 {
00068     yajl_type type;
00071     union
00072     {
00073         char * string;
00074         struct {
00075             long long i; /*< integer value, if representable. */
00076             double  d;   /*< double value, if representable. */
00080             char   *r;   /*< unparsed number in string form. */
00081             unsigned int flags;
00082         } number;
00083         struct {
00084             const char **keys; /*< Array of keys */
00085             yajl_val *values; /*< Array of values. */
00086             size_t len; /*< Number of key-value-pairs. */
00087         } object;
00088         struct {
00089             yajl_val *values; /*< Array of elements. */
00090             size_t len; /*< Number of elements. */
00091         } array;
00092     } u;
00093 };
00094 
00117 YAJL_API yajl_val yajl_tree_parse (const char *input,
00118                                    char *error_buffer, size_t error_buffer_size);
00119 
00126 YAJL_API void yajl_tree_free (yajl_val v);
00127 
00142 YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type type);
00143 
00144 /* Various convenience macros to check the type of a `yajl_val` */
00145 #define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == yajl_t_string))
00146 #define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == yajl_t_number))
00147 #define YAJL_IS_INTEGER(v) (YAJL_IS_NUMBER(v) && ((v)->u.flags & YAJL_NUMBER_INT_VALID))
00148 #define YAJL_IS_DOUBLE(v) (YAJL_IS_NUMBER(v) && ((v)->u.flags & YAJL_NUMBER_DOUBLE_VALID))
00149 #define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == yajl_t_object))
00150 #define YAJL_IS_ARRAY(v)  (((v) != NULL) && ((v)->type == yajl_t_array ))
00151 #define YAJL_IS_TRUE(v)   (((v) != NULL) && ((v)->type == yajl_t_true  ))
00152 #define YAJL_IS_FALSE(v)  (((v) != NULL) && ((v)->type == yajl_t_false ))
00153 #define YAJL_IS_NULL(v)   (((v) != NULL) && ((v)->type == yajl_t_null  ))
00154 
00157 #define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->u.string : NULL)
00158 
00161 #define YAJL_GET_NUMBER(v) ((v)->u.number.r)
00162 
00165 #define YAJL_GET_DOUBLE(v) ((v)->u.number.d)
00166 
00169 #define YAJL_GET_INTEGER(v) ((v)->u.number.i)
00170 
00172 #define YAJL_GET_OBJECT(v) (YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL)
00173 
00175 #define YAJL_GET_ARRAY(v)  (YAJL_IS_ARRAY(v)  ? &(v)->u.array  : NULL)
00176 
00177 #endif /* YAJL_TREE_H */