source: branches/1.7x/libtransmission/JSON_parser.h @ 9359

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

(1.7x) backports for 1.76: r9307 #2360 ratio-limit becomes a strange value on uClibc

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