
[ English | 中文 ]
Introduction
This project is lightweight, has minimal dependencies, and can be integrated into various communication methods (TCP, UDP, message queues, etc.). It is simple to use (with only a few functional APIs) and has good performance (using hash-based indexing instead of polling all methods). It also supports batch calls (JSON Array), automatic generation of corresponding error messages or custom error messages based on requests, customizable memory management hooks, and notification requests.
How to Use
Simply add the project source files (mjsonrpc.c, mjsonrpc.h) and the cJSON library to your own project and compile them together. Alternatively, you can compile them into a dynamic library and link it.
Function Definitions
For detailed API descriptions, please refer to src/mjsonrpc.h and Doxygen docs of this repo.
Example
#include <stdio.h>
#include <stdlib.h>
cJSON *result = cJSON_CreateString("Hello, World!");
return result;
}
int main() {
const char *json_request = "{\"jsonrpc\":\"2.0\",\"method\":\"hello\",\"id\":1}";
int result;
printf("Error processing request: %d\n", result);
}
if (json_response) {
printf("Response: %s\n", json_response);
free(json_response);
}
return 0;
}
int mjrpc_destroy_handle(mjrpc_handle_t *handle)
Destroy a JSON-RPC handle and free all associated memory.
mjrpc_handle_t * mjrpc_create_handle(size_t initial_capacity)
Create a new JSON-RPC handle.
int mjrpc_add_method(mjrpc_handle_t *handle, mjrpc_func function_pointer, const char *method_name, void *arg2func)
Register a new RPC method.
char * mjrpc_process_str(mjrpc_handle_t *handle, const char *request_str, int *ret_code)
Process a JSON-RPC request string.
A lightweight JSON-RPC 2.0 message parser and generator based on cJSON.
@ MJRPC_RET_OK
Operation completed successfully.
Context structure passed to RPC method callback functions.
Main handle structure for managing RPC methods.
References