yajl - JSON library working with the compiler. URL: http://lloyd.github.com/yajl/

Dependencies:   mbed

Committer:
rolf
Date:
Wed Nov 18 17:56:51 2009 +0000
Revision:
0:34f4a53d4ca3

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rolf 0:34f4a53d4ca3 1 /*
rolf 0:34f4a53d4ca3 2 * Copyright 2007-2009, Lloyd Hilaiel.
rolf 0:34f4a53d4ca3 3 *
rolf 0:34f4a53d4ca3 4 * Redistribution and use in source and binary forms, with or without
rolf 0:34f4a53d4ca3 5 * modification, are permitted provided that the following conditions are
rolf 0:34f4a53d4ca3 6 * met:
rolf 0:34f4a53d4ca3 7 *
rolf 0:34f4a53d4ca3 8 * 1. Redistributions of source code must retain the above copyright
rolf 0:34f4a53d4ca3 9 * notice, this list of conditions and the following disclaimer.
rolf 0:34f4a53d4ca3 10 *
rolf 0:34f4a53d4ca3 11 * 2. Redistributions in binary form must reproduce the above copyright
rolf 0:34f4a53d4ca3 12 * notice, this list of conditions and the following disclaimer in
rolf 0:34f4a53d4ca3 13 * the documentation and/or other materials provided with the
rolf 0:34f4a53d4ca3 14 * distribution.
rolf 0:34f4a53d4ca3 15 *
rolf 0:34f4a53d4ca3 16 * 3. Neither the name of Lloyd Hilaiel nor the names of its
rolf 0:34f4a53d4ca3 17 * contributors may be used to endorse or promote products derived
rolf 0:34f4a53d4ca3 18 * from this software without specific prior written permission.
rolf 0:34f4a53d4ca3 19 *
rolf 0:34f4a53d4ca3 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
rolf 0:34f4a53d4ca3 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
rolf 0:34f4a53d4ca3 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
rolf 0:34f4a53d4ca3 23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
rolf 0:34f4a53d4ca3 24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
rolf 0:34f4a53d4ca3 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
rolf 0:34f4a53d4ca3 26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
rolf 0:34f4a53d4ca3 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
rolf 0:34f4a53d4ca3 28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
rolf 0:34f4a53d4ca3 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
rolf 0:34f4a53d4ca3 30 * POSSIBILITY OF SUCH DAMAGE.
rolf 0:34f4a53d4ca3 31 */
rolf 0:34f4a53d4ca3 32
rolf 0:34f4a53d4ca3 33 /**
rolf 0:34f4a53d4ca3 34 * \file yajl_parse.h
rolf 0:34f4a53d4ca3 35 * Interface to YAJL's JSON parsing facilities.
rolf 0:34f4a53d4ca3 36 */
rolf 0:34f4a53d4ca3 37
rolf 0:34f4a53d4ca3 38 #include <yajl/yajl_common.h>
rolf 0:34f4a53d4ca3 39
rolf 0:34f4a53d4ca3 40 #ifndef __YAJL_PARSE_H__
rolf 0:34f4a53d4ca3 41 #define __YAJL_PARSE_H__
rolf 0:34f4a53d4ca3 42
rolf 0:34f4a53d4ca3 43 #ifdef __cplusplus
rolf 0:34f4a53d4ca3 44 extern "C" {
rolf 0:34f4a53d4ca3 45 #endif
rolf 0:34f4a53d4ca3 46 /** error codes returned from this interface */
rolf 0:34f4a53d4ca3 47 typedef enum {
rolf 0:34f4a53d4ca3 48 /** no error was encountered */
rolf 0:34f4a53d4ca3 49 yajl_status_ok,
rolf 0:34f4a53d4ca3 50 /** a client callback returned zero, stopping the parse */
rolf 0:34f4a53d4ca3 51 yajl_status_client_canceled,
rolf 0:34f4a53d4ca3 52 /** The parse cannot yet complete because more json input text
rolf 0:34f4a53d4ca3 53 * is required, call yajl_parse with the next buffer of input text.
rolf 0:34f4a53d4ca3 54 * (pertinent only when stream parsing) */
rolf 0:34f4a53d4ca3 55 yajl_status_insufficient_data,
rolf 0:34f4a53d4ca3 56 /** An error occured during the parse. Call yajl_get_error for
rolf 0:34f4a53d4ca3 57 * more information about the encountered error */
rolf 0:34f4a53d4ca3 58 yajl_status_error
rolf 0:34f4a53d4ca3 59 } yajl_status;
rolf 0:34f4a53d4ca3 60
rolf 0:34f4a53d4ca3 61 /** attain a human readable, english, string for an error */
rolf 0:34f4a53d4ca3 62 const char * YAJL_API yajl_status_to_string(yajl_status code);
rolf 0:34f4a53d4ca3 63
rolf 0:34f4a53d4ca3 64 /** an opaque handle to a parser */
rolf 0:34f4a53d4ca3 65 typedef struct yajl_handle_t * yajl_handle;
rolf 0:34f4a53d4ca3 66
rolf 0:34f4a53d4ca3 67 /** yajl is an event driven parser. this means as json elements are
rolf 0:34f4a53d4ca3 68 * parsed, you are called back to do something with the data. The
rolf 0:34f4a53d4ca3 69 * functions in this table indicate the various events for which
rolf 0:34f4a53d4ca3 70 * you will be called back. Each callback accepts a "context"
rolf 0:34f4a53d4ca3 71 * pointer, this is a void * that is passed into the yajl_parse
rolf 0:34f4a53d4ca3 72 * function which the client code may use to pass around context.
rolf 0:34f4a53d4ca3 73 *
rolf 0:34f4a53d4ca3 74 * All callbacks return an integer. If non-zero, the parse will
rolf 0:34f4a53d4ca3 75 * continue. If zero, the parse will be canceled and
rolf 0:34f4a53d4ca3 76 * yajl_status_client_canceled will be returned from the parse.
rolf 0:34f4a53d4ca3 77 *
rolf 0:34f4a53d4ca3 78 * Note about handling of numbers:
rolf 0:34f4a53d4ca3 79 * yajl will only convert numbers that can be represented in a double
rolf 0:34f4a53d4ca3 80 * or a long int. All other numbers will be passed to the client
rolf 0:34f4a53d4ca3 81 * in string form using the yajl_number callback. Furthermore, if
rolf 0:34f4a53d4ca3 82 * yajl_number is not NULL, it will always be used to return numbers,
rolf 0:34f4a53d4ca3 83 * that is yajl_integer and yajl_double will be ignored. If
rolf 0:34f4a53d4ca3 84 * yajl_number is NULL but one of yajl_integer or yajl_double are
rolf 0:34f4a53d4ca3 85 * defined, parsing of a number larger than is representable
rolf 0:34f4a53d4ca3 86 * in a double or long int will result in a parse error.
rolf 0:34f4a53d4ca3 87 */
rolf 0:34f4a53d4ca3 88 typedef struct {
rolf 0:34f4a53d4ca3 89 int (* yajl_null)(void * ctx);
rolf 0:34f4a53d4ca3 90 int (* yajl_boolean)(void * ctx, int boolVal);
rolf 0:34f4a53d4ca3 91 int (* yajl_integer)(void * ctx, long integerVal);
rolf 0:34f4a53d4ca3 92 int (* yajl_double)(void * ctx, double doubleVal);
rolf 0:34f4a53d4ca3 93 /** A callback which passes the string representation of the number
rolf 0:34f4a53d4ca3 94 * back to the client. Will be used for all numbers when present */
rolf 0:34f4a53d4ca3 95 int (* yajl_number)(void * ctx, const char * numberVal,
rolf 0:34f4a53d4ca3 96 unsigned int numberLen);
rolf 0:34f4a53d4ca3 97
rolf 0:34f4a53d4ca3 98 /** strings are returned as pointers into the JSON text when,
rolf 0:34f4a53d4ca3 99 * possible, as a result, they are _not_ null padded */
rolf 0:34f4a53d4ca3 100 int (* yajl_string)(void * ctx, const unsigned char * stringVal,
rolf 0:34f4a53d4ca3 101 unsigned int stringLen);
rolf 0:34f4a53d4ca3 102
rolf 0:34f4a53d4ca3 103 int (* yajl_start_map)(void * ctx);
rolf 0:34f4a53d4ca3 104 int (* yajl_map_key)(void * ctx, const unsigned char * key,
rolf 0:34f4a53d4ca3 105 unsigned int stringLen);
rolf 0:34f4a53d4ca3 106 int (* yajl_end_map)(void * ctx);
rolf 0:34f4a53d4ca3 107
rolf 0:34f4a53d4ca3 108 int (* yajl_start_array)(void * ctx);
rolf 0:34f4a53d4ca3 109 int (* yajl_end_array)(void * ctx);
rolf 0:34f4a53d4ca3 110 } yajl_callbacks;
rolf 0:34f4a53d4ca3 111
rolf 0:34f4a53d4ca3 112 /** configuration structure for the generator */
rolf 0:34f4a53d4ca3 113 typedef struct {
rolf 0:34f4a53d4ca3 114 /** if nonzero, javascript style comments will be allowed in
rolf 0:34f4a53d4ca3 115 * the json input, both slash star and slash slash */
rolf 0:34f4a53d4ca3 116 unsigned int allowComments;
rolf 0:34f4a53d4ca3 117 /** if nonzero, invalid UTF8 strings will cause a parse
rolf 0:34f4a53d4ca3 118 * error */
rolf 0:34f4a53d4ca3 119 unsigned int checkUTF8;
rolf 0:34f4a53d4ca3 120 } yajl_parser_config;
rolf 0:34f4a53d4ca3 121
rolf 0:34f4a53d4ca3 122 /** allocate a parser handle
rolf 0:34f4a53d4ca3 123 * \param callbacks a yajl callbacks structure specifying the
rolf 0:34f4a53d4ca3 124 * functions to call when different JSON entities
rolf 0:34f4a53d4ca3 125 * are encountered in the input text. May be NULL,
rolf 0:34f4a53d4ca3 126 * which is only useful for validation.
rolf 0:34f4a53d4ca3 127 * \param config configuration parameters for the parse.
rolf 0:34f4a53d4ca3 128 * \param ctx a context pointer that will be passed to callbacks.
rolf 0:34f4a53d4ca3 129 */
rolf 0:34f4a53d4ca3 130 yajl_handle YAJL_API yajl_alloc(const yajl_callbacks * callbacks,
rolf 0:34f4a53d4ca3 131 const yajl_parser_config * config,
rolf 0:34f4a53d4ca3 132 const yajl_alloc_funcs * allocFuncs,
rolf 0:34f4a53d4ca3 133 void * ctx);
rolf 0:34f4a53d4ca3 134
rolf 0:34f4a53d4ca3 135 /** free a parser handle */
rolf 0:34f4a53d4ca3 136 void YAJL_API yajl_free(yajl_handle handle);
rolf 0:34f4a53d4ca3 137
rolf 0:34f4a53d4ca3 138 /** Parse some json!
rolf 0:34f4a53d4ca3 139 * \param hand - a handle to the json parser allocated with yajl_alloc
rolf 0:34f4a53d4ca3 140 * \param jsonText - a pointer to the UTF8 json text to be parsed
rolf 0:34f4a53d4ca3 141 * \param jsonTextLength - the length, in bytes, of input text
rolf 0:34f4a53d4ca3 142 */
rolf 0:34f4a53d4ca3 143 yajl_status YAJL_API yajl_parse(yajl_handle hand,
rolf 0:34f4a53d4ca3 144 const unsigned char * jsonText,
rolf 0:34f4a53d4ca3 145 unsigned int jsonTextLength);
rolf 0:34f4a53d4ca3 146
rolf 0:34f4a53d4ca3 147 /** Parse any remaining buffered json.
rolf 0:34f4a53d4ca3 148 * Since yajl is a stream-based parser, without an explicit end of
rolf 0:34f4a53d4ca3 149 * input, yajl sometimes can't decide if content at the end of the
rolf 0:34f4a53d4ca3 150 * stream is valid or not. For example, if "1" has been fed in,
rolf 0:34f4a53d4ca3 151 * yajl can't know whether another digit is next or some character
rolf 0:34f4a53d4ca3 152 * that would terminate the integer token.
rolf 0:34f4a53d4ca3 153 *
rolf 0:34f4a53d4ca3 154 * \param hand - a handle to the json parser allocated with yajl_alloc
rolf 0:34f4a53d4ca3 155 */
rolf 0:34f4a53d4ca3 156 yajl_status yajl_parse_complete(yajl_handle hand);
rolf 0:34f4a53d4ca3 157
rolf 0:34f4a53d4ca3 158 /** get an error string describing the state of the
rolf 0:34f4a53d4ca3 159 * parse.
rolf 0:34f4a53d4ca3 160 *
rolf 0:34f4a53d4ca3 161 * If verbose is non-zero, the message will include the JSON
rolf 0:34f4a53d4ca3 162 * text where the error occured, along with an arrow pointing to
rolf 0:34f4a53d4ca3 163 * the specific char.
rolf 0:34f4a53d4ca3 164 *
rolf 0:34f4a53d4ca3 165 * A dynamically allocated string will be returned which should
rolf 0:34f4a53d4ca3 166 * be freed with yajl_free_error
rolf 0:34f4a53d4ca3 167 */
rolf 0:34f4a53d4ca3 168 unsigned char * YAJL_API yajl_get_error(yajl_handle hand, int verbose,
rolf 0:34f4a53d4ca3 169 const unsigned char * jsonText,
rolf 0:34f4a53d4ca3 170 unsigned int jsonTextLength);
rolf 0:34f4a53d4ca3 171
rolf 0:34f4a53d4ca3 172 /** free an error returned from yajl_get_error */
rolf 0:34f4a53d4ca3 173 void YAJL_API yajl_free_error(yajl_handle hand, unsigned char * str);
rolf 0:34f4a53d4ca3 174
rolf 0:34f4a53d4ca3 175 #ifdef __cplusplus
rolf 0:34f4a53d4ca3 176 }
rolf 0:34f4a53d4ca3 177 #endif
rolf 0:34f4a53d4ca3 178
rolf 0:34f4a53d4ca3 179 #endif