Skip to content

Usage of libjsonrpc

iantropov edited this page Jan 3, 2013 · 5 revisions

LibJSONRPC - it is the library that provide for applications additional interface - web-interface via JSON-RPC protocol.

Its interface consists of three main parts :

  • JSON functions
  • JSON-RPC functions
  • JSON-RPC transport functions

1. JSON functions

For dealing with JSON data uses struct json_object

struct json_object *json_int_new(int i);

struct json_object *json_double_new(double d);

struct json_object *json_boolean_new(boolean b);

struct json_object *json_string_new(char *s);

struct json_object *json_string_new_len(char *s, size_t len);

struct json_object *json_null_new();

This group of functions creates JSON data from C native data types

int json_int_get(struct json_object *obj);

double json_double_get(struct json_object *obj);

boolean json_boolean_get(struct json_object *obj);

char *json_string_get(struct json_object *obj);

This group of functions retrieves C native data values from its JSON representation

struct json_object *json_array_new();

int json_array_length(struct json_object *obj);

struct json_object *json_array_get(struct json_object *obj, int i);

int json_array_add(struct json_object *obj, struct json_object *val);

`void json_array_del(struct json_object *obj, int ind);

This group of functions deals with JSON arrays

struct json_object *json_object_new();

struct json_object *json_object_get(struct json_object *obj, char *key);

void json_object_del(struct json_object *obj, char *key);

int json_object_add(struct json_object *obj, char *key, struct json_object *val);

This group of functions deals with JSON objects

char *json_to_string(struct json_object *obj);

enum json_type json_type(struct json_object *obj);

int json_equals(struct json_object *obj_1, struct json_object *obj_2);

This group consists of some helper functions.

###2. JSON-RPC functions

For dealing with JSON-RPC uses two structures

struct json_rpc; - for save information about all callbacks, incoming requests and so on.

struct json_rpc_request; - for representation incoming JSON-RPC request

typedef void (*json_rpc_method)(struct json_rpc_request *req, struct json_object *obj, void *arg); - it is a type of JSON-RPC registered methods (callbacks in implementation)

typedef void (*json_rpc_result)(struct json_rpc *jr, struct json_object *obj, void *arg); - callbacks of this types invoked after processing JSON-RPC response

struct json_rpc *json_rpc_new();

void json_rpc_free(struct json_rpc *jr);

these function deals with main json_rpc structure

int json_rpc_add_method(struct json_rpc *jr, char *name, json_rpc_method method, void *arg);

void json_rpc_del_method(struct json_rpc *jr, char *name);

adding/removing registered callbacks that invoked by name from JSON-RPC requests

void json_rpc_return(struct json_rpc_request *jr, struct json_object *res); - this is only true way of return from registered callbacks

void json_rpc_process_request(struct json_rpc *jr, struct json_object *obj, json_rpc_result user_cb, void *cb_arg); - process request and call user_cb with cb_arg after completion processing request

int json_rpc_preprocess_request(struct json_rpc *jr, struct json_object *req, json_rpc_result res_cb, void *cb_arg); - this method allows to bind users res_cbwith id of request, and so thisres_cb` will be invoked after recieving responce with same id.

void json_rpc_process_response(struct json_rpc *jr, struct json_object *resp);

int json_rpc_is_response(struct json_object *obj);

methods that deal with received JSON-RPC responses

Clone this wiki locally