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