Retrieve the type of the next token in the JSON stream.
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);
ctx
expected_type
MCOREST_API_JSON_TKN_TYPE value of the next token.type
MCOREST_API_JSON_TKN_TYPE value of the next token.This function retrieves the type of the next token in the JSON stream. If
expected_typeis notMCOREST_API_JSON_TKN_UNKNOWN, and it does not match the next token in the stream, error codeMCO_E_REST_JSONis returned, and the actual type of the token is returned through thetypepointer. Depending on the returned tokentype, one of the following read functions should be called to read the data:
MCOREST_API_JSON_TKN_OBJECT_BGN: mcorest_api_json_read_object_begin()MCOREST_API_JSON_TKN_OBJECT_END: mcorest_api_json_read_object_end()MCOREST_API_JSON_TKN_ARRAY_BGN: mcorest_api_json_read_array_begin()MCOREST_API_JSON_TKN_ARRAY_END: mcorest_api_json_read_array_end()MCOREST_API_JSON_TKN_STRING: mcorest_api_json_read_string()MCOREST_API_JSON_TKN_NUMBER: mcorest_api_json_read_[numeric/long/i64/double]()If the returned
typeindicates one of the literals, no additionalmcorest_api_json_read_*()calls are needed; the caller should proceed to read the next token:
MCOREST_API_JSON_TKN_TRUE,MCOREST_API_JSON_TKN_FALSEorMCOREST_API_JSON_TKN_NULL.
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);
...
}