Skip to content
Newer
Older
100644 227 lines (182 sloc) 6.53 KB
e3324a0 @hungc Source for JSON parser (version 2010-11-25) as provided by Jean Gress…
hungc authored Jan 27, 2012
1 /* See JSON_parser.c for copyright information and licensing. */
2
3 #ifndef JSON_PARSER_H
4 #define JSON_PARSER_H
5
6 /* JSON_parser.h */
7
8
9 #include <stddef.h>
10
11 /* Windows DLL stuff */
12 #ifdef JSON_PARSER_DLL
13 # ifdef _MSC_VER
14 # ifdef JSON_PARSER_DLL_EXPORTS
15 # define JSON_PARSER_DLL_API __declspec(dllexport)
16 # else
17 # define JSON_PARSER_DLL_API __declspec(dllimport)
18 # endif
19 # else
20 # define JSON_PARSER_DLL_API
21 # endif
22 #else
23 # define JSON_PARSER_DLL_API
24 #endif
25
26 /* Determine the integer type use to parse non-floating point numbers */
27 #if __STDC_VERSION__ >= 199901L || HAVE_LONG_LONG == 1
28 typedef long long JSON_int_t;
29 #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld"
30 #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld"
31 #else
32 typedef long JSON_int_t;
33 #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%ld"
34 #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%ld"
35 #endif
36
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 typedef enum
43 {
44 JSON_E_NONE = 0,
45 JSON_E_INVALID_CHAR,
46 JSON_E_INVALID_KEYWORD,
47 JSON_E_INVALID_ESCAPE_SEQUENCE,
48 JSON_E_INVALID_UNICODE_SEQUENCE,
49 JSON_E_INVALID_NUMBER,
50 JSON_E_NESTING_DEPTH_REACHED,
51 JSON_E_UNBALANCED_COLLECTION,
52 JSON_E_EXPECTED_KEY,
53 JSON_E_EXPECTED_COLON,
54 JSON_E_OUT_OF_MEMORY
55 } JSON_error;
56
57 typedef enum
58 {
59 JSON_T_NONE = 0,
60 JSON_T_ARRAY_BEGIN,
61 JSON_T_ARRAY_END,
62 JSON_T_OBJECT_BEGIN,
63 JSON_T_OBJECT_END,
64 JSON_T_INTEGER,
65 JSON_T_FLOAT,
66 JSON_T_NULL,
67 JSON_T_TRUE,
68 JSON_T_FALSE,
69 JSON_T_STRING,
70 JSON_T_KEY,
71 JSON_T_MAX
72 } JSON_type;
73
74 typedef struct JSON_value_struct {
75 union {
76 JSON_int_t integer_value;
77
78 double float_value;
79
80 struct {
81 const char* value;
82 size_t length;
83 } str;
84 } vu;
85 } JSON_value;
86
87 typedef struct JSON_parser_struct* JSON_parser;
88
89 /*! \brief JSON parser callback
90
91 \param ctx The pointer passed to new_JSON_parser.
92 \param type An element of JSON_type but not JSON_T_NONE.
93 \param value A representation of the parsed value. This parameter is NULL for
94 JSON_T_ARRAY_BEGIN, JSON_T_ARRAY_END, JSON_T_OBJECT_BEGIN, JSON_T_OBJECT_END,
95 JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned
96 as zero-terminated C strings.
97
98 \return Non-zero if parsing should continue, else zero.
99 */
100 typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value);
101
102
103 /**
104 A typedef for allocator functions semantically compatible with malloc().
105 */
106 typedef void* (*JSON_malloc_t)(size_t n);
107 /**
108 A typedef for deallocator functions semantically compatible with free().
109 */
110 typedef void (*JSON_free_t)(void* mem);
111
112 /*! \brief The structure used to configure a JSON parser object
113 */
114 typedef struct {
115 /** Pointer to a callback, called when the parser has something to tell
116 the user. This parameter may be NULL. In this case the input is
117 merely checked for validity.
118 */
119 JSON_parser_callback callback;
120 /**
121 Callback context - client-specified data to pass to the
122 callback function. This parameter may be NULL.
123 */
124 void* callback_ctx;
125 /** Specifies the levels of nested JSON to allow. Negative numbers yield unlimited nesting.
126 If negative, the parser can parse arbitrary levels of JSON, otherwise
127 the depth is the limit.
128 */
129 int depth;
130 /**
131 To allow C style comments in JSON, set to non-zero.
132 */
133 int allow_comments;
134 /**
135 To decode floating point numbers manually set this parameter to
136 non-zero.
137 */
138 int handle_floats_manually;
139 /**
140 The memory allocation routine, which must be semantically
141 compatible with malloc(3). If set to NULL, malloc(3) is used.
142
143 If this is set to a non-NULL value then the 'free' member MUST be
144 set to the proper deallocation counterpart for this function.
145 Failure to do so results in undefined behaviour at deallocation
146 time.
147 */
148 JSON_malloc_t malloc;
149 /**
150 The memory deallocation routine, which must be semantically
151 compatible with free(3). If set to NULL, free(3) is used.
152
153 If this is set to a non-NULL value then the 'alloc' member MUST be
154 set to the proper allocation counterpart for this function.
155 Failure to do so results in undefined behaviour at deallocation
156 time.
157 */
158 JSON_free_t free;
159 } JSON_config;
160
161 /*! \brief Initializes the JSON parser configuration structure to default values.
162
163 The default configuration is
164 - 127 levels of nested JSON (depends on JSON_PARSER_STACK_SIZE, see json_parser.c)
165 - no parsing, just checking for JSON syntax
166 - no comments
167 - Uses realloc() for memory de/allocation.
168
169 \param config. Used to configure the parser.
170 */
171 JSON_PARSER_DLL_API void init_JSON_config(JSON_config * config);
172
173 /*! \brief Create a JSON parser object
174
175 \param config. Used to configure the parser. Set to NULL to use
176 the default configuration. See init_JSON_config. Its contents are
177 copied by this function, so it need not outlive the returned
178 object.
179
180 \return The parser object, which is owned by the caller and must eventually
181 be freed by calling delete_JSON_parser().
182 */
183 JSON_PARSER_DLL_API JSON_parser new_JSON_parser(JSON_config const* config);
184
185 /*! \brief Destroy a previously created JSON parser object. */
186 JSON_PARSER_DLL_API void delete_JSON_parser(JSON_parser jc);
187
188 /*! \brief Parse a character.
189
190 \return Non-zero, if all characters passed to this function are part of are valid JSON.
191 */
192 JSON_PARSER_DLL_API int JSON_parser_char(JSON_parser jc, int next_char);
193
194 /*! \brief Finalize parsing.
195
196 Call this method once after all input characters have been consumed.
197
198 \return Non-zero, if all parsed characters are valid JSON, zero otherwise.
199 */
200 JSON_PARSER_DLL_API int JSON_parser_done(JSON_parser jc);
201
202 /*! \brief Determine if a given string is valid JSON white space
203
204 \return Non-zero if the string is valid, zero otherwise.
205 */
206 JSON_PARSER_DLL_API int JSON_parser_is_legal_white_space_string(const char* s);
207
208 /*! \brief Gets the last error that occurred during the use of JSON_parser.
209
210 \return A value from the JSON_error enum.
211 */
212 JSON_PARSER_DLL_API int JSON_parser_get_last_error(JSON_parser jc);
213
214 /*! \brief Re-sets the parser to prepare it for another parse run.
215
216 \return True (non-zero) on success, 0 on error (e.g. !jc).
217 */
218 JSON_PARSER_DLL_API int JSON_parser_reset(JSON_parser jc);
219
220
221 #ifdef __cplusplus
222 }
223 #endif
224
225
226 #endif /* JSON_PARSER_H */
Something went wrong with that request. Please try again.