Annotation of parser3/src/lib/json/JSON_parser.h, revision 1.2

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

E-mail: