mco_lJSON_ctx_close

Prototype

    MCO_RET mco_lJSON_ctx_close(lews_JSON_context_h json_ctx);

Arguments

json_ctx
Pointer to an initialized lews_JSON_context_t structure.

Description

This function closes the root JSON object by appending a closing brace (}) to the output buffer.

It does not automatically flush or send the data to the client. However, if there is insufficient space in the output buffer to write the closing brace, the function will first flush the current buffer contents to the network, clear the buffer, and then write the } character.

Before writing the closing brace, the function verifies that all nested JSON objects and arrays have been properly closed. If the internal structure counter is not zero, it returns an error indicating unmatched brackets or braces.

After calling this function, the application should explicitly call mco_lews_flush() to ensure all remaining data is sent to the client.

    lews_JSON_context_t json_ctx;
    ...
    // Respond with JSON
    ret = mco_lews_append_http_header(p_ctx, 200, "application/json");
    if (ret != MCO_S_OK) {
        printf("mco_lews_append_http_header error %d\n", ret);
        return;
    }
    ret = mco_lews_append_header_end(p_ctx);
    if (ret != MCO_S_OK) {
        printf("mco_lews_append_header_end error %d\n", ret);
        return;
    }
    ret = mco_lJSON_ctx_open(&json_ctx, p_ctx);
    if (ret != MCO_S_OK) {
        printf("mco_lJSON_ctx_open error %d\n", ret);
        return;
    }
    ...
    // Add JSON data
    ...
    mco_lJSON_ctx_close(&json_ctx);
    ret = mco_lews_flush(p_ctx);
    if (ret < 0) {
        printf("mco_lews_flush error %d\n", ret);
        return;
    }

Return Codes

MCO_S_OK
Root JSON object successfully closed.
MCO_E_NW_SENDERR
An error occurred while flushing the output buffer due to insufficient space for the closing brace.
MCO_E_LEWS_CLOSING_BRACKET_MISSING
Not all JSON objects or arrays were properly closed before calling this function (unbalanced structure).
Other error codes
May indicate internal errors.

Files

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