mco_lJSON_obj_open_arr

Prototype

    MCO_RET mco_lJSON_obj_open_arr(lews_JSON_context_h json_ctx, const char *key);

Arguments

json_ctx
Pointer to an initialized lews_JSON_context_t structure.
key
Null-terminated string specifying the name of the array to be added to the current JSON object.

Description

This function adds a new JSON array as a member of the current JSON object, using the specified key as its name. After this call, all subsequent JSON construction operations (e.g., mco_lJSON_arr_add_str, mco_lJSON_arr_add_int) will operate on this newly opened array until it is closed with mco_lJSON_obj_close().

The function may trigger a buffer flush and send data over the network if there is insufficient space in the output buffer to write the key and opening bracket ("key":[).

This function can only be called when the current context is inside a JSON object. Attempting to call it at the root level or inside an array will result in an error.

    ...
    ret = mco_lJSON_obj_open_arr(&json_ctx, "hobbies");
    if (ret == MCO_S_OK) {
        ret = mco_lJSON_arr_add_str(&json_ctx, "hockey");
        if (ret != MCO_S_OK) {
            printf("mco_lJSON_arr_add_str error %d\n", ret);
        }
        ret = mco_lJSON_arr_add_str(&json_ctx, "football");
        if (ret != MCO_S_OK) {
            printf("mco_lJSON_arr_add_str error %d\n", ret);
        }
        ret = mco_lJSON_arr_add_str(&json_ctx, "basketball");
        if (ret != MCO_S_OK) {
            printf("mco_lJSON_arr_add_str error %d\n", ret);
        }
        ret = mco_lJSON_obj_close(&json_ctx);
        if (ret != MCO_S_OK) {
            printf("mco_lJSON_obj_close error %d\n", ret);
        }
    }
    ...

Return Codes

MCO_S_OK
Array successfully opened and added to the current JSON object.
MCO_E_NW_SENDERR
An error occurred while flushing the output buffer due to insufficient space for the key and opening bracket.
MCO_E_LEWS_JSON_STACK_OVERFLOW
The internal JSON nesting stack has reached its maximum depth.
MCO_E_LEWS_PARENT_IS_NOT_OBJECT
The current parent context is not a JSON object (e.g., you are at the root level or inside an array).
Other error codes
May indicate internal errors (e.g., invalid arguments, null pointers).

Files

Header file:
mcoewslite.h
Source file:
mcoewslite.c
Library:
mcoewslite.a