JSONRPC2: a set of classes to encode/decode JSON RPC messages

Classes to parse and serialise JSON-RPC 2.0 requests/responses. Access message parts via methods.
835 Downloads
Updated 27 Jan 2014

View License

Background
----------
JSON (JavaScript Object Notation) is a highly portable, human-readable and "fat-free" text format to represent complex and hierarchical data. It is as powerful as XML, but less verbose. JSON format is widely used for data-exchange in applications, and is essential for the wild success of Ajax and Web2.0. (JSONlab definition)

Message queues provide an asynchronous communications protocol, meaning that the sender and receiver of the message do not need to interact with the message queue at the same time. Messages placed onto the queue are stored until the recipient retrieves them. (Wikipedia definition)

RPC (Remote Procedure Call) allows one computer to execute a procedure (aka method) on another computer. The execution begins when the remote machine receives a message telling it what procedure to execute. RPC packets typically pass a correlation id for matching responses to the original call, response queue name identifying the message queue where responses should be sent, and value which holds one or more messages formatted in a supported protocol (here we are working with messages in the JSONRPC protocol).

JSONRPC is a protocol specifying the format of messages passed via RPC through a message queue. This code implements each of the standard message types. Each type is a separate class with properties to hold the values of the parts allowing them to be accessed (set/get) through method calls and property setters/getters. This implementation supports parameters specified by name or by order.

* JSONRPC2Notification - parts: version, method, params - Notifications specify the method to execute and the parameters to pass to the method. No response is expected of a notification since it does not have an id for associating the notification with a response.

* JSONRPC2Request - parts: version, id, method, params - Requests specify the method to execute and the parameters to pass to the method. A response sending the results which were returned from the method is expected with an id that corresponds to the request's id.

* JSONRPC2Response - parts: version, id, result - Responses specify the results that were returned from the execution of a request's method.

* JSONRPC2Error - parts: version, id, errcode, errmsg - Errors specify any problems that occurred during parsing of the incoming request/notification or execution errors of the method.

NOTE: Version (i.e., JSONRPC protocol version) is always 2.0 for this implementation.

Technology
----------
Several pieces of technology need to be in place to work with message queues.

Message Queue Implementation Layer - This handles the sending and receiving messages at the lowest level. No interpretation is placed on the contents of the message. It will define the format of the RPC packets (i.e., correlation_id, reply_to, message). ActiveMQ is an example of a Message Queue implementation. (Not included in this code.)

Message Queue MATLAB Wrapper Layer - ActiveMQ is implemented in JAVA. This layer wraps java methods with MATLAB code allowing the Listerner Implementation Layer access to the Message Queue Implementation Layer (Not included in this code.)

Listener Implementation Layer - This handles listening to a request queue, executing methods, and sending results to the response queue. It uses the Message Queue Wrapper Layer to interact with queues. (Not included in this code.)

JSONRPC Implementation Layer - This handles decoding requests and encoding responses/errors. (THIS CODE)

Examples
--------
See jsonrpc2Examples package for working examples.

Dependencies
------------
To run, you must download JSONlab dependency. Tests have additional dependencies. See README.txt in each package for more information.

NOTE: I have code that implements the other layers, but it is not as clean. I can provide that code to you as is upon request.

Cite As

E. L. Rayle (2024). JSONRPC2: a set of classes to encode/decode JSON RPC messages (https://www.mathworks.com/matlabcentral/fileexchange/45238-jsonrpc2-a-set-of-classes-to-encode-decode-json-rpc-messages), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0