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