libyajl2 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +65 -10
- data/ext/libyajl2/api/yajl_common.h +75 -0
- data/ext/libyajl2/api/yajl_gen.h +167 -0
- data/ext/libyajl2/api/yajl_parse.h +226 -0
- data/ext/libyajl2/api/yajl_tree.h +186 -0
- data/ext/libyajl2/api/yajl_version.h +23 -0
- data/ext/libyajl2/extconf.rb +24 -177
- data/ext/libyajl2/patches/000-mingw-gcc.patch +26 -0
- data/ext/libyajl2/yajl/yajl_common.h +75 -0
- data/ext/libyajl2/yajl/yajl_gen.h +167 -0
- data/ext/libyajl2/yajl/yajl_parse.h +226 -0
- data/ext/libyajl2/yajl/yajl_tree.h +186 -0
- data/ext/libyajl2/yajl/yajl_version.h +23 -0
- data/ext/libyajl2/yajl.c +175 -0
- data/ext/libyajl2/yajl_alloc.c +52 -0
- data/ext/libyajl2/yajl_alloc.h +34 -0
- data/ext/libyajl2/yajl_buf.c +103 -0
- data/ext/libyajl2/yajl_buf.h +57 -0
- data/ext/libyajl2/yajl_bytestack.h +69 -0
- data/ext/libyajl2/yajl_encode.c +220 -0
- data/ext/libyajl2/yajl_encode.h +34 -0
- data/ext/libyajl2/yajl_gen.c +362 -0
- data/ext/libyajl2/yajl_lex.c +763 -0
- data/ext/libyajl2/yajl_lex.h +117 -0
- data/ext/libyajl2/yajl_parser.c +498 -0
- data/ext/libyajl2/yajl_parser.h +78 -0
- data/ext/libyajl2/yajl_tree.c +503 -0
- data/ext/libyajl2/yajl_version.c +7 -0
- data/lib/libyajl2/version.rb +1 -1
- metadata +28 -3
- data/ext/libyajl2/Makefile +0 -9
@@ -0,0 +1,167 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
|
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_gen.h
|
19
|
+
* Interface to YAJL's JSON generation facilities.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#include <yajl/yajl_common.h>
|
23
|
+
|
24
|
+
#ifndef __YAJL_GEN_H__
|
25
|
+
#define __YAJL_GEN_H__
|
26
|
+
|
27
|
+
#include <stddef.h>
|
28
|
+
|
29
|
+
#ifdef __cplusplus
|
30
|
+
extern "C" {
|
31
|
+
#endif
|
32
|
+
/** generator status codes */
|
33
|
+
typedef enum {
|
34
|
+
/** no error */
|
35
|
+
yajl_gen_status_ok = 0,
|
36
|
+
/** at a point where a map key is generated, a function other than
|
37
|
+
* yajl_gen_string was called */
|
38
|
+
yajl_gen_keys_must_be_strings,
|
39
|
+
/** YAJL's maximum generation depth was exceeded. see
|
40
|
+
* YAJL_MAX_DEPTH */
|
41
|
+
yajl_max_depth_exceeded,
|
42
|
+
/** A generator function (yajl_gen_XXX) was called while in an error
|
43
|
+
* state */
|
44
|
+
yajl_gen_in_error_state,
|
45
|
+
/** A complete JSON document has been generated */
|
46
|
+
yajl_gen_generation_complete,
|
47
|
+
/** yajl_gen_double was passed an invalid floating point value
|
48
|
+
* (infinity or NaN). */
|
49
|
+
yajl_gen_invalid_number,
|
50
|
+
/** A print callback was passed in, so there is no internal
|
51
|
+
* buffer to get from */
|
52
|
+
yajl_gen_no_buf,
|
53
|
+
/** returned from yajl_gen_string() when the yajl_gen_validate_utf8
|
54
|
+
* option is enabled and an invalid was passed by client code.
|
55
|
+
*/
|
56
|
+
yajl_gen_invalid_string
|
57
|
+
} yajl_gen_status;
|
58
|
+
|
59
|
+
/** an opaque handle to a generator */
|
60
|
+
typedef struct yajl_gen_t * yajl_gen;
|
61
|
+
|
62
|
+
/** a callback used for "printing" the results. */
|
63
|
+
typedef void (*yajl_print_t)(void * ctx,
|
64
|
+
const char * str,
|
65
|
+
size_t len);
|
66
|
+
|
67
|
+
/** configuration parameters for the parser, these may be passed to
|
68
|
+
* yajl_gen_config() along with option specific argument(s). In general,
|
69
|
+
* all configuration parameters default to *off*. */
|
70
|
+
typedef enum {
|
71
|
+
/** generate indented (beautiful) output */
|
72
|
+
yajl_gen_beautify = 0x01,
|
73
|
+
/**
|
74
|
+
* Set an indent string which is used when yajl_gen_beautify
|
75
|
+
* is enabled. Maybe something like \\t or some number of
|
76
|
+
* spaces. The default is four spaces ' '.
|
77
|
+
*/
|
78
|
+
yajl_gen_indent_string = 0x02,
|
79
|
+
/**
|
80
|
+
* Set a function and context argument that should be used to
|
81
|
+
* output generated json. the function should conform to the
|
82
|
+
* yajl_print_t prototype while the context argument is a
|
83
|
+
* void * of your choosing.
|
84
|
+
*
|
85
|
+
* example:
|
86
|
+
* yajl_gen_config(g, yajl_gen_print_callback, myFunc, myVoidPtr);
|
87
|
+
*/
|
88
|
+
yajl_gen_print_callback = 0x04,
|
89
|
+
/**
|
90
|
+
* Normally the generator does not validate that strings you
|
91
|
+
* pass to it via yajl_gen_string() are valid UTF8. Enabling
|
92
|
+
* this option will cause it to do so.
|
93
|
+
*/
|
94
|
+
yajl_gen_validate_utf8 = 0x08,
|
95
|
+
/**
|
96
|
+
* the forward solidus (slash or '/' in human) is not required to be
|
97
|
+
* escaped in json text. By default, YAJL will not escape it in the
|
98
|
+
* iterest of saving bytes. Setting this flag will cause YAJL to
|
99
|
+
* always escape '/' in generated JSON strings.
|
100
|
+
*/
|
101
|
+
yajl_gen_escape_solidus = 0x10
|
102
|
+
} yajl_gen_option;
|
103
|
+
|
104
|
+
/** allow the modification of generator options subsequent to handle
|
105
|
+
* allocation (via yajl_alloc)
|
106
|
+
* \returns zero in case of errors, non-zero otherwise
|
107
|
+
*/
|
108
|
+
YAJL_API int yajl_gen_config(yajl_gen g, yajl_gen_option opt, ...);
|
109
|
+
|
110
|
+
/** allocate a generator handle
|
111
|
+
* \param allocFuncs an optional pointer to a structure which allows
|
112
|
+
* the client to overide the memory allocation
|
113
|
+
* used by yajl. May be NULL, in which case
|
114
|
+
* malloc/free/realloc will be used.
|
115
|
+
*
|
116
|
+
* \returns an allocated handle on success, NULL on failure (bad params)
|
117
|
+
*/
|
118
|
+
YAJL_API yajl_gen yajl_gen_alloc(const yajl_alloc_funcs * allocFuncs);
|
119
|
+
|
120
|
+
/** free a generator handle */
|
121
|
+
YAJL_API void yajl_gen_free(yajl_gen handle);
|
122
|
+
|
123
|
+
YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long long int number);
|
124
|
+
/** generate a floating point number. number may not be infinity or
|
125
|
+
* NaN, as these have no representation in JSON. In these cases the
|
126
|
+
* generator will return 'yajl_gen_invalid_number' */
|
127
|
+
YAJL_API yajl_gen_status yajl_gen_double(yajl_gen hand, double number);
|
128
|
+
YAJL_API yajl_gen_status yajl_gen_number(yajl_gen hand,
|
129
|
+
const char * num,
|
130
|
+
size_t len);
|
131
|
+
YAJL_API yajl_gen_status yajl_gen_string(yajl_gen hand,
|
132
|
+
const unsigned char * str,
|
133
|
+
size_t len);
|
134
|
+
YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand);
|
135
|
+
YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean);
|
136
|
+
YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand);
|
137
|
+
YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand);
|
138
|
+
YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand);
|
139
|
+
YAJL_API yajl_gen_status yajl_gen_array_close(yajl_gen hand);
|
140
|
+
|
141
|
+
/** access the null terminated generator buffer. If incrementally
|
142
|
+
* outputing JSON, one should call yajl_gen_clear to clear the
|
143
|
+
* buffer. This allows stream generation. */
|
144
|
+
YAJL_API yajl_gen_status yajl_gen_get_buf(yajl_gen hand,
|
145
|
+
const unsigned char ** buf,
|
146
|
+
size_t * len);
|
147
|
+
|
148
|
+
/** clear yajl's output buffer, but maintain all internal generation
|
149
|
+
* state. This function will not "reset" the generator state, and is
|
150
|
+
* intended to enable incremental JSON outputing. */
|
151
|
+
YAJL_API void yajl_gen_clear(yajl_gen hand);
|
152
|
+
|
153
|
+
/** Reset the generator state. Allows a client to generate multiple
|
154
|
+
* json entities in a stream. The "sep" string will be inserted to
|
155
|
+
* separate the previously generated entity from the current,
|
156
|
+
* NULL means *no separation* of entites (clients beware, generating
|
157
|
+
* multiple JSON numbers without a separator, for instance, will result in ambiguous output)
|
158
|
+
*
|
159
|
+
* Note: this call will not clear yajl's output buffer. This
|
160
|
+
* may be accomplished explicitly by calling yajl_gen_clear() */
|
161
|
+
YAJL_API void yajl_gen_reset(yajl_gen hand, const char * sep);
|
162
|
+
|
163
|
+
#ifdef __cplusplus
|
164
|
+
}
|
165
|
+
#endif
|
166
|
+
|
167
|
+
#endif
|
@@ -0,0 +1,226 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
|
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_parse.h
|
19
|
+
* Interface to YAJL's JSON stream parsing facilities.
|
20
|
+
*/
|
21
|
+
|
22
|
+
#include <yajl/yajl_common.h>
|
23
|
+
|
24
|
+
#ifndef __YAJL_PARSE_H__
|
25
|
+
#define __YAJL_PARSE_H__
|
26
|
+
|
27
|
+
#include <stddef.h>
|
28
|
+
|
29
|
+
#ifdef __cplusplus
|
30
|
+
extern "C" {
|
31
|
+
#endif
|
32
|
+
/** error codes returned from this interface */
|
33
|
+
typedef enum {
|
34
|
+
/** no error was encountered */
|
35
|
+
yajl_status_ok,
|
36
|
+
/** a client callback returned zero, stopping the parse */
|
37
|
+
yajl_status_client_canceled,
|
38
|
+
/** An error occured during the parse. Call yajl_get_error for
|
39
|
+
* more information about the encountered error */
|
40
|
+
yajl_status_error
|
41
|
+
} yajl_status;
|
42
|
+
|
43
|
+
/** attain a human readable, english, string for an error */
|
44
|
+
YAJL_API const char * yajl_status_to_string(yajl_status code);
|
45
|
+
|
46
|
+
/** an opaque handle to a parser */
|
47
|
+
typedef struct yajl_handle_t * yajl_handle;
|
48
|
+
|
49
|
+
/** yajl is an event driven parser. this means as json elements are
|
50
|
+
* parsed, you are called back to do something with the data. The
|
51
|
+
* functions in this table indicate the various events for which
|
52
|
+
* you will be called back. Each callback accepts a "context"
|
53
|
+
* pointer, this is a void * that is passed into the yajl_parse
|
54
|
+
* function which the client code may use to pass around context.
|
55
|
+
*
|
56
|
+
* All callbacks return an integer. If non-zero, the parse will
|
57
|
+
* continue. If zero, the parse will be canceled and
|
58
|
+
* yajl_status_client_canceled will be returned from the parse.
|
59
|
+
*
|
60
|
+
* \attention {
|
61
|
+
* A note about the handling of numbers:
|
62
|
+
*
|
63
|
+
* yajl will only convert numbers that can be represented in a
|
64
|
+
* double or a 64 bit (long long) int. All other numbers will
|
65
|
+
* be passed to the client in string form using the yajl_number
|
66
|
+
* callback. Furthermore, if yajl_number is not NULL, it will
|
67
|
+
* always be used to return numbers, that is yajl_integer and
|
68
|
+
* yajl_double will be ignored. If yajl_number is NULL but one
|
69
|
+
* of yajl_integer or yajl_double are defined, parsing of a
|
70
|
+
* number larger than is representable in a double or 64 bit
|
71
|
+
* integer will result in a parse error.
|
72
|
+
* }
|
73
|
+
*/
|
74
|
+
typedef struct {
|
75
|
+
int (* yajl_null)(void * ctx);
|
76
|
+
int (* yajl_boolean)(void * ctx, int boolVal);
|
77
|
+
int (* yajl_integer)(void * ctx, long long integerVal);
|
78
|
+
int (* yajl_double)(void * ctx, double doubleVal);
|
79
|
+
/** A callback which passes the string representation of the number
|
80
|
+
* back to the client. Will be used for all numbers when present */
|
81
|
+
int (* yajl_number)(void * ctx, const char * numberVal,
|
82
|
+
size_t numberLen);
|
83
|
+
|
84
|
+
/** strings are returned as pointers into the JSON text when,
|
85
|
+
* possible, as a result, they are _not_ null padded */
|
86
|
+
int (* yajl_string)(void * ctx, const unsigned char * stringVal,
|
87
|
+
size_t stringLen);
|
88
|
+
|
89
|
+
int (* yajl_start_map)(void * ctx);
|
90
|
+
int (* yajl_map_key)(void * ctx, const unsigned char * key,
|
91
|
+
size_t stringLen);
|
92
|
+
int (* yajl_end_map)(void * ctx);
|
93
|
+
|
94
|
+
int (* yajl_start_array)(void * ctx);
|
95
|
+
int (* yajl_end_array)(void * ctx);
|
96
|
+
} yajl_callbacks;
|
97
|
+
|
98
|
+
/** allocate a parser handle
|
99
|
+
* \param callbacks a yajl callbacks structure specifying the
|
100
|
+
* functions to call when different JSON entities
|
101
|
+
* are encountered in the input text. May be NULL,
|
102
|
+
* which is only useful for validation.
|
103
|
+
* \param afs memory allocation functions, may be NULL for to use
|
104
|
+
* C runtime library routines (malloc and friends)
|
105
|
+
* \param ctx a context pointer that will be passed to callbacks.
|
106
|
+
*/
|
107
|
+
YAJL_API yajl_handle yajl_alloc(const yajl_callbacks * callbacks,
|
108
|
+
yajl_alloc_funcs * afs,
|
109
|
+
void * ctx);
|
110
|
+
|
111
|
+
|
112
|
+
/** configuration parameters for the parser, these may be passed to
|
113
|
+
* yajl_config() along with option specific argument(s). In general,
|
114
|
+
* all configuration parameters default to *off*. */
|
115
|
+
typedef enum {
|
116
|
+
/** Ignore javascript style comments present in
|
117
|
+
* JSON input. Non-standard, but rather fun
|
118
|
+
* arguments: toggled off with integer zero, on otherwise.
|
119
|
+
*
|
120
|
+
* example:
|
121
|
+
* yajl_config(h, yajl_allow_comments, 1); // turn comment support on
|
122
|
+
*/
|
123
|
+
yajl_allow_comments = 0x01,
|
124
|
+
/**
|
125
|
+
* When set the parser will verify that all strings in JSON input are
|
126
|
+
* valid UTF8 and will emit a parse error if this is not so. When set,
|
127
|
+
* this option makes parsing slightly more expensive (~7% depending
|
128
|
+
* on processor and compiler in use)
|
129
|
+
*
|
130
|
+
* example:
|
131
|
+
* yajl_config(h, yajl_dont_validate_strings, 1); // disable utf8 checking
|
132
|
+
*/
|
133
|
+
yajl_dont_validate_strings = 0x02,
|
134
|
+
/**
|
135
|
+
* By default, upon calls to yajl_complete_parse(), yajl will
|
136
|
+
* ensure the entire input text was consumed and will raise an error
|
137
|
+
* otherwise. Enabling this flag will cause yajl to disable this
|
138
|
+
* check. This can be useful when parsing json out of a that contains more
|
139
|
+
* than a single JSON document.
|
140
|
+
*/
|
141
|
+
yajl_allow_trailing_garbage = 0x04,
|
142
|
+
/**
|
143
|
+
* Allow multiple values to be parsed by a single handle. The
|
144
|
+
* entire text must be valid JSON, and values can be seperated
|
145
|
+
* by any kind of whitespace. This flag will change the
|
146
|
+
* behavior of the parser, and cause it continue parsing after
|
147
|
+
* a value is parsed, rather than transitioning into a
|
148
|
+
* complete state. This option can be useful when parsing multiple
|
149
|
+
* values from an input stream.
|
150
|
+
*/
|
151
|
+
yajl_allow_multiple_values = 0x08,
|
152
|
+
/**
|
153
|
+
* When yajl_complete_parse() is called the parser will
|
154
|
+
* check that the top level value was completely consumed. I.E.,
|
155
|
+
* if called whilst in the middle of parsing a value
|
156
|
+
* yajl will enter an error state (premature EOF). Setting this
|
157
|
+
* flag suppresses that check and the corresponding error.
|
158
|
+
*/
|
159
|
+
yajl_allow_partial_values = 0x10
|
160
|
+
} yajl_option;
|
161
|
+
|
162
|
+
/** allow the modification of parser options subsequent to handle
|
163
|
+
* allocation (via yajl_alloc)
|
164
|
+
* \returns zero in case of errors, non-zero otherwise
|
165
|
+
*/
|
166
|
+
YAJL_API int yajl_config(yajl_handle h, yajl_option opt, ...);
|
167
|
+
|
168
|
+
/** free a parser handle */
|
169
|
+
YAJL_API void yajl_free(yajl_handle handle);
|
170
|
+
|
171
|
+
/** Parse some json!
|
172
|
+
* \param hand - a handle to the json parser allocated with yajl_alloc
|
173
|
+
* \param jsonText - a pointer to the UTF8 json text to be parsed
|
174
|
+
* \param jsonTextLength - the length, in bytes, of input text
|
175
|
+
*/
|
176
|
+
YAJL_API yajl_status yajl_parse(yajl_handle hand,
|
177
|
+
const unsigned char * jsonText,
|
178
|
+
size_t jsonTextLength);
|
179
|
+
|
180
|
+
/** Parse any remaining buffered json.
|
181
|
+
* Since yajl is a stream-based parser, without an explicit end of
|
182
|
+
* input, yajl sometimes can't decide if content at the end of the
|
183
|
+
* stream is valid or not. For example, if "1" has been fed in,
|
184
|
+
* yajl can't know whether another digit is next or some character
|
185
|
+
* that would terminate the integer token.
|
186
|
+
*
|
187
|
+
* \param hand - a handle to the json parser allocated with yajl_alloc
|
188
|
+
*/
|
189
|
+
YAJL_API yajl_status yajl_complete_parse(yajl_handle hand);
|
190
|
+
|
191
|
+
/** get an error string describing the state of the
|
192
|
+
* parse.
|
193
|
+
*
|
194
|
+
* If verbose is non-zero, the message will include the JSON
|
195
|
+
* text where the error occured, along with an arrow pointing to
|
196
|
+
* the specific char.
|
197
|
+
*
|
198
|
+
* \returns A dynamically allocated string will be returned which should
|
199
|
+
* be freed with yajl_free_error
|
200
|
+
*/
|
201
|
+
YAJL_API unsigned char * yajl_get_error(yajl_handle hand, int verbose,
|
202
|
+
const unsigned char * jsonText,
|
203
|
+
size_t jsonTextLength);
|
204
|
+
|
205
|
+
/**
|
206
|
+
* get the amount of data consumed from the last chunk passed to YAJL.
|
207
|
+
*
|
208
|
+
* In the case of a successful parse this can help you understand if
|
209
|
+
* the entire buffer was consumed (which will allow you to handle
|
210
|
+
* "junk at end of input").
|
211
|
+
*
|
212
|
+
* In the event an error is encountered during parsing, this function
|
213
|
+
* affords the client a way to get the offset into the most recent
|
214
|
+
* chunk where the error occured. 0 will be returned if no error
|
215
|
+
* was encountered.
|
216
|
+
*/
|
217
|
+
YAJL_API size_t yajl_get_bytes_consumed(yajl_handle hand);
|
218
|
+
|
219
|
+
/** free an error returned from yajl_get_error */
|
220
|
+
YAJL_API void yajl_free_error(yajl_handle hand, unsigned char * str);
|
221
|
+
|
222
|
+
#ifdef __cplusplus
|
223
|
+
}
|
224
|
+
#endif
|
225
|
+
|
226
|
+
#endif
|
@@ -0,0 +1,186 @@
|
|
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/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
|
+
/**
|
62
|
+
* A JSON value representation capable of holding one of the seven
|
63
|
+
* types above. For "string", "number", "object", and "array"
|
64
|
+
* additional data is available in the union. The "YAJL_IS_*"
|
65
|
+
* and "YAJL_GET_*" macros below allow type checking and convenient
|
66
|
+
* value extraction.
|
67
|
+
*/
|
68
|
+
struct yajl_val_s
|
69
|
+
{
|
70
|
+
/** Type of the value contained. Use the "YAJL_IS_*" macros to check for a
|
71
|
+
* specific type. */
|
72
|
+
yajl_type type;
|
73
|
+
/** Type-specific data. You may use the "YAJL_GET_*" macros to access these
|
74
|
+
* members. */
|
75
|
+
union
|
76
|
+
{
|
77
|
+
char * string;
|
78
|
+
struct {
|
79
|
+
long long i; /*< integer value, if representable. */
|
80
|
+
double d; /*< double value, if representable. */
|
81
|
+
char *r; /*< unparsed number in string form. */
|
82
|
+
/** Signals whether the \em i and \em d members are
|
83
|
+
* valid. See \c YAJL_NUMBER_INT_VALID and
|
84
|
+
* \c YAJL_NUMBER_DOUBLE_VALID. */
|
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
|
+
|
99
|
+
/**
|
100
|
+
* Parse a string.
|
101
|
+
*
|
102
|
+
* Parses an null-terminated string containing JSON data and returns a pointer
|
103
|
+
* to the top-level value (root of the parse tree).
|
104
|
+
*
|
105
|
+
* \param input Pointer to a null-terminated utf8 string containing
|
106
|
+
* JSON data.
|
107
|
+
* \param error_buffer Pointer to a buffer in which an error message will
|
108
|
+
* be stored if \em yajl_tree_parse fails, or
|
109
|
+
* \c NULL. The buffer will be initialized before
|
110
|
+
* parsing, so its content will be destroyed even if
|
111
|
+
* \em yajl_tree_parse succeeds.
|
112
|
+
* \param error_buffer_size Size of the memory area pointed to by
|
113
|
+
* \em error_buffer_size. If \em error_buffer_size is
|
114
|
+
* \c NULL, this argument is ignored.
|
115
|
+
*
|
116
|
+
* \returns Pointer to the top-level value or \c NULL on error. The memory
|
117
|
+
* pointed to must be freed using \em yajl_tree_free. In case of an error, a
|
118
|
+
* null terminated message describing the error in more detail is stored in
|
119
|
+
* \em error_buffer if it is not \c NULL.
|
120
|
+
*/
|
121
|
+
YAJL_API yajl_val yajl_tree_parse (const char *input,
|
122
|
+
char *error_buffer, size_t error_buffer_size);
|
123
|
+
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Free a parse tree returned by "yajl_tree_parse".
|
127
|
+
*
|
128
|
+
* \param v Pointer to a JSON value returned by "yajl_tree_parse". Passing NULL
|
129
|
+
* is valid and results in a no-op.
|
130
|
+
*/
|
131
|
+
YAJL_API void yajl_tree_free (yajl_val v);
|
132
|
+
|
133
|
+
/**
|
134
|
+
* Access a nested value inside a tree.
|
135
|
+
*
|
136
|
+
* \param parent the node under which you'd like to extract values.
|
137
|
+
* \param path A null terminated array of strings, each the name of an object key
|
138
|
+
* \param type the yajl_type of the object you seek, or yajl_t_any if any will do.
|
139
|
+
*
|
140
|
+
* \returns a pointer to the found value, or NULL if we came up empty.
|
141
|
+
*
|
142
|
+
* Future Ideas: it'd be nice to move path to a string and implement support for
|
143
|
+
* a teeny tiny micro language here, so you can extract array elements, do things
|
144
|
+
* like .first and .last, even .length. Inspiration from JSONPath and css selectors?
|
145
|
+
* No it wouldn't be fast, but that's not what this API is about.
|
146
|
+
*/
|
147
|
+
YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type type);
|
148
|
+
|
149
|
+
/* Various convenience macros to check the type of a `yajl_val` */
|
150
|
+
#define YAJL_IS_STRING(v) (((v) != NULL) && ((v)->type == yajl_t_string))
|
151
|
+
#define YAJL_IS_NUMBER(v) (((v) != NULL) && ((v)->type == yajl_t_number))
|
152
|
+
#define YAJL_IS_INTEGER(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_INT_VALID))
|
153
|
+
#define YAJL_IS_DOUBLE(v) (YAJL_IS_NUMBER(v) && ((v)->u.number.flags & YAJL_NUMBER_DOUBLE_VALID))
|
154
|
+
#define YAJL_IS_OBJECT(v) (((v) != NULL) && ((v)->type == yajl_t_object))
|
155
|
+
#define YAJL_IS_ARRAY(v) (((v) != NULL) && ((v)->type == yajl_t_array ))
|
156
|
+
#define YAJL_IS_TRUE(v) (((v) != NULL) && ((v)->type == yajl_t_true ))
|
157
|
+
#define YAJL_IS_FALSE(v) (((v) != NULL) && ((v)->type == yajl_t_false ))
|
158
|
+
#define YAJL_IS_NULL(v) (((v) != NULL) && ((v)->type == yajl_t_null ))
|
159
|
+
|
160
|
+
/** Given a yajl_val_string return a ptr to the bare string it contains,
|
161
|
+
* or NULL if the value is not a string. */
|
162
|
+
#define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->u.string : NULL)
|
163
|
+
|
164
|
+
/** Get the string representation of a number. You should check type first,
|
165
|
+
* perhaps using YAJL_IS_NUMBER */
|
166
|
+
#define YAJL_GET_NUMBER(v) ((v)->u.number.r)
|
167
|
+
|
168
|
+
/** Get the double representation of a number. You should check type first,
|
169
|
+
* perhaps using YAJL_IS_DOUBLE */
|
170
|
+
#define YAJL_GET_DOUBLE(v) ((v)->u.number.d)
|
171
|
+
|
172
|
+
/** Get the 64bit (long long) integer representation of a number. You should
|
173
|
+
* check type first, perhaps using YAJL_IS_INTEGER */
|
174
|
+
#define YAJL_GET_INTEGER(v) ((v)->u.number.i)
|
175
|
+
|
176
|
+
/** Get a pointer to a yajl_val_object or NULL if the value is not an object. */
|
177
|
+
#define YAJL_GET_OBJECT(v) (YAJL_IS_OBJECT(v) ? &(v)->u.object : NULL)
|
178
|
+
|
179
|
+
/** Get a pointer to a yajl_val_array or NULL if the value is not an object. */
|
180
|
+
#define YAJL_GET_ARRAY(v) (YAJL_IS_ARRAY(v) ? &(v)->u.array : NULL)
|
181
|
+
|
182
|
+
#ifdef __cplusplus
|
183
|
+
}
|
184
|
+
#endif
|
185
|
+
|
186
|
+
#endif /* YAJL_TREE_H */
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#ifndef YAJL_VERSION_H_
|
2
|
+
#define YAJL_VERSION_H_
|
3
|
+
|
4
|
+
#include <yajl/yajl_common.h>
|
5
|
+
|
6
|
+
#define YAJL_MAJOR 2
|
7
|
+
#define YAJL_MINOR 1
|
8
|
+
#define YAJL_MICRO 1
|
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
|
+
|