mcorest_api_json_read_next_token

Retrieve the type of the next token in the JSON stream.

For an overview see page mcorest_api_json_read_*

Prototype

    MCO_RET mcorest_api_json_read_next_token( /*IN*/ mcorest_api_json_read_ctx_t *ctx,
                            /*IN*/ MCOREST_API_JSON_TKN_TYPE expected_type,
                            /*OUT*/ MCOREST_API_JSON_TKN_TYPE *type);

Arguments

ctx
Pointer to the JSON read context structure.
expected_type
The expected MCOREST_API_JSON_TKN_TYPE value of the next token.
type
The actual MCOREST_API_JSON_TKN_TYPE value of the next token.

Description

This function retrieves the type of the next token in the JSON stream. If expected_type is not MCOREST_API_JSON_TKN_UNKNOWN, and it does not match the next token in the stream, error code MCO_E_REST_JSON is returned, and the actual type of the token is returned through the type pointer. Depending on the returned token type, one of the following read functions should be called to read the data:

If the returned type indicates one of the literals, no additional mcorest_api_json_read_*() calls are needed; the caller should proceed to read the next token:

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);
        ...
    }