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 */
|
2008-09-05 14:31:46 +00:00
|
|
|
/*
|
2008-09-23 19:11:04 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# 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
|
2008-09-05 14:31:46 +00:00
|
|
|
/*
|
2008-09-23 19:11:04 +00:00
|
|
|
#endif
|
|
|
|
*/
|
2008-05-21 14:19:49 +00:00
|
|
|
|
2008-06-16 04:03:06 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
typedef int64_t JSON_int_t;
|
2008-09-23 19:11:04 +00:00
|
|
|
#define JSON_PARSER_INTEGER_SSCANF_TOKEN "%" PRId64
|
|
|
|
#define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%" PRId64
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
2008-09-23 19:11:04 +00:00
|
|
|
#endif
|
2008-05-21 14:19:49 +00:00
|
|
|
|
2008-09-23 19:11:04 +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;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
typedef struct JSON_value_struct
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
JSON_int_t integer_value;
|
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
long double float_value;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
2008-05-29 03:24:26 +00:00
|
|
|
const char* value;
|
2008-09-23 19:11:04 +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;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
/*! \brief JSON parser callback
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
\param ctx The pointer passed to new_JSON_parser.
|
2008-09-23 19:11:04 +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.
|
2008-09-23 19:11:04 +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
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
/*! \brief The structure used to configure a JSON parser object
|
2008-05-21 14:19:49 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
\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
|
2008-09-23 19:11:04 +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.
|
2008-09-23 19:11:04 +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.
|
2008-09-23 19:11:04 +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.
|
2008-09-23 19:11:04 +00:00
|
|
|
*/
|
|
|
|
typedef struct JSON_config_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;
|
|
|
|
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
/*! \brief Initializes the JSON parser configuration structure to default
|
|
|
|
values.
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
The default configuration is
|
2008-09-23 19:11:04 +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.
|
2008-09-23 19:11:04 +00:00
|
|
|
*/
|
|
|
|
JSON_PARSER_DLL_API void init_JSON_config(
|
|
|
|
JSON_config* config );
|
|
|
|
|
|
|
|
/*! \brief Create a JSON parser object
|
2008-05-21 14:19:49 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
\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
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
\return The parser object.
|
2008-09-23 19:11:04 +00:00
|
|
|
*/
|
|
|
|
JSON_PARSER_DLL_API extern struct JSON_parser_struct* new_JSON_parser(
|
|
|
|
JSON_config* config );
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
/*! \brief Destroy a previously created JSON parser object. */
|
2008-09-23 19:11:04 +00:00
|
|
|
JSON_PARSER_DLL_API extern void delete_JSON_parser(
|
|
|
|
struct JSON_parser_struct* jc );
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
/*! \brief Parse a character.
|
|
|
|
|
2008-09-23 19:11:04 +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(
|
|
|
|
struct JSON_parser_struct* 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.
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
\return Non-zero, if all parsed characters are valid JSON, zero otherwise.
|
2008-09-23 19:11:04 +00:00
|
|
|
*/
|
|
|
|
JSON_PARSER_DLL_API extern int JSON_parser_done(
|
|
|
|
struct JSON_parser_struct* jc );
|
2008-05-21 14:19:49 +00:00
|
|
|
|
2008-09-23 19:11:04 +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.
|
2008-09-23 19:11:04 +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
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
#endif
|
|
|
|
|
2008-05-21 14:19:49 +00:00
|
|
|
|
|
|
|
#endif /* JSON_PARSER_H */
|