yaji 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ #ifndef __YAJL_COMMON_H__
34
+ #define __YAJL_COMMON_H__
35
+
36
+ #ifdef __cplusplus
37
+ extern "C" {
38
+ #endif
39
+
40
+ #define YAJL_MAX_DEPTH 128
41
+
42
+ /* msft dll export gunk. To build a DLL on windows, you
43
+ * must define WIN32, YAJL_SHARED, and YAJL_BUILD. To use a shared
44
+ * DLL, you must define YAJL_SHARED and WIN32 */
45
+ #if defined(WIN32) && defined(YAJL_SHARED)
46
+ # ifdef YAJL_BUILD
47
+ # define YAJL_API __declspec(dllexport)
48
+ # else
49
+ # define YAJL_API __declspec(dllimport)
50
+ # endif
51
+ #else
52
+ # if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 303
53
+ # define YAJL_API __attribute__ ((visibility("default")))
54
+ # else
55
+ # define YAJL_API
56
+ # endif
57
+ #endif
58
+
59
+ /** pointer to a malloc function, supporting client overriding memory
60
+ * allocation routines */
61
+ typedef void * (*yajl_malloc_func)(void *ctx, unsigned int sz);
62
+
63
+ /** pointer to a free function, supporting client overriding memory
64
+ * allocation routines */
65
+ typedef void (*yajl_free_func)(void *ctx, void * ptr);
66
+
67
+ /** pointer to a realloc function which can resize an allocation. */
68
+ typedef void * (*yajl_realloc_func)(void *ctx, void * ptr, unsigned int sz);
69
+
70
+ /** A structure which can be passed to yajl_*_alloc routines to allow the
71
+ * client to specify memory allocation functions to be used. */
72
+ typedef struct
73
+ {
74
+ /** pointer to a function that can allocate uninitialized memory */
75
+ yajl_malloc_func malloc;
76
+ /** pointer to a function that can resize memory allocations */
77
+ yajl_realloc_func realloc;
78
+ /** pointer to a function that can free memory allocated using
79
+ * reallocFunction or mallocFunction */
80
+ yajl_free_func free;
81
+ /** a context pointer that will be passed to above allocation routines */
82
+ void * ctx;
83
+ } yajl_alloc_funcs;
84
+
85
+ #ifdef __cplusplus
86
+ }
87
+ #endif
88
+
89
+ #endif
@@ -0,0 +1,159 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ /**
34
+ * \file yajl_gen.h
35
+ * Interface to YAJL's JSON generation facilities.
36
+ */
37
+
38
+ #include <yajl/yajl_common.h>
39
+
40
+ #ifndef __YAJL_GEN_H__
41
+ #define __YAJL_GEN_H__
42
+
43
+ #ifdef __cplusplus
44
+ extern "C" {
45
+ #endif
46
+ /** generator status codes */
47
+ typedef enum {
48
+ /** no error */
49
+ yajl_gen_status_ok = 0,
50
+ /** at a point where a map key is generated, a function other than
51
+ * yajl_gen_string was called */
52
+ yajl_gen_keys_must_be_strings,
53
+ /** YAJL's maximum generation depth was exceeded. see
54
+ * YAJL_MAX_DEPTH */
55
+ yajl_max_depth_exceeded,
56
+ /** A generator function (yajl_gen_XXX) was called while in an error
57
+ * state */
58
+ yajl_gen_in_error_state,
59
+ /** A complete JSON document has been generated */
60
+ yajl_gen_generation_complete,
61
+ /** yajl_gen_double was passed an invalid floating point value
62
+ * (infinity or NaN). */
63
+ yajl_gen_invalid_number,
64
+ /** A print callback was passed in, so there is no internal
65
+ * buffer to get from */
66
+ yajl_gen_no_buf
67
+ } yajl_gen_status;
68
+
69
+ /** an opaque handle to a generator */
70
+ typedef struct yajl_gen_t * yajl_gen;
71
+
72
+ /** a callback used for "printing" the results. */
73
+ typedef void (*yajl_print_t)(void * ctx,
74
+ const char * str,
75
+ unsigned int len);
76
+
77
+ /** configuration structure for the generator */
78
+ typedef struct {
79
+ /** generate indented (beautiful) output */
80
+ unsigned int beautify;
81
+ /** an opportunity to define an indent string. such as \\t or
82
+ * some number of spaces. default is four spaces ' '. This
83
+ * member is only relevant when beautify is true */
84
+ const char * indentString;
85
+ } yajl_gen_config;
86
+
87
+ /** allocate a generator handle
88
+ * \param config a pointer to a structure containing parameters which
89
+ * configure the behavior of the json generator
90
+ * \param allocFuncs an optional pointer to a structure which allows
91
+ * the client to overide the memory allocation
92
+ * used by yajl. May be NULL, in which case
93
+ * malloc/free/realloc will be used.
94
+ *
95
+ * \returns an allocated handle on success, NULL on failure (bad params)
96
+ */
97
+ YAJL_API yajl_gen yajl_gen_alloc(const yajl_gen_config * config,
98
+ const yajl_alloc_funcs * allocFuncs);
99
+
100
+ /** allocate a generator handle that will print to the specified
101
+ * callback rather than storing the results in an internal buffer.
102
+ * \param callback a pointer to a printer function. May be NULL
103
+ * in which case, the results will be store in an
104
+ * internal buffer.
105
+ * \param config a pointer to a structure containing parameters
106
+ * which configure the behavior of the json
107
+ * generator.
108
+ * \param allocFuncs an optional pointer to a structure which allows
109
+ * the client to overide the memory allocation
110
+ * used by yajl. May be NULL, in which case
111
+ * malloc/free/realloc will be used.
112
+ * \param ctx a context pointer that will be passed to the
113
+ * printer callback.
114
+ *
115
+ * \returns an allocated handle on success, NULL on failure (bad params)
116
+ */
117
+ YAJL_API yajl_gen yajl_gen_alloc2(const yajl_print_t callback,
118
+ const yajl_gen_config * config,
119
+ const yajl_alloc_funcs * allocFuncs,
120
+ void * ctx);
121
+
122
+ /** free a generator handle */
123
+ YAJL_API void yajl_gen_free(yajl_gen handle);
124
+
125
+ YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long int number);
126
+ /** generate a floating point number. number may not be infinity or
127
+ * NaN, as these have no representation in JSON. In these cases the
128
+ * generator will return 'yajl_gen_invalid_number' */
129
+ YAJL_API yajl_gen_status yajl_gen_double(yajl_gen hand, double number);
130
+ YAJL_API yajl_gen_status yajl_gen_number(yajl_gen hand,
131
+ const char * num,
132
+ unsigned int len);
133
+ YAJL_API yajl_gen_status yajl_gen_string(yajl_gen hand,
134
+ const unsigned char * str,
135
+ unsigned int len);
136
+ YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand);
137
+ YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean);
138
+ YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand);
139
+ YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand);
140
+ YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand);
141
+ YAJL_API yajl_gen_status yajl_gen_array_close(yajl_gen hand);
142
+
143
+ /** access the null terminated generator buffer. If incrementally
144
+ * outputing JSON, one should call yajl_gen_clear to clear the
145
+ * buffer. This allows stream generation. */
146
+ YAJL_API yajl_gen_status yajl_gen_get_buf(yajl_gen hand,
147
+ const unsigned char ** buf,
148
+ unsigned int * len);
149
+
150
+ /** clear yajl's output buffer, but maintain all internal generation
151
+ * state. This function will not "reset" the generator state, and is
152
+ * intended to enable incremental JSON outputing. */
153
+ YAJL_API void yajl_gen_clear(yajl_gen hand);
154
+
155
+ #ifdef __cplusplus
156
+ }
157
+ #endif
158
+
159
+ #endif
@@ -0,0 +1,193 @@
1
+ /*
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are
6
+ * met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in
13
+ * the documentation and/or other materials provided with the
14
+ * distribution.
15
+ *
16
+ * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
+ * contributors may be used to endorse or promote products derived
18
+ * from this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ /**
34
+ * \file yajl_parse.h
35
+ * Interface to YAJL's JSON parsing facilities.
36
+ */
37
+
38
+ #include <yajl/yajl_common.h>
39
+
40
+ #ifndef __YAJL_PARSE_H__
41
+ #define __YAJL_PARSE_H__
42
+
43
+ #ifdef __cplusplus
44
+ extern "C" {
45
+ #endif
46
+ /** error codes returned from this interface */
47
+ typedef enum {
48
+ /** no error was encountered */
49
+ yajl_status_ok,
50
+ /** a client callback returned zero, stopping the parse */
51
+ yajl_status_client_canceled,
52
+ /** The parse cannot yet complete because more json input text
53
+ * is required, call yajl_parse with the next buffer of input text.
54
+ * (pertinent only when stream parsing) */
55
+ yajl_status_insufficient_data,
56
+ /** An error occured during the parse. Call yajl_get_error for
57
+ * more information about the encountered error */
58
+ yajl_status_error
59
+ } yajl_status;
60
+
61
+ /** attain a human readable, english, string for an error */
62
+ YAJL_API const char * yajl_status_to_string(yajl_status code);
63
+
64
+ /** an opaque handle to a parser */
65
+ typedef struct yajl_handle_t * yajl_handle;
66
+
67
+ /** yajl is an event driven parser. this means as json elements are
68
+ * parsed, you are called back to do something with the data. The
69
+ * functions in this table indicate the various events for which
70
+ * you will be called back. Each callback accepts a "context"
71
+ * pointer, this is a void * that is passed into the yajl_parse
72
+ * function which the client code may use to pass around context.
73
+ *
74
+ * All callbacks return an integer. If non-zero, the parse will
75
+ * continue. If zero, the parse will be canceled and
76
+ * yajl_status_client_canceled will be returned from the parse.
77
+ *
78
+ * Note about handling of numbers:
79
+ * yajl will only convert numbers that can be represented in a double
80
+ * or a long int. All other numbers will be passed to the client
81
+ * in string form using the yajl_number callback. Furthermore, if
82
+ * yajl_number is not NULL, it will always be used to return numbers,
83
+ * that is yajl_integer and yajl_double will be ignored. If
84
+ * yajl_number is NULL but one of yajl_integer or yajl_double are
85
+ * defined, parsing of a number larger than is representable
86
+ * in a double or long int will result in a parse error.
87
+ */
88
+ typedef struct {
89
+ int (* yajl_null)(void * ctx);
90
+ int (* yajl_boolean)(void * ctx, int boolVal);
91
+ int (* yajl_integer)(void * ctx, long integerVal);
92
+ int (* yajl_double)(void * ctx, double doubleVal);
93
+ /** A callback which passes the string representation of the number
94
+ * back to the client. Will be used for all numbers when present */
95
+ int (* yajl_number)(void * ctx, const char * numberVal,
96
+ unsigned int numberLen);
97
+
98
+ /** strings are returned as pointers into the JSON text when,
99
+ * possible, as a result, they are _not_ null padded */
100
+ int (* yajl_string)(void * ctx, const unsigned char * stringVal,
101
+ unsigned int stringLen);
102
+
103
+ int (* yajl_start_map)(void * ctx);
104
+ int (* yajl_map_key)(void * ctx, const unsigned char * key,
105
+ unsigned int stringLen);
106
+ int (* yajl_end_map)(void * ctx);
107
+
108
+ int (* yajl_start_array)(void * ctx);
109
+ int (* yajl_end_array)(void * ctx);
110
+ } yajl_callbacks;
111
+
112
+ /** configuration structure for the generator */
113
+ typedef struct {
114
+ /** if nonzero, javascript style comments will be allowed in
115
+ * the json input, both slash star and slash slash */
116
+ unsigned int allowComments;
117
+ /** if nonzero, invalid UTF8 strings will cause a parse
118
+ * error */
119
+ unsigned int checkUTF8;
120
+ } yajl_parser_config;
121
+
122
+ /** allocate a parser handle
123
+ * \param callbacks a yajl callbacks structure specifying the
124
+ * functions to call when different JSON entities
125
+ * are encountered in the input text. May be NULL,
126
+ * which is only useful for validation.
127
+ * \param config configuration parameters for the parse.
128
+ * \param ctx a context pointer that will be passed to callbacks.
129
+ */
130
+ YAJL_API yajl_handle yajl_alloc(const yajl_callbacks * callbacks,
131
+ const yajl_parser_config * config,
132
+ const yajl_alloc_funcs * allocFuncs,
133
+ void * ctx);
134
+
135
+ /** free a parser handle */
136
+ YAJL_API void yajl_free(yajl_handle handle);
137
+
138
+ /** Parse some json!
139
+ * \param hand - a handle to the json parser allocated with yajl_alloc
140
+ * \param jsonText - a pointer to the UTF8 json text to be parsed
141
+ * \param jsonTextLength - the length, in bytes, of input text
142
+ */
143
+ YAJL_API yajl_status yajl_parse(yajl_handle hand,
144
+ const unsigned char * jsonText,
145
+ unsigned int jsonTextLength);
146
+
147
+ /** Parse any remaining buffered json.
148
+ * Since yajl is a stream-based parser, without an explicit end of
149
+ * input, yajl sometimes can't decide if content at the end of the
150
+ * stream is valid or not. For example, if "1" has been fed in,
151
+ * yajl can't know whether another digit is next or some character
152
+ * that would terminate the integer token.
153
+ *
154
+ * \param hand - a handle to the json parser allocated with yajl_alloc
155
+ */
156
+ YAJL_API yajl_status yajl_parse_complete(yajl_handle hand);
157
+
158
+ /** get an error string describing the state of the
159
+ * parse.
160
+ *
161
+ * If verbose is non-zero, the message will include the JSON
162
+ * text where the error occured, along with an arrow pointing to
163
+ * the specific char.
164
+ *
165
+ * \returns A dynamically allocated string will be returned which should
166
+ * be freed with yajl_free_error
167
+ */
168
+ YAJL_API unsigned char * yajl_get_error(yajl_handle hand, int verbose,
169
+ const unsigned char * jsonText,
170
+ unsigned int jsonTextLength);
171
+
172
+ /**
173
+ * get the amount of data consumed from the last chunk passed to YAJL.
174
+ *
175
+ * In the case of a successful parse this can help you understand if
176
+ * the entire buffer was consumed (which will allow you to handle
177
+ * "junk at end of input".
178
+ *
179
+ * In the event an error is encountered during parsing, this function
180
+ * affords the client a way to get the offset into the most recent
181
+ * chunk where the error occured. 0 will be returned if no error
182
+ * was encountered.
183
+ */
184
+ YAJL_API unsigned int yajl_get_bytes_consumed(yajl_handle hand);
185
+
186
+ /** free an error returned from yajl_get_error */
187
+ YAJL_API void yajl_free_error(yajl_handle hand, unsigned char * str);
188
+
189
+ #ifdef __cplusplus
190
+ }
191
+ #endif
192
+
193
+ #endif
@@ -0,0 +1,23 @@
1
+ #ifndef YAJL_VERSION_H_
2
+ #define YAJL_VERSION_H_
3
+
4
+ #include "api/yajl_common.h"
5
+
6
+ #define YAJL_MAJOR 1
7
+ #define YAJL_MINOR 0
8
+ #define YAJL_MICRO 12
9
+
10
+ #define YAJL_VERSION ((YAJL_MAJOR * 10000) + (YAJL_MINOR * 100) + YAJL_MICRO)
11
+
12
+ #ifdef __cplusplus
13
+ extern "C" {
14
+ #endif
15
+
16
+ extern int YAJL_API yajl_version(void);
17
+
18
+ #ifdef __cplusplus
19
+ }
20
+ #endif
21
+
22
+ #endif /* YAJL_VERSION_H_ */
23
+