MCO_RET mco_lJSON_ctx_open(lews_JSON_context_h json_ctx, lews_connection_context_h ctx);
json_ctx
lews_JSON_context_t structure that will be initialized for JSON construction.ctx
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; }
MCO_S_OK
MCO_E_NW_SENDERR