JSON-RPC
Encyclopedia
JSON-RPC is a remote procedure call
Remote procedure call
In computer science, a remote procedure call is an inter-process communication that allows a computer program to cause a subroutine or procedure to execute in another address space without the programmer explicitly coding the details for this remote interaction...

 protocol encoded in JSON
JSON
JSON , or JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects...

. It is a very simple protocol (and very similar to XML-RPC
XML-RPC
XML-RPC is a remote procedure call protocol which uses XML to encode its calls and HTTP as a transport mechanism. "XML-RPC" also refers generically to the use of XML for remote procedure call, independently of the specific protocol...

), defining only a handful of data types and commands. JSON-RPC allows for notifications (info sent to the server that doesn't require a response) and for multiple calls to be sent to the server which may be answered out of order.

History

Version Description Dated
1.0 Original version Currently considered official according to 2005
1.1 WD Working draft Adds named parameters, adds specific error codes, and adds introspection functions. 2006-08-07
1.1 Alt Suggestion for a simple JSON-RPC 1.1 Alternative proposal to 1.1 WD. 2007-05-06
1.2 Proposal A later revision of this document was renamed to 2.0. 2007-12-27
2.0 Specification proposal 2009-05-24
2.0 (Revised) Specification proposal 2010-03-26

Usage

JSON-RPC works by sending a request to a server implementing this protocol. The client in that case is typically software wanting to call a single method of a remote system. Multiple input parameters can be passed to the remote method as an array or object, whereas the method itself can return multiple output data as well. (This depends on the implemented version.)

A remote method is invoked by sending a request to a remote service using HTTP or a TCP/IP socket (starting with version 2.0). When Using HTTP, the content-type may be defined as application/json.

All transfer types are single objects, serialized using JSON. A request is a call to a specific method provided by a remote system. It must contain three certain properties:
  • method - A String with the name of the method to be invoked.
  • params - An Array of objects to be passed as parameters to the defined method.
  • id - A value of any type, which is used to match the response with the request that it is replying to.


The receiver of the request must reply with a valid response to all received requests. A response must contain the properties mentioned below.
  • result - The data returned by the invoked method. If an error occurred while invoking the method, this value must be null.
  • error - A specified Error code if there was an error invoking the method, otherwise null.
  • id - The id of the request it is responding to.


Since there are situations where no response is needed or even desired, notifications were introduced. A notification is similar to a request except for the id, which is not needed because no response will be returned. In this case the id property should be omitted (Version 2.0) or be null (Version 1.0).

Examples

In these examples, --> denotes data sent to a service (request), while <-- denotes data coming from a service. (Although this direction often is called response in client-server computing, depending on the JSON-RPC version it does not necessarily imply answer to a request).

Version 1.0

A simple request and response:

--> { "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
<-- { "result": "Hello JSON-RPC", "error": null, "id": 1}

This example shows parts of a communication from an example chat application. The chat service sends notifications for each chat message the client peer should receive. The client peer sends requests to post messages to the chat and expects a positive reply to know the message has been posted.

...
--> {"method": "postMessage", "params": ["Hello all!"], "id": 99}
<-- {"result": 1, "error": null, "id": 99}
<-- {"method": "handleMessage", "params": ["user1", "we were just talking"], "id": null}
<-- {"method": "handleMessage", "params": ["user3", "sorry, gotta go now, ttyl"], "id": null}
--> {"method": "postMessage", "params": ["I have a question:"], "id": 101}
<-- {"method": "userLeft", "params": ["user3"], "id": null}
<-- {"result": 1, "error": null, "id": 101}
...


Because params field is an array of objects, the following format is also ok:



{
"method": "methodnamehere",
"params": [
{
"firstparam": "this contains information of the firstparam.",
"secondparam": 1121211234,
"thirdparam": "this contains information of the thirdparam."
},
{
"fourthparam": "this is already a different object.",
"secondparam": "there can be same name fields in different objects.",
"thirdparam": "this contains information of the thirdparam."
}
],
"id": 1234
}

Version 1.1 (Working Draft)

The format of the contents of a request might be something like that shown below:

{
"version": "1.1",
"method": "confirmFruitPurchase",
"id": "194521489",
"params": [
[ "apple", "orange", "pear" ],
1.123
]
}

//The format of a response might be something like this:

{
"version": "1.1",
"result": "done",
"error": null,
"id": "194521489"
}

Version 2.0 (Specification Proposal)

Procedure Call with positional parameters:

--> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
<-- {"jsonrpc": "2.0", "result": 19, "id": 1}


--> {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
<-- {"jsonrpc": "2.0", "result": -19, "id": 2}

Procedure Call with named parameters:

--> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
<-- {"jsonrpc": "2.0", "result": 19, "id": 3}


--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
<-- {"jsonrpc": "2.0", "result": 19, "id": 4}

Notification:

--> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}


--> {"jsonrpc": "2.0", "method": "foobar"}

Procedure Call of non-existent procedure:

--> {"jsonrpc": "2.0", "method": "foobar", "id": 10}
<-- {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Procedure not found."}, "id": 10}

Procedure Call with invalid JSON:

--> {"jsonrpc": "2.0", "method": "foobar", "params": "bar", "baz"]
<-- {"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}

Procedure Call with invalid JSON-RPC:

--> [1,2,3]
<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC."}, "id": null}


--> {"jsonrpc": "2.0", "method": 1, "params": "bar"}
<-- {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC."}, "id": null}

Implementations

  • jsonrpc4j is a java implementation JSON-RPC 2.0 supporting streaming as well as HTTP servers. It also has support for spring service exporter\consumer.
  • json-rpc is a generic java/javascript implementation which integrates well on Android/Servlets/Standalone Java/Javascript/App-Engine applications.
  • php-json-rpc is a simple PHP implementation of JSON-RPC 2.0.
  • easyXDM is a library for cross-domain messaging with a built in RPC feature. The library supports all browsers by using a mix of postMessage, nix, frameElement, window.name, and FIM, and is very easy to use.
  • Dojo Toolkit
    Dojo Toolkit
    Dojo Toolkit is an open source modular JavaScript library designed to ease the rapid development of cross-platform, JavaScript/Ajax-based applications and web sites. It was started by Alex Russell, Dylan Schiemann, David Schontzler, and others in 2004 and is dual-licensed under the modified BSD...

     offers a broad support for JSON-RPC
  • Pmrpc is an inter-window and Web Worker remote procedure call JavaScript library for use within HTML5 browsers. Pmrpc is an implementation of JSON-RPC using the HTML5 postMessage API for message transport.
  • JSON Toolkit an implementation for Delphi
  • Jayrock
    Jayrock
    Jayrock is an open source implementation of JSON and JSON-RPC for the Microsoft .NET Framework, including ASP.NET. Jayrock allows clients such as client-side web page JavaScript, to be able to call into server-side methods using JSON as the wire format and JSON-RPC as the procedure invocation...

     is a server implementation of JSON-RPC 1.0 for versions 1.1 and 2.0 of Microsoft's .NET Framework
    .NET Framework
    The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...

    .
  • As of version 2.0, XINS
    XML Interface for Network Services
    XML Interface for Network Services is an open source technology for definition and implementation of internet applications, which enforces a specification-oriented approach.-Specification-oriented approach:...

     supports both JSON
    JSON
    JSON , or JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects...

     and JSON-RPC.
  • qooxdoo includes a JSON-RPC implementation with optional backends in Java, PHP, Perl and Python.
  • Barracuda Web Server's integrated JSON-RPC online documentation.
  • JSON-RPC implementation in JavaScript includes JSON-RPC over HTTP and over TCP/IP Sockets
  • JSON/XML-RPC Client and Server Implementations which abstract-away the differences between JSON-RPC and XML-RPC and permit cross-site requests.
  • jpoxy - A simple Java JSON-RPC implementation designed to be simple to implement and able to expose public methods in existing POJOs via a robust RPC framework.
  • jabsorb - A lightweight Ajax/Web 2.0 JSON-RPC Java framework that extends the JSON-RPC protocol with additional ORB
    Object request broker
    In distributed computing, an object request broker is a piece of middleware software that allows programmers to make program calls from one computer to another via a network...

     functionality such as circular references support.
  • JSON-RPC implementation in Java A JavaScript to Java AJAX communications library (now merged with jabsorb.)
  • Jettison - Java library
  • JSON Service Another JSON-RPC library for Java build on top of Jackson library.
  • LEWOStuff Includes Java support for JSON-RPC also with specific support for WebObjects.
  • Perl implementations on the CPAN
    CPAN
    CPAN, the Comprehensive Perl Archive Network, is an archive of nearly 100,000 modules of software written in Perl, as well as documentation for it. It has a presence on the World Wide Web at and is mirrored worldwide at more than 200 locations...

  • JsonRpc-Cpp OpenSource JSON-RPC implementation in C++
  • TclSOAP Includes Tcl support for JSON-RPC in addition to SOAP and XML-RPC.
  • Pyjamas contains a JSONRPC client implementation, as standard (Pyjamas
    Pyjamas (software)
    Pyjamas is a tool and framework for developing Ajax applications in Python. It contains a stand-alone Python-to-JavaScript compiler, an Ajax framework and widget toolkit, and through use of these components, developers can write comprehensive applications, to run in all major web browsers, without...

     is a framework where applications are written in Python
    Python (programming language)
    Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

     but are compiled to Javascript
    JavaScript
    JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

    ).
  • Zope 3 JSON-RPC Python based JSON RPC server and client implementation for Zope 3
  • JQuery JSON-RPC Server This is a JSON-RPC server, specifically made to work with the Zend Framework JSON RPC Server. The Zend Framework JSON-RPC server is mildly off spec, and therefore this may not work with other JSON-RPC servers.
  • jsonrpc2php is a PHP5 BSD'd JSON-RPC 2.0 Core class and example Server
  • jsonrpclib is a JSON-RPC client module for Python.
  • Tornado web server
    Tornado (web server)
    Tornado is a scalable, non-blocking web server and web application framework. It was developed for use by FriendFeed; the company was acquired by Facebook in 2009 and Tornado was open-sourced soon after.- Performance :...

     supports serving JSON-RPC using the tornadorpc plugin.
  • JSON-RPC 2.0 Base is a minimalist Java library for parsing, representing and serializing JSON-RPC 2.0 messages (open source).
  • JSON-RPC 2.0 Client is a Java library for establishing client sessions to a JSON-RPC 2.0 server (open source).
  • JSON-RPC 2.0 Server is a simple Java framework for processing JSON-RPC 2.0 requests and notifications (open source).
  • JSON-RPC 2.0 Shell for querying, testing and debugging remote JSON-RPC 2.0 web services (commercial).
  • Synopse SQLite3 database Framework Open Source Delphi database access, User Interface generation, reporting, i18n in Client/Server AJAX/RESTful model.
  • Objective-C DeferredKit includes a JSON-RPC 1.0 client.
  • java-json-rpc JSON-RPC 2.0 implementation for J2EE servers.
  • lib-json-rpc JSON-RPC 2.0 implementation on servlet, client, javascript
  • simplejsonrpc Another simple JSON-RPC 2.0 Servlet, servicing the methods of a class.
  • Tivoka An object oriented JSON-RPC 2.0 client and server implementation for PHP
  • junior PHP client/server library for JSON-RPC 2.0
  • json-rpc-php PHP client/server library for JSON-RPC 2.0
  • jimson JSON-RPC 2.0 client and server for Ruby
  • The Wakanda platform includes a JSON-RPC 2.0 client in its Ajax Framework and a JSON-RPC 2.0 service in Server-Side JavaScript
  • Phobos is a JSON-RPC 2.0 implementation for Qt/C++
    C++
    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

    . You can use it to send and receive messages over any communication layer (there are TCP and HTTP classes ready to use, also).
  • Deimos is a JSON-RPC 1.0 and 2.0 implementation for nodejs/javascript
    JavaScript
    JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

     (server-side).
  • Another PHP implementation of 2.0 protocol with the "dot magic" for php (= support for grouping of methods and separation by dots)
  • An implementation of JSON-RPC 2.0 for Python + Twisted which uses composition to maximize code reusability
  • Demiurgic JSON-RPC JSON-RPC 2.0 Client for Objective-C



The original official homepage has links to more implementations.

See also

  • Remote procedure call
    Remote procedure call
    In computer science, a remote procedure call is an inter-process communication that allows a computer program to cause a subroutine or procedure to execute in another address space without the programmer explicitly coding the details for this remote interaction...

  • SOAPjr
    SOAPjr
    SOAPjr is a protocol specification for exchanging structured information in the implementation of Web services in computer networks. It is a hybrid of SOAP and JSON-RPC .-Introduction:...

     - a hybrid of SOAP and JSON-RPC
  • JSON-WSP - a JSON-RPC inspired protocol with a service description specification.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK