mjsonrpc 1.0
A lightweight JSON-RPC 2.0 message parser and generator based on cJSON
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Enumerations | Functions
mjsonrpc.h File Reference

A lightweight JSON-RPC 2.0 message parser and generator based on cJSON. More...

#include "cJSON.h"
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  mjrpc_func_ctx_t
 Context structure passed to RPC method callback functions. More...
 
struct  mjrpc_method
 Internal structure representing a registered RPC method. More...
 
struct  mjrpc_handle
 Main handle structure for managing RPC methods. More...
 

Macros

#define JSON_RPC_CODE_PARSE_ERROR   (-32700)
 Parse error - Invalid JSON was received by the server.
 
#define JSON_RPC_CODE_INVALID_REQUEST   (-32600)
 Invalid Request - The JSON sent is not a valid Request object.
 
#define JSON_RPC_CODE_METHOD_NOT_FOUND   (-32601)
 Method not found - The method does not exist / is not available.
 
#define JSON_RPC_CODE_INVALID_PARAMS   (-32603)
 Invalid params - Invalid method parameter(s)
 
#define JSON_RPC_CODE_INTERNAL_ERROR   (-32693)
 Internal error - Internal JSON-RPC error.
 

Typedefs

typedef cJSON *(* mjrpc_func) (mjrpc_func_ctx_t *context, cJSON *params, cJSON *id)
 Function pointer type for RPC method implementations.
 
typedef struct mjrpc_handle mjrpc_handle_t
 Typedef for struct mjrpc_handle for convenience.
 
typedef void *(* mjrpc_malloc_func) (size_t size)
 Function pointer type for custom memory allocation.
 
typedef void(* mjrpc_free_func) (void *ptr)
 Function pointer type for custom memory deallocation.
 
typedef char *(* mjrpc_strdup_func) (const char *str)
 Function pointer type for custom string duplication.
 

Enumerations

enum  mjrpc_error_return {
  MJRPC_RET_OK , MJRPC_RET_OK_NOTIFICATION , MJRPC_RET_ERROR_MEM_ALLOC_FAILED , MJRPC_RET_ERROR_NOT_FOUND ,
  MJRPC_RET_ERROR_EMPTY_REQUEST , MJRPC_RET_ERROR_NOT_OBJ_ARY , MJRPC_RET_ERROR_PARSE_FAILED , MJRPC_RET_ERROR_HANDLE_NOT_INITIALIZED ,
  MJRPC_RET_ERROR_INVALID_PARAM
}
 Reserved for implementation-defined server-errors (-32000 to -32099) More...
 

Functions

int mjrpc_set_memory_hooks (mjrpc_malloc_func malloc_func, mjrpc_free_func free_func, mjrpc_strdup_func strdup_func)
 Set custom memory management functions.
 
char * mjrpc_request_str (const char *method, cJSON *params, cJSON *id)
 Build a JSON-RPC request as a string.
 
cJSON * mjrpc_request_cjson (const char *method, cJSON *params, cJSON *id)
 Build a JSON-RPC request as a cJSON object.
 
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.
 
mjrpc_handle_tmjrpc_create_handle (size_t initial_capacity)
 Create a new JSON-RPC handle.
 
int mjrpc_destroy_handle (mjrpc_handle_t *handle)
 Destroy a JSON-RPC handle and free all associated memory.
 
int mjrpc_add_method (mjrpc_handle_t *handle, mjrpc_func function_pointer, const char *method_name, void *arg2func)
 Register a new RPC method.
 
int mjrpc_del_method (mjrpc_handle_t *handle, const char *method_name)
 Unregister an RPC method.
 
char * mjrpc_process_str (mjrpc_handle_t *handle, const char *request_str, int *ret_code)
 Process a JSON-RPC request string.
 
cJSON * mjrpc_process_cjson (mjrpc_handle_t *handle, const cJSON *request_cjson, int *ret_code)
 Process a JSON-RPC request cJSON object.
 

Detailed Description

A lightweight JSON-RPC 2.0 message parser and generator based on cJSON.

Author
Xiao
Date
2024
Version
1.0

This library provides a complete implementation of JSON-RPC 2.0 specification with minimal dependencies. It supports:

The library is designed to be integrated into various communication methods (TCP, UDP, message queues, etc.) and provides a simple API for easy usage.

Copyright (c) 2024 Xiao

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Definition in file mjsonrpc.h.

Typedef Documentation

◆ mjrpc_func

mjrpc_func

Function pointer type for RPC method implementations.

Parameters
contextContext structure containing user data and error handling
paramsJSON parameters passed to the method (can be NULL)
idRequest ID for responses (can be NULL for notifications)
Returns
cJSON* Result to be returned to client (will be freed automatically)
Note
If error_code is set in context, the error_message will be used instead of the returned result

Definition at line 145 of file mjsonrpc.h.

Enumeration Type Documentation

◆ mjrpc_error_return

Reserved for implementation-defined server-errors (-32000 to -32099)

Return codes for mjsonrpc library functions

Enumerator
MJRPC_RET_OK 

Operation completed successfully.

MJRPC_RET_OK_NOTIFICATION 

Operation completed successfully (notification request)

MJRPC_RET_ERROR_MEM_ALLOC_FAILED 

Memory allocation failed.

MJRPC_RET_ERROR_NOT_FOUND 

Requested method not found.

MJRPC_RET_ERROR_EMPTY_REQUEST 

Empty request received.

MJRPC_RET_ERROR_NOT_OBJ_ARY 

Request is not a JSON object or array.

MJRPC_RET_ERROR_PARSE_FAILED 

JSON parsing failed.

MJRPC_RET_ERROR_HANDLE_NOT_INITIALIZED 

Handle not initialized.

MJRPC_RET_ERROR_INVALID_PARAM 

Invalid parameter provided.

Definition at line 84 of file mjsonrpc.h.