Skip to content
/ mjsonrpc Public

A lightweight server program based on cJSON for JSON-RPC 2.0 | 一个轻量化的基于 cJSON 的 JSON-RPC 2.0 的服务端实现

License

Notifications You must be signed in to change notification settings

sfxfs/mjsonrpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mjsonrpc - A Minimal JSON-RPC 2.0 Server Middleware Based on cJSON

English | 中文

Introduction

This middleware is suitable for Linux systems and can be integrated with various communication methods (TCP, UDP, serial). It is easy to use (with only a few functional functions) and supports batch calls (JSON Array) as well as custom error messages.

Functions

  1. Construct a Normal Response Object

    cJSON *mjrpc_response_ok(cJSON *result, cJSON *id);
  2. Construct an Error Response Object

    cJSON *mjrpc_response_error(int code, char *message, cJSON *id);
  3. Add a Method

    int mjrpc_add_method(mjrpc_handle_t *handle,
                         mjrpc_func function_pointer,
                         char *method_name, void *arg2func);
  4. Delete a Method

    int mjrpc_del_method(mjrpc_handle_t *handle, char *method_name);
  5. Process Request String

    char *mjrpc_process_str(mjrpc_handle_t *handle,
                            const char *request_str,
                            int *ret_code);
  6. Process Request cJSON Structure

    cJSON *mjrpc_process_cjson(mjrpc_handle_t *handle,
                               cJSON *request_cjson,
                               int *ret_code);

Example

#include <stdio.h>
#include <stdlib.h>
#include "mjsonrpc.h"

// Define a simple JSON-RPC method
cJSON *hello_world(mjrpc_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 = {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}";
    char *json_response = NULL;

    // Process the request
    int result;
    json_response = mjrpc_process_str(&handle, json_request, &result);

    if (result != MJRPC_RET_OK)
    {
        printf("Error processing request: %d\n", result);
    }

    if (json_response)
    {
        printf("Response: %s\n", json_response);
        free(json_response);
    }

    // Cleanup
    mjrpc_del_method(&handle, "hello");

    return 0;
}

References

JSON-RPC Official Docs

jsonrpc-c

About

A lightweight server program based on cJSON for JSON-RPC 2.0 | 一个轻量化的基于 cJSON 的 JSON-RPC 2.0 的服务端实现

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published