mco_lJSON_ctx_open

Prototype

    MCO_RET mco_lJSON_ctx_open(lews_JSON_context_h json_ctx, lews_connection_context_h ctx);

Arguments

json_ctx
Pointer to a lews_JSON_context_t structure that will be initialized for JSON construction.
ctx
Handle to the active connection context where the JSON data will be written.

Description

This function initializes the provided JSON context and opens a root JSON object ({) in the output buffer of the specified connection.

If the output buffer does not have enough space to write the opening brace, the function automatically flushes (sends) the current buffer contents to the client, clears the buffer, and then writes the { character.

Because this function may trigger a network send operation, it can return a network error if the flush fails.

The JSON context is opaque and must not be modified directly—subsequent JSON content should be added using dedicated JSON construction functions (e.g., mco_lJSON_obj_add_str, mco_lJSON_obj_add_int, etc.).

After building the JSON structure, the context must be closed with mco_lJSON_ctx_close() to finalize the object.

    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
JSON context successfully initialized and root object opened.
MCO_E_NW_SENDERR
An error occurred while flushing the output buffer to the client.
Other error codes
May indicate internal errors

Files

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