| 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 | |
| 17 | /** |
| 18 | * \file yajl_tree.h |
| 19 | * |
| 20 | * Parses JSON data and returns the data in tree form. |
| 21 | * |
| 22 | * \author Florian Forster |
| 23 | * \date August 2010 |
| 24 | * |
| 25 | * This interface makes quick parsing and extraction of |
| 26 | * smallish JSON docs trivial: |
| 27 | * |
| 28 | * \include example/parse_config.c |
| 29 | */ |
| 30 | |
| 31 | #ifndef YAJL_TREE_H |
| 32 | #define YAJL_TREE_H 1 |
| 33 | |
| 34 | #include "yajl_common.h" |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | /** possible data types that a yajl_val_s can hold */ |
| 41 | typedef enum { |
| 42 | yajl_t_string = 1, |
| 43 | yajl_t_number = 2, |
| 44 | yajl_t_object = 3, |
| 45 | yajl_t_array = 4, |
| 46 | yajl_t_true = 5, |
| 47 | yajl_t_false = 6, |
| 48 | yajl_t_null = 7, |
| 49 | /** The any type isn't valid for yajl_val_s.type, but can be |
| 50 | * used as an argument to routines like yajl_tree_get(). |
| 51 | */ |
| 52 | yajl_t_any = 8 |
| 53 | } yajl_type; |
| 54 | |
| 55 | #define YAJL_NUMBER_INT_VALID 0x01 |
| 56 | #define YAJL_NUMBER_DOUBLE_VALID 0x02 |
| 57 | |
| 58 | /** A pointer to a node in the parse tree */ |
| 59 | typedef struct yajl_val_s * yajl_val; |
| 60 | |
| 61 | /** Inner JSON types */ |
| 62 | typedef struct |
| 63 | { |
| 64 | char **keys; /*< Array of keys */ |
| 65 | yajl_val *values; /*< Array of values. */ |
| 66 | size_t len; /*< Number of key-value-pairs. */ |
| 67 | } yajl_object_s, *yajl_object; |
| 68 | typedef struct |
| 69 | { |
| 70 | yajl_val *values; /*< Array of elements. */ |
| 71 | size_t len; /*< Number of elements. */ |
| 72 | } yajl_array_s, *yajl_array; |
| 73 | |
| 74 | /** |
| 75 | * A JSON value representation capable of holding one of the seven |
| 76 | * types above. For "string", "number", "object", and "array" |
| 77 | * additional data is available in the union. The "YAJL_IS_*" |
| 78 | * and "YAJL_GET_*" macros below allow type checking and convenient |
| 79 | * value extraction. |
| 80 | */ |
| 81 | struct yajl_val_s |
| 82 | { |
| 83 | /** Type of the value contained. Use the "YAJL_IS_*" macros to check for a |
| 84 | * specific type. */ |
| 85 | yajl_type type; |
| 86 | /** Type-specific data. You may use the "YAJL_GET_*" macros to access these |
| 87 | * members. */ |
| 88 | union |
| 89 | { |
| 90 | char * string; |
| 91 | struct { |
| 92 | long long i; /*< integer value, if representable. */ |
| 93 | double d; /*< double value, if representable. */ |
| 94 | char *r; /*< unparsed number in string form. */ |
| 95 | /** Signals whether the \em i and \em d members are |
| 96 | * valid. See \c YAJL_NUMBER_INT_VALID and |
| 97 | * \c YAJL_NUMBER_DOUBLE_VALID. */ |
| 98 | unsigned int flags; |
| 99 | } number; |
| 100 | yajl_object_s object; |
| 101 | yajl_array_s array; |
| 102 | } u; |
| 103 | }; |
| 104 | |
| 105 | /** |
| 106 | * Parse a string. |
| 107 | * |
| 108 | * Parses an null-terminated string containing JSON data and returns a pointer |
| 109 | * to the top-level value (root of the parse tree). |
| 110 | * |
| 111 | * \param input Pointer to a null-terminated utf8 string containing |
| 112 | * JSON data. |
| 113 | * \param error_buffer Pointer to a buffer in which an error message will |
| 114 | * be stored if \em yajl_tree_parse fails, or |
| 115 | * \c NULL. The buffer will be initialized before |
| 116 | * parsing, so its content will be destroyed even if |
| 117 | * \em yajl_tree_parse succeeds. |
| 118 | * \param error_buffer_size Size of the memory area pointed to by |
| 119 | * \em error_buffer_size. If \em error_buffer_size is |
| 120 | * \c NULL, this argument is ignored. |
| 121 | * |
| 122 | * \returns Pointer to the top-level value or \c NULL on error. The memory |
| 123 | * pointed to must be freed using \em yajl_tree_free. In case of an error, a |
| 124 | * null terminated message describing the error in more detail is stored in |
| 125 | * \em error_buffer if it is not \c NULL. |
| 126 | */ |
| 127 | YAJL_API yajl_val yajl_tree_parse (const char *input, |
| 128 | char *error_buffer, size_t error_buffer_size); |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Free a parse tree returned by "yajl_tree_parse". |
| 133 | * |
| 134 | * \param v Pointer to a JSON value returned by "yajl_tree_parse". Passing NULL |
| 135 | * is valid and results in a no-op. |
| 136 | */ |
| 137 | YAJL_API void yajl_tree_free (yajl_val v); |
| 138 | |
| 139 | /** |
| 140 | * Access a nested value inside a tree. |
| 141 | * |
| 142 | * \param parent the node under which you'd like to extract values. |
| 143 | * \param path A null terminated array of strings, each the name of an object key |
| 144 | * \param type the yajl_type of the object you seek, or yajl_t_any if any will do. |
| 145 | * |
| 146 | * \returns a pointer to the found value, or NULL if we came up empty. |
| 147 | * |
| 148 | * Future Ideas: it'd be nice to move path to a string and implement support for |
| 149 | * a teeny tiny micro language here, so you can extract array elements, do things |
| 150 | * like .first and .last, even .length. Inspiration from JSONPath and css selectors? |
| 151 | * No it wouldn't be fast, but that's not what this API is about. |
| 152 | */ |
| 153 | YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type type); |
| 154 | |
| 155 | /* Various convenience macros to check the type of a `yajl_val` */ |
| 156 | #define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == yajl_t_string)) |
| 157 | #define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == yajl_t_number)) |
| 158 | #define YAJL_IS_INTEGER(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_INT_VALID)) |
| 159 | #define YAJL_IS_DOUBLE(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_DOUBLE_VALID)) |
| 160 | #define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == yajl_t_object)) |
| 161 | #define YAJL_IS_ARRAY(v) (((v) != NULL) && ((v)->type == yajl_t_array )) |
| 162 | #define YAJL_IS_TRUE(v) (((v) != NULL) && ((v)->type == yajl_t_true )) |
| 163 | #define YAJL_IS_FALSE(v) (((v) != NULL) && ((v)->type == yajl_t_false )) |
| 164 | #define YAJL_IS_NULL(v) (((v) != NULL) && ((v)->type == yajl_t_null )) |
| 165 | |
| 166 | /** Given a yajl_val_string return a ptr to the bare string it contains, |
| 167 | * or NULL if the value is not a string. */ |
| 168 | #define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->u.string : NULL) |
| 169 | |
| 170 | /** Get the string representation of a number. You should check type first, |
| 171 | * perhaps using YAJL_IS_NUMBER */ |
| 172 | #define YAJL_GET_NUMBER(v) ((v)->u.number.r) |
| 173 | |
| 174 | /** Get the double representation of a number. You should check type first, |
| 175 | * perhaps using YAJL_IS_DOUBLE */ |
| 176 | #define YAJL_GET_DOUBLE(v) ((v)->u.number.d) |
| 177 | |
| 178 | /** Get the 64bit (long long) integer representation of a number. You should |
| 179 | * check type first, perhaps using YAJL_IS_INTEGER */ |
| 180 | #define YAJL_GET_INTEGER(v) ((v)->u.number.i) |
| 181 | |
| 182 | /** Get a pointer to a yajl_val_object or NULL if the value is not an object. */ |
| 183 | #define YAJL_GET_OBJECT(v) (YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL) |
| 184 | |
| 185 | /** Get a pointer to a yajl_val_array or NULL if the value is not an object. */ |
| 186 | #define YAJL_GET_ARRAY(v) (YAJL_IS_ARRAY(v) ? &(v)->u.array : NULL) |
| 187 | |
| 188 | #ifdef __cplusplus |
| 189 | } |
| 190 | #endif |
| 191 | |
| 192 | #endif /* YAJL_TREE_H */ |