mco_lJSON_obj_close

Prototype

    MCO_RET mco_lJSON_obj_close(lews_JSON_context_h json_ctx);

Arguments

json_ctx
Pointer to an initialized lews_JSON_context_t structure.

Description

This function closes the current JSON object or array by appending the appropriate closing character (} for objects, ] for arrays) and returns the context to the parent level.

After this call, all subsequent JSON construction operations will apply to the parent container (object or array).

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 closing character.

This function should only be called after a corresponding open operation (mco_lJSON_obj_open, mco_lJSON_obj_open_arr, or mco_lJSON_arr_open). Attempting to close when no container is active results 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
Current JSON object or array successfully closed.
MCO_E_NW_SENDERR
An error occurred while flushing the output buffer due to insufficient space for the closing character.
MCO_E_LEWS_JSON_UNNECESSARY_CLOSE
Attempted to close a container when no object or array was currently open (e.g., extra or unmatched close call).
Other error codes
May indicate internal errors (e.g., invalid context pointer).

Files

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