Web Services API Structures

The eXtremeDB Web Services API uses streams to read and write data from JSON requests. The constants, prototypes, structures and enums used to manage streams are defined in the following sections.

Stream Structures

The stream prototypes and structures used by mcorest_api_json_read_*() and mcorest_api_json_write_*() functions are defined in file include/mcorestapi.h as follows:

     
    typedef int (* mcorest_api_stream_read_fn)(void *ctx, char *buf, int len);
     
    typedef int (* mcorest_api_stream_write_fn)(void *ctx, const char *buf, int len);
 
    typedef struct mcorest_api_stream_t_
    {
        void *ctx;  /* User-defined context that will be passed to read_fn and write_fn. */
        mcorest_api_stream_read_fn read_fn;  /* Stream read function. */
        mcorest_api_stream_write_fn write_fn;  /* Stream write function. */
 
    } mcorest_api_stream_t;
     

JSON Read Context Structure

The JSON context structure used by mcorest_api_json_read_*() functions is defined in file include/mcorestapi.h as follows:

     
    #define MCOREST_API_JSON_READER_BUFFER_SIZE 256
     
    typedef struct mcorest_api_json_read_ctx_t_
    {
        mcorest_api_stream_t *stream;  /* Stream to read JSON data from. */
         
        /* The following fields are for the JSON reader's internal use. */
        int stream_err;  /* Stream error flag. */
        int stream_end;  /* Stream end flag. */
        char buf[MCOREST_API_JSON_READER_BUFFER_SIZE];  /* Stream input buffer. */
        int buf_len;  /* Number of bytes available in the input buffer. */
        const char *buf_p;  /* Current buffer position pointer. */
        int obj_level;  /* Object nesting level. */
        int arr_level;  /* Array nesting level. */
        int read_escaped;  /* Escaped character read flag. */
     
    } mcorest_api_json_read_ctx_t;
     

JSON Token Types

The enum that defines the possible token types returned by function mcorest_api_json_read_next_token() is defined in file include/mcorestapi.h as follows:

     
    typedef enum MCOREST_API_JSON_TKN_TYPE_E_
    {
        MCOREST_API_JSON_TKN_UNKNOWN = 0,  /* Unknown token. */
        MCOREST_API_JSON_TKN_OBJECT_BGN,  /* Object start ('{'). */
        MCOREST_API_JSON_TKN_OBJECT_END,  /* Object end ('}'). */
        MCOREST_API_JSON_TKN_ARRAY_BGN,  /* Array start ('['). */
        MCOREST_API_JSON_TKN_ARRAY_END,  /* Array end (']'). */
        MCOREST_API_JSON_TKN_STRING,  /* Quoted string. */
        MCOREST_API_JSON_TKN_NUMBER,  /* Number (integer or real). */
        MCOREST_API_JSON_TKN_TRUE,  /* "true" literal. */
        MCOREST_API_JSON_TKN_FALSE,  /* "false" literal. */
        MCOREST_API_JSON_TKN_NULL  /* "null" literal. */
 
    } MCOREST_API_JSON_TKN_TYPE;
     

JSON Write Context Structure

The JSON context structure used by mcorest_api_json_write_*() functions is defined in file include/mcorestapi.h as follows:

 
    typedef struct mcorest_api_json_write_ctx_t_
    {
        mcorest_api_stream_t *stream;  /* Stream to write JSON data into. */
        int indent;  /* Indentation level for pretty-printing. */
         
        /* The following fields are for the JSON encoder's internal use. */
        int first_entry;  /* Non-zero if the next key is the first in the object/array. */
        int key_allowed;  /* Non-zero if a key is allowed to be written. */
        int key_was_put;  /* Non-zero if a key has just been written. */
        int obj_level;  /* Object nesting level. */
        int arr_level;  /* Array nesting level. */
     
    } mcorest_api_json_write_ctx_t;