YAJL  2.0.4
yajl_tree.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2011 Florian Forster <ff at octo.it>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
31 #ifndef YAJL_TREE_H
32 #define YAJL_TREE_H 1
33 
34 #include <yajl/yajl_common.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
41 typedef enum {
53 } yajl_type;
54 
55 #define YAJL_NUMBER_INT_VALID 0x01
56 #define YAJL_NUMBER_DOUBLE_VALID 0x02
57 
59 typedef struct yajl_val_s * yajl_val;
60 
68 struct yajl_val_s
69 {
75  union
76  {
77  char * string;
78  struct {
79  long long i; /*< integer value, if representable. */
80  double d; /*< double value, if representable. */
84  char *r; /*< unparsed number in string form. */
85  unsigned int flags;
86  } number;
87  struct {
88  const char **keys; /*< Array of keys */
89  yajl_val *values; /*< Array of values. */
90  size_t len; /*< Number of key-value-pairs. */
91  } object;
92  struct {
93  yajl_val *values; /*< Array of elements. */
94  size_t len; /*< Number of elements. */
95  } array;
96  } u;
97 };
98 
121 YAJL_API yajl_val yajl_tree_parse (const char *input,
122  char *error_buffer, size_t error_buffer_size);
123 
130 YAJL_API void yajl_tree_free (yajl_val v);
131 
146 YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type type);
147 
148 /* Various convenience macros to check the type of a `yajl_val` */
149 #define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == yajl_t_string))
150 #define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == yajl_t_number))
151 #define YAJL_IS_INTEGER(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_INT_VALID))
152 #define YAJL_IS_DOUBLE(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_DOUBLE_VALID))
153 #define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == yajl_t_object))
154 #define YAJL_IS_ARRAY(v) (((v) != NULL) && ((v)->type == yajl_t_array ))
155 #define YAJL_IS_TRUE(v) (((v) != NULL) && ((v)->type == yajl_t_true ))
156 #define YAJL_IS_FALSE(v) (((v) != NULL) && ((v)->type == yajl_t_false ))
157 #define YAJL_IS_NULL(v) (((v) != NULL) && ((v)->type == yajl_t_null ))
158 
161 #define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->u.string : NULL)
162 
165 #define YAJL_GET_NUMBER(v) ((v)->u.number.r)
166 
169 #define YAJL_GET_DOUBLE(v) ((v)->u.number.d)
170 
173 #define YAJL_GET_INTEGER(v) ((v)->u.number.i)
174 
176 #define YAJL_GET_OBJECT(v) (YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL)
177 
179 #define YAJL_GET_ARRAY(v) (YAJL_IS_ARRAY(v) ? &(v)->u.array : NULL)
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif /* YAJL_TREE_H */