Interpretation of json-rpc 2.0 specifications

Source: Internet
Author: User
Tags parse error

Interpretation of json-rpc 2.0 specifications

The JSON-RPC2.0 specifications are maintained by the JSON-RPC Working Group (json-rpc@googlegroups.com) and released on 2010-03-26 (based on the version), the most recent update on 2013-01-04.

Overall, the 2.0 version of The JSON-RPC specification change is very small, the major changes are about 3 points:

  1. You can use arrays or named parameters for parameters.
  2. Details of batch requests are clarified
  3. The error handling mechanism standardizes the compatibility with Version 1.0.
    • It is recommended that the 2.0 standard be compatible with the 1.0 protocol, but it is not mandatory. If it is not compatible, a friendly prompt is recommended.
    • A parameter is added to the request and response messages to indicate the Protocol Version: jsonrpc, which must be "2.0 ".
    • Method modification: The method name starting with rpc indicates the methods and extensions in rpc. It must be unavailable elsewhere.
    • The request parameters can use arrays.[Parameter 1, parameter 2,]You can also use named Parameters{key:value}.
    • When the request parameter is null, params can be omitted.
    • Generally, the id should not be null. If it is a numerical value, it should not be a decimal number.
    • When no id exists in the request, it is treated as a notification. (In 1.0, the id is null .)
    • The request parameters must be exactly matched, including case sensitivity.
    • The response must contain result or error, but both Members must not. Batch request

      Finally, I made it clear how to perform this batch request, that is, to wrap multiple request objects in an array in a request. For example, package five requests:

      --> [        {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},        {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},        {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},        {"foo": "boo"},        {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},        {"jsonrpc": "2.0", "method": "get_data", "id": "9"}     ]<-- [        {"jsonrpc": "2.0", "result": 7, "id": "1"},        {"jsonrpc": "2.0", "result": 19, "id": "2"},        {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},        {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"},        {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}    ]

      The Specification defines that all requests should be executed concurrently, and the return order is not guaranteed. The client uses the id to match the corresponding request and response. In addition, if an error occurs during request processing, a unified error message is returned (that is, the failure is not distinguished, and all errors are considered as failures ). This design seems to be for consideration of transactions, but it should be troublesome in general use cases.

      Error object

      The improved error mechanism is that the error becomes a clearly defined object. There are three attributes:

      • Code: Numeric value. See the error code in the next section.
      • Message: an error message in string format.
      • Data: Optional. A value or object defined by the server to append additional information.

        It is much better than the original extensive error mechanism.

        Error Code

        I borrowed the server-side error code from the XML-RPC:

        Code Message Meaning
        -32700 Parse error Invalid JSON was encoded ed by the server. An error occurred on the server while parsing the JSON text.
        -32600 Invalid Request The JSON sent is not a valid Request object.
        -32601 Method not found The method does not exist/is not available.
        -32602 Invalid params Invalid method parameter (s ).
        -32603 Internal error Internal JSON-RPC error.
        -32000 to-32099 Server error Reserved for implementation-defined server-errors.
        References
        • 2.0 full text of regulations
        • 2.0-1.0 Improvement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.