mcorest_api_json_read_string

Read a quoted string from the input stream.

For an overview see page mcorest_api_json_read_*

Prototype

 
    MCO_RET mcorest_api_json_read_string( mcorest_api_json_read_ctx_t *ctx, 
                            int as_key, 
                            int cont,
                            char *buf,
                            int *len);
 

Arguments

ctx Pointer to the JSON read context structure
as_key Set to non-zero to treat the string value as a JSON key
cont Set to non-zero in subsequent calls to function mcorest_api_json_read_string() to retrieve consecutive fragments of the string
buf The buffer to read data into (this buffer is never zero-terminated)
len On input, defines the size of the buffer. On output, returns the actual number of characters written into the buffer

Description

This function reads a quoted string from the input stream. It is possible that the JSON string does not fit into the provided buffer. In this case, the output value of len will be the same as the input value. The following fragment(s) of the string can be read with subsequent calls to this function with the cont flag set to a non-zero value. If the JSON string fits into the buffer, the output len value will always be smaller than the input value. No subsequent calls to this function should be made in this case. The buffer is never zero-terminated. Both keys and string values in JSON have the same format. The JSON reader does not track the JSON context, and thus cannot differentiate keys from string values. The application should set the as_key flag to a non-zero value if it expects the string to be a key. An error will be returned if this flag is not set but the string turns out to be a key (i.e. is followed by ':'), or vice versa

Return Codes

MCO_S_OK = 0 Success
MCO_E_REST_* Any of the Web Services Error Codes

Example

 
    static void read_object(void *ctx, mcohs_request_h req);
    {
        MCOREST_API_JSON_TKN_TYPE tkn = MCOREST_API_JSON_TKN_UNKNOWN;
        mcorest_api_json_read_ctx_t jctx;
        mcorest_api_stream_t jstream;
        char key[32];
        int len = sizeof(key);
        long l;
         
        mcorest_api_stream_with_request(req, &jstream);
        mcorest_api_json_read_start(&jstream, &jctx);
         
        // Start reading the top-level JSON object
        mcorest_api_json_next_token(&jctx, MCOREST_API_JSON_TKN_OBJECT_BGN, NULL);
        mcorest_api_json_read_object_begin(&jctx);
 
        // Read first key in the top-level object
        mcorest_api_json_next_token(&jctx, MCOREST_API_JSON_TKN_UNKNOWN, &tkn);
        while (tkn == MCOREST_API_JSON_TKN_STRING) 
        {
            // As long as we're getting strings from the object, we interpret them as keys,
            // and then read the values
            mcorest_api_json_read_string(&jctx, 1, 0, key, &len);
 
            // Read the value type
            mcorest_api_json_next_token(&jctx, MCOREST_API_JSON_TKN_UNKNOWN, &tkn);
            if ((0 == strcmp(key, "max_records") && (tkn == MCOREST_API_JSON_TKN_NUMBER)) 
            {
                mcorest_api_json_read_long(&jctx, &l);
            }
            ...
            mcorest_api_json_next_token(&jctx, MCOREST_API_JSON_TKN_UNKNOWN, &tkn);
        }
        mcorest_api_json_read_object_end(jctx);
        ...
    }
     

Files

Header file:
mcorestapi.h
Source file:
jsonrd.c
Library:
libmcorest.a