mjsonrpc 1.0
A lightweight JSON-RPC 2.0 message parser and generator based on cJSON
Loading...
Searching...
No Matches
Functions
Response Helper Functions

Functions for creating JSON-RPC responses. More...

Functions

cJSON * mjrpc_response_ok (cJSON *result, cJSON *id)
 Build a successful JSON-RPC response with result.
 
cJSON * mjrpc_response_error (int code, char *message, cJSON *id)
 Build an error JSON-RPC response.
 

Detailed Description

Functions for creating JSON-RPC responses.

Function Documentation

◆ mjrpc_response_error()

cJSON * mjrpc_response_error ( int  code,
char *  message,
cJSON *  id 
)

Build an error JSON-RPC response.

Creates a JSON-RPC 2.0 error response object with the specified error code, message, and request ID.

Parameters
codeError code (standard JSON-RPC codes or custom codes)
messageError message (will be freed automatically, can be NULL)
idClient request ID (will be owned by response)
Returns
cJSON pointer containing the error response (caller must delete)
Return values
NULLIf an error occurred (message and id will be released)
Note
If message is NULL, a default message will be used
Example:
cJSON *id = cJSON_CreateNumber(1);
char *msg = strdup("Invalid parameters");
if (response) {
// Send error response...
cJSON_Delete(response);
}
#define JSON_RPC_CODE_INVALID_PARAMS
Invalid params - Invalid method parameter(s)
Definition mjsonrpc.h:70
cJSON * mjrpc_response_error(int code, char *message, cJSON *id)
Build an error JSON-RPC response.

◆ mjrpc_response_ok()

cJSON * mjrpc_response_ok ( cJSON *  result,
cJSON *  id 
)

Build a successful JSON-RPC response with result.

Creates a JSON-RPC 2.0 response object containing the specified result and request ID.

Parameters
resultResult data to be returned (will be owned by response)
idClient request ID (will be owned by response)
Returns
cJSON pointer containing the response (caller must delete)
Return values
NULLIf an error occurred (result and id will be released)
Note
Both result and id must not be NULL
Example:
cJSON *result = cJSON_CreateString("Hello, World!");
cJSON *id = cJSON_CreateNumber(1);
cJSON *response = mjrpc_response_ok(result, id);
if (response) {
char *response_str = cJSON_Print(response);
// Send response...
free(response_str);
cJSON_Delete(response);
}
cJSON * mjrpc_response_ok(cJSON *result, cJSON *id)
Build a successful JSON-RPC response with result.