https://github.com/sfxfs/mjsonrpc
A JSON-RPC 2.0 Message Parser and Generator Based in ANSI C | 基于 C 语言的 JSON-RPC 2.0 的消息解析器和生成器
https://github.com/sfxfs/mjsonrpc
c json jsonrpc2 rpc
Last synced: about 1 month ago
JSON representation
A JSON-RPC 2.0 Message Parser and Generator Based in ANSI C | 基于 C 语言的 JSON-RPC 2.0 的消息解析器和生成器
- Host: GitHub
- URL: https://github.com/sfxfs/mjsonrpc
- Owner: sfxfs
- License: mit
- Created: 2024-10-07T13:41:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-02-17T10:27:48.000Z (about 1 month ago)
- Last Synced: 2026-02-17T14:34:03.953Z (about 1 month ago)
- Topics: c, json, jsonrpc2, rpc
- Language: C
- Homepage: https://sfxfs.github.io/mjsonrpc/
- Size: 320 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mjsonrpc - A JSON-RPC 2.0 Message Parser and Generator Based on cJSON
     
[ English | [中文](README_CN.md) ]
### 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, notification requests, and more.
### Features
- **Lightweight & Minimal Dependencies**: Only depends on cJSON
- **Hash-based Method Indexing**: Fast method lookup
- **Batch Requests**: Support for JSON Array batch calls
- **Customizable Memory Management**: User-defined malloc/free/strdup hooks
- **Thread-Safe**: Thread-local storage for memory hooks
- **POSIX Array Params**: Support for both object and array parameters
- **Method Enumeration**: Query registered methods at runtime
### 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](https://sfxfs.github.io/mjsonrpc).
### Example
```c
#include "mjsonrpc.h"
#include
#include
// Define a simple JSON-RPC method
cJSON *hello_world(mjrpc_func_ctx_t *context, cJSON *params, cJSON *id) {
cJSON *result = cJSON_CreateString("Hello, World!");
return result;
}
int main() {
// Initialize mjrpc_handle_t
mjrpc_handle_t* handle = mjrpc_create_handle(0);
// Add a method
mjrpc_add_method(handle, hello_world, "hello", NULL);
// Construct a JSON-RPC request
const char *json_request = "{\"jsonrpc\":\"2.0\",\"method\":\"hello\",\"id\":1}";
// Process the request
int result;
char *json_response = mjrpc_process_str(handle, json_request, &result);
// Check return code
if (result != MJRPC_RET_OK) {
printf("Error processing request: %d\n", result);
}
// Check response string
if (json_response) {
printf("Response: %s\n", json_response);
free(json_response);
}
// Cleanup
mjrpc_destroy_handle(handle);
return 0;
}
```
### References
- [DaveGamble/cJSON](https://github.com/DaveGamble/cJSON)
- [JSON-RPC Specification](https://www.jsonrpc.org/specification)
- [hmng/jsonrpc-c](https://github.com/hmng/jsonrpc-c)