source: trunk/libtransmission/JSON_parser.h @ 8050

Last change on this file since 8050 was 7197, checked in by charles, 13 years ago

(libT) add the #include guards to "private" third party header files.

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