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