| 7747 |
ripley |
1 |
#ifndef JSON_PARSER_H
|
|
|
2 |
#define JSON_PARSER_H
|
|
|
3 |
|
|
|
4 |
/* JSON_parser.h */
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
#include <stddef.h>
|
|
|
8 |
|
|
|
9 |
#if 0
|
|
|
10 |
|
|
|
11 |
/* Windows DLL stuff */
|
|
|
12 |
#ifdef _WIN32
|
|
|
13 |
# ifdef JSON_PARSER_DLL_EXPORTS
|
|
|
14 |
# define JSON_PARSER_DLL_API __declspec(dllexport)
|
|
|
15 |
# else
|
|
|
16 |
# define JSON_PARSER_DLL_API __declspec(dllimport)
|
|
|
17 |
# endif
|
|
|
18 |
#else
|
|
|
19 |
# define JSON_PARSER_DLL_API
|
|
|
20 |
#endif
|
|
|
21 |
|
|
|
22 |
#else
|
|
|
23 |
|
|
|
24 |
#define JSON_PARSER_DLL_API
|
|
|
25 |
|
|
|
26 |
#endif /* #if 0 */
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
/* Determine the integer type use to parse non-floating point numbers */
|
|
|
30 |
#if __STDC_VERSION__ >= 199901L || HAVE_LONG_LONG == 1
|
|
|
31 |
typedef long long JSON_int_t;
|
|
|
32 |
//#warning "using long long"
|
|
|
33 |
#define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld"
|
|
|
34 |
#define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld"
|
|
|
35 |
#else
|
|
|
36 |
typedef long JSON_int_t;
|
|
|
37 |
#define JSON_PARSER_INTEGER_SSCANF_TOKEN "%ld"
|
|
|
38 |
#define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%ld"
|
|
|
39 |
#endif
|
|
|
40 |
|
|
|
41 |
#define MAX_INT 2147483647L
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
#ifdef __cplusplus
|
|
|
45 |
extern "C" {
|
|
|
46 |
#endif
|
|
|
47 |
|
|
|
48 |
typedef enum
|
|
|
49 |
{
|
|
|
50 |
JSON_T_NONE = 0,
|
|
|
51 |
JSON_T_ARRAY_BEGIN,
|
|
|
52 |
JSON_T_ARRAY_END,
|
|
|
53 |
JSON_T_OBJECT_BEGIN,
|
|
|
54 |
JSON_T_OBJECT_END,
|
|
|
55 |
JSON_T_INTEGER,
|
|
|
56 |
JSON_T_FLOAT,
|
|
|
57 |
JSON_T_NULL,
|
|
|
58 |
JSON_T_TRUE,
|
|
|
59 |
JSON_T_FALSE,
|
|
|
60 |
JSON_T_STRING,
|
|
|
61 |
JSON_T_KEY,
|
|
|
62 |
JSON_T_MAX
|
|
|
63 |
} JSON_type;
|
|
|
64 |
|
|
|
65 |
#ifndef WIN32
|
|
|
66 |
typedef long double DoubleType;
|
|
|
67 |
#define DoubleScanFormat "%Lf"
|
|
|
68 |
#else
|
|
|
69 |
typedef double DoubleType;
|
|
|
70 |
#define DoubleScanFormat "%lf"
|
|
|
71 |
#endif
|
|
|
72 |
|
|
|
73 |
typedef struct JSON_value_struct {
|
|
|
74 |
union {
|
|
|
75 |
JSON_int_t integer_value;
|
|
|
76 |
|
|
|
77 |
DoubleType float_value;
|
|
|
78 |
|
|
|
79 |
struct {
|
|
|
80 |
const char* value;
|
|
|
81 |
size_t length;
|
|
|
82 |
} str;
|
|
|
83 |
} vu;
|
|
|
84 |
} JSON_value;
|
|
|
85 |
|
|
|
86 |
/*! \brief JSON parser callback
|
|
|
87 |
|
|
|
88 |
\param ctx The pointer passed to new_JSON_parser.
|
|
|
89 |
\param type An element of JSON_type but not JSON_T_NONE.
|
|
|
90 |
\param value A representation of the parsed value. This parameter is NULL for
|
|
|
91 |
JSON_T_ARRAY_BEGIN, JSON_T_ARRAY_END, JSON_T_OBJECT_BEGIN, JSON_T_OBJECT_END,
|
|
|
92 |
JSON_T_NULL, JSON_T_TRUE, and SON_T_FALSE. String values are always returned
|
|
|
93 |
as zero-terminated C strings.
|
|
|
94 |
|
|
|
95 |
\return Non-zero if parsing should continue, else zero.
|
|
|
96 |
*/
|
|
|
97 |
typedef int (*JSON_parser_callback)(void* ctx, int type, const struct JSON_value_struct* value);
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
/*! \brief The structure used to configure a JSON parser object
|
|
|
101 |
|
|
|
102 |
\param depth If negative, the parser can parse arbitrary levels of JSON, otherwise
|
|
|
103 |
the depth is the limit
|
|
|
104 |
\param Pointer to a callback. This parameter may be NULL. In this case the input is merely checked for validity.
|
|
|
105 |
\param Callback context. This parameter may be NULL.
|
|
|
106 |
\param depth. Specifies the levels of nested JSON to allow. Negative numbers yield unlimited nesting.
|
|
|
107 |
\param allowComments. To allow C style comments in JSON, set to non-zero.
|
|
|
108 |
\param handleFloatsManually. To decode floating point numbers manually set this parameter to non-zero.
|
|
|
109 |
|
|
|
110 |
\return The parser object.
|
|
|
111 |
*/
|
|
|
112 |
typedef struct JSON_config_struct {
|
|
|
113 |
JSON_parser_callback callback;
|
|
|
114 |
void* callback_ctx;
|
|
|
115 |
int depth;
|
|
|
116 |
int allow_comments;
|
|
|
117 |
int handle_floats_manually;
|
|
|
118 |
} JSON_config;
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
/*! \brief Initializes the JSON parser configuration structure to default values.
|
|
|
122 |
|
|
|
123 |
The default configuration is
|
|
|
124 |
- 127 levels of nested JSON (depends on JSON_PARSER_STACK_SIZE, see json_parser.c)
|
|
|
125 |
- no parsing, just checking for JSON syntax
|
|
|
126 |
- no comments
|
|
|
127 |
|
|
|
128 |
\param config. Used to configure the parser.
|
|
|
129 |
*/
|
|
|
130 |
JSON_PARSER_DLL_API void init_JSON_config(JSON_config* config);
|
|
|
131 |
|
|
|
132 |
/*! \brief Create a JSON parser object
|
|
|
133 |
|
|
|
134 |
\param config. Used to configure the parser. Set to NULL to use the default configuration.
|
|
|
135 |
See init_JSON_config
|
|
|
136 |
|
|
|
137 |
\return The parser object.
|
|
|
138 |
*/
|
|
|
139 |
JSON_PARSER_DLL_API extern struct JSON_parser_struct* new_JSON_parser(JSON_config* config);
|
|
|
140 |
|
|
|
141 |
/*! \brief Destroy a previously created JSON parser object. */
|
|
|
142 |
JSON_PARSER_DLL_API extern void delete_JSON_parser(struct JSON_parser_struct* jc);
|
|
|
143 |
|
|
|
144 |
/*! \brief Parse a character.
|
|
|
145 |
|
|
|
146 |
\return Non-zero, if all characters passed to this function are part of are valid JSON.
|
|
|
147 |
*/
|
|
|
148 |
JSON_PARSER_DLL_API extern int JSON_parser_char(struct JSON_parser_struct* jc, int next_char);
|
|
|
149 |
|
|
|
150 |
/*! \brief Finalize parsing.
|
|
|
151 |
|
|
|
152 |
Call this method once after all input characters have been consumed.
|
|
|
153 |
|
|
|
154 |
\return Non-zero, if all parsed characters are valid JSON, zero otherwise.
|
|
|
155 |
*/
|
|
|
156 |
JSON_PARSER_DLL_API extern int JSON_parser_done(struct JSON_parser_struct* jc);
|
|
|
157 |
|
|
|
158 |
/*! \brief Determine if a given string is valid JSON white space
|
|
|
159 |
|
|
|
160 |
\return Non-zero if the string is valid, zero otherwise.
|
|
|
161 |
*/
|
|
|
162 |
JSON_PARSER_DLL_API extern int JSON_parser_is_legal_white_space_string(const char* s);
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
#ifdef __cplusplus
|
|
|
166 |
}
|
|
|
167 |
#endif
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
#endif /* JSON_PARSER_H */
|