2009-12-15 00:51:39 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
|
|
|
#error only libtransmission should #include this header.
|
|
|
|
#endif
|
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
#ifndef JSON_PARSER_H
|
|
|
|
#define JSON_PARSER_H
|
|
|
|
|
|
|
|
/* JSON_parser.h */
|
|
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
/* Windows DLL stuff */
|
2010-06-21 16:44:35 +00:00
|
|
|
#if defined(WIN32) && !defined(STATICLIB)
|
2009-05-15 13:16:34 +00:00
|
|
|
# ifdef JSON_PARSER_DLL_EXPORTS
|
|
|
|
# define JSON_PARSER_DLL_API __declspec(dllexport)
|
|
|
|
# else
|
|
|
|
# define JSON_PARSER_DLL_API __declspec(dllimport)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define JSON_PARSER_DLL_API
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Determine the integer type use to parse non-floating point numbers */
|
2010-04-17 13:29:02 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
typedef int64_t JSON_int_t;
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
2009-05-15 13:16:34 +00:00
|
|
|
#endif
|
2008-05-21 14:19:49 +00:00
|
|
|
|
2009-05-15 13:16:34 +00:00
|
|
|
typedef enum
|
2008-05-21 14:19:49 +00:00
|
|
|
{
|
|
|
|
JSON_T_NONE = 0,
|
|
|
|
JSON_T_ARRAY_BEGIN,
|
|
|
|
JSON_T_ARRAY_END,
|
|
|
|
JSON_T_OBJECT_BEGIN,
|
|
|
|
JSON_T_OBJECT_END,
|
|
|
|
JSON_T_INTEGER,
|
|
|
|
JSON_T_FLOAT,
|
|
|
|
JSON_T_NULL,
|
|
|
|
JSON_T_TRUE,
|
|
|
|
JSON_T_FALSE,
|
|
|
|
JSON_T_STRING,
|
|
|
|
JSON_T_KEY,
|
|
|
|
JSON_T_MAX
|
|
|
|
} JSON_type;
|
|
|
|
|
2009-05-15 13:16:34 +00:00
|
|
|
typedef struct JSON_value_struct {
|
|
|
|
union {
|
|
|
|
JSON_int_t integer_value;
|
|
|
|
|
2009-10-14 13:22:42 +00:00
|
|
|
double float_value;
|
2009-05-15 13:16:34 +00:00
|
|
|
|
|
|
|
struct {
|
2008-05-29 03:24:26 +00:00
|
|
|
const char* value;
|
2009-05-15 13:16:34 +00:00
|
|
|
size_t length;
|
2008-05-29 03:24:26 +00:00
|
|
|
} str;
|
|
|
|
} vu;
|
2008-05-21 14:19:49 +00:00
|
|
|
} JSON_value;
|
|
|
|
|
2009-05-15 13:16:34 +00:00
|
|
|
typedef struct JSON_parser_struct* JSON_parser;
|
|
|
|
|
|
|
|
/*! \brief JSON parser callback
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
\param ctx The pointer passed to new_JSON_parser.
|
2009-05-15 13:16:34 +00:00
|
|
|
\param type An element of JSON_type but not JSON_T_NONE.
|
|
|
|
\param value A representation of the parsed value. This parameter is NULL for
|
|
|
|
JSON_T_ARRAY_BEGIN, JSON_T_ARRAY_END, JSON_T_OBJECT_BEGIN, JSON_T_OBJECT_END,
|
|
|
|
JSON_T_NULL, JSON_T_TRUE, and SON_T_FALSE. String values are always returned
|
2008-05-21 14:19:49 +00:00
|
|
|
as zero-terminated C strings.
|
|
|
|
|
|
|
|
\return Non-zero if parsing should continue, else zero.
|
2009-05-15 13:16:34 +00:00
|
|
|
*/
|
|
|
|
typedef int (*JSON_parser_callback)(void* ctx, int type, const struct JSON_value_struct* value);
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
|
2009-05-15 13:16:34 +00:00
|
|
|
/*! \brief The structure used to configure a JSON parser object
|
|
|
|
|
|
|
|
\param depth If negative, the parser can parse arbitrary levels of JSON, otherwise
|
2008-05-21 14:19:49 +00:00
|
|
|
the depth is the limit
|
2009-05-15 13:16:34 +00:00
|
|
|
\param Pointer to a callback. This parameter may be NULL. In this case the input is merely checked for validity.
|
2008-05-21 14:19:49 +00:00
|
|
|
\param Callback context. This parameter may be NULL.
|
2009-05-15 13:16:34 +00:00
|
|
|
\param depth. Specifies the levels of nested JSON to allow. Negative numbers yield unlimited nesting.
|
2008-05-21 14:19:49 +00:00
|
|
|
\param allowComments. To allow C style comments in JSON, set to non-zero.
|
2009-05-15 13:16:34 +00:00
|
|
|
\param handleFloatsManually. To decode floating point numbers manually set this parameter to non-zero.
|
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
\return The parser object.
|
2009-05-15 13:16:34 +00:00
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
JSON_parser_callback callback;
|
|
|
|
void* callback_ctx;
|
|
|
|
int depth;
|
|
|
|
int allow_comments;
|
|
|
|
int handle_floats_manually;
|
2008-05-21 14:19:49 +00:00
|
|
|
} JSON_config;
|
|
|
|
|
|
|
|
|
2009-05-15 13:16:34 +00:00
|
|
|
/*! \brief Initializes the JSON parser configuration structure to default values.
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
The default configuration is
|
2009-05-15 13:16:34 +00:00
|
|
|
- 127 levels of nested JSON (depends on JSON_PARSER_STACK_SIZE, see json_parser.c)
|
2008-05-21 14:19:49 +00:00
|
|
|
- no parsing, just checking for JSON syntax
|
|
|
|
- no comments
|
|
|
|
|
|
|
|
\param config. Used to configure the parser.
|
2009-05-15 13:16:34 +00:00
|
|
|
*/
|
|
|
|
JSON_PARSER_DLL_API void init_JSON_config(JSON_config* config);
|
2008-05-21 14:19:49 +00:00
|
|
|
|
2009-05-15 13:16:34 +00:00
|
|
|
/*! \brief Create a JSON parser object
|
|
|
|
|
|
|
|
\param config. Used to configure the parser. Set to NULL to use the default configuration.
|
2008-05-21 14:19:49 +00:00
|
|
|
See init_JSON_config
|
2009-05-15 13:16:34 +00:00
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
\return The parser object.
|
2009-05-15 13:16:34 +00:00
|
|
|
*/
|
|
|
|
JSON_PARSER_DLL_API extern JSON_parser new_JSON_parser(JSON_config* config);
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
/*! \brief Destroy a previously created JSON parser object. */
|
2009-05-15 13:16:34 +00:00
|
|
|
JSON_PARSER_DLL_API extern void delete_JSON_parser(JSON_parser jc);
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
/*! \brief Parse a character.
|
|
|
|
|
2009-05-15 13:16:34 +00:00
|
|
|
\return Non-zero, if all characters passed to this function are part of are valid JSON.
|
|
|
|
*/
|
|
|
|
JSON_PARSER_DLL_API extern int JSON_parser_char(JSON_parser jc, int next_char);
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
/*! \brief Finalize parsing.
|
|
|
|
|
|
|
|
Call this method once after all input characters have been consumed.
|
2009-05-15 13:16:34 +00:00
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
\return Non-zero, if all parsed characters are valid JSON, zero otherwise.
|
2009-05-15 13:16:34 +00:00
|
|
|
*/
|
|
|
|
JSON_PARSER_DLL_API extern int JSON_parser_done(JSON_parser jc);
|
2008-05-21 14:19:49 +00:00
|
|
|
|
2009-05-15 13:16:34 +00:00
|
|
|
/*! \brief Determine if a given string is valid JSON white space
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
\return Non-zero if the string is valid, zero otherwise.
|
2009-05-15 13:16:34 +00:00
|
|
|
*/
|
|
|
|
JSON_PARSER_DLL_API extern int JSON_parser_is_legal_white_space_string(const char* s);
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2009-05-15 13:16:34 +00:00
|
|
|
#endif
|
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
#endif /* JSON_PARSER_H */
|