Skip to content

Commit 496e3da

Browse files
committed
Add new feat. to support construct a json-rpc request
1 parent 7cc2c17 commit 496e3da

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

example/invoke_a_func/invoke_a_func.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ int main()
1919
// Add a method
2020
mjrpc_add_method(handle, hello_world, "hello", NULL);
2121

22-
// Construct a JSON-RPC request
23-
const char* json_request = "{\"jsonrpc\":\"2.0\",\"method\":\"hello\",\"id\":1}";
22+
// Construct a JSON-RPC request: {"jsonrpc":"2.0","method":"hello","id":1}
23+
char* json_request = mjrpc_request_str("hello", NULL, cJSON_CreateNumber(1));
2424
char* json_response = NULL;
2525

2626
// Process the request
@@ -33,7 +33,9 @@ int main()
3333
assert(json_response != NULL);
3434

3535
// show the response
36+
printf("Request: %s\n", json_request);
3637
printf("Response: %s\n", json_response);
38+
free(json_request);
3739
free(json_response);
3840

3941
// Cleanup

src/mjsonrpc.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,35 @@ static cJSON* rpc_handle_ary_req(const mjrpc_handle_t* handle, const cJSON* requ
187187

188188
/*--- main functions ----*/
189189

190+
cJSON* mjrpc_request_cjson(const char* method, cJSON* params, cJSON* id)
191+
{
192+
if (method == NULL)
193+
return NULL;
194+
195+
cJSON* json = cJSON_CreateObject();
196+
if (json == NULL)
197+
return NULL;
198+
199+
cJSON_AddStringToObject(json, "jsonrpc", "2.0");
200+
cJSON_AddStringToObject(json, "method", method);
201+
cJSON_AddItemToObject(json, "params", params);
202+
cJSON_AddItemToObject(json, "id", id);
203+
204+
return json;
205+
}
206+
207+
char* mjrpc_request_str(const char* method, cJSON* params, cJSON* id)
208+
{
209+
cJSON* json = mjrpc_request_cjson(method, params, id);
210+
if (json == NULL)
211+
return NULL;
212+
213+
const char* json_str = cJSON_PrintUnformatted(json);
214+
cJSON_Delete(json);
215+
216+
return json_str;
217+
}
218+
190219
cJSON* mjrpc_response_ok(cJSON* result, cJSON* id)
191220
{
192221
if (id == NULL || result == NULL)

src/mjsonrpc.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ typedef struct mjrpc_handle
7676
size_t size;
7777
} mjrpc_handle_t;
7878

79+
80+
/* --- For client --- */
81+
82+
/**
83+
* @brief build a jsonrpc string request
84+
* @param method name of method
85+
* @param params parameters for the method
86+
* @param id request id, can be NULL for notification
87+
* @return string pointer
88+
*/
89+
char* mjrpc_request_str(const char* method, cJSON* params, cJSON* id);
90+
91+
/**
92+
* @brief build a jsonrpc cjson request
93+
* @param method name of method
94+
* @param params parameters for the method
95+
* @param id request id, can be NULL for notification
96+
* @return cjson pointer
97+
*/
98+
cJSON* mjrpc_request_cjson(const char* method, cJSON* params, cJSON* id);
99+
100+
101+
/* --- For server --- */
102+
79103
/**
80104
* @brief build a jsonrpc response with result
81105
* @param result call result

0 commit comments

Comments
 (0)