# jsonrpc Package

## JSONRPC

JSON-RPC wrappers for version 1.0 and 2.0.

Objects diring init operation try to choose JSON-RPC 2.0 and in case of error JSON-RPC 1.0. from_json methods could decide what format is it by presence of ‘jsonrpc’ attribute.

class jsonrpc.jsonrpc.JSONRPCRequest

  Bases: `jsonrpc.utils.JSONSerializable`

  JSONRPC Request.

  classmethod from_data(data)
  classmethod from_json(json_str)

## Exceptions

JSON-RPC Exceptions.

exception jsonrpc.exceptions.JSONRPCDispatchException(code=None, message=None, data=None, *args, **kwargs)

  Bases: `jsonrpc.exceptions.JSONRPCException`

  JSON-RPC Dispatch Exception.

  Should be thrown in dispatch methods.

class jsonrpc.exceptions.JSONRPCError(code=None, message=None, data=None)

  Bases: `object`

  Error for JSON-RPC communication.

  When a rpc call encounters an error, the Response Object MUST contain the error member with a value that is a Object with the following members:

  Parameters

    - code (int) – A Number that indicates the error type that occurred. This MUST be an integer. The error codes from and including -32768 to -32000 are reserved for pre-defined errors. Any code within this range, but not defined explicitly below is reserved for future use. The error codes are nearly the same as those suggested for XML-RPC at the following url: http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php

    - message (str) – A String providing a short description of the error. The message SHOULD be limited to a concise single sentence.

    - data (int or str or dict or list, optional) – A Primitive or Structured value that contains additional information about the error. This may be omitted. The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).

  property code
  property data
  static deserialize(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

    Deserialize `s` (a `str`, `bytes` or `bytearray` instance containing a JSON document) to a Python object.

    `object_hook` is an optional function that will be called with the result of any object literal decode (a `dict`). The return value of `object_hook` will be used instead of the `dict`. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

    `object_pairs_hook` is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of `object_pairs_hook` will be used instead of the `dict`. This feature can be used to implement custom decoders. If `object_hook` is also defined, the `object_pairs_hook` takes priority.

    `parse_float`, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal).

    `parse_int`, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).

    `parse_constant`, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered.

    To use a custom `JSONDecoder` subclass, specify it with the `cls` kwarg; otherwise `JSONDecoder` is used.

  classmethod from_json(json_str)
  property json
  property message
  static serialize(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

    Serialize `obj` to a JSON formatted `str`.

    If `skipkeys` is true then `dict` keys that are not basic types (`str`, `int`, `float`, `bool`, `None`) will be skipped instead of raising a `TypeError`.

    If `ensure_ascii` is false, then the return value can contain non-ASCII characters if they appear in strings contained in `obj`. Otherwise, all such characters are escaped in JSON strings.

    If `check_circular` is false, then the circular reference check for container types will be skipped and a circular reference will result in an `RecursionError` (or worse).

    If `allow_nan` is false, then it will be a `ValueError` to serialize out of range `float` values (`nan`, `inf`, `-inf`) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (`NaN`, `Infinity`, `-Infinity`).

    If `indent` is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. `None` is the most compact representation.

    If specified, `separators` should be an `(item_separator, key_separator)` tuple. The default is `(', ', ': ')` if indent is `None` and `(',', ': ')` otherwise. To get the most compact JSON representation, you should specify `(',', ':')` to eliminate whitespace.

    `default(obj)` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.

    If sort_keys is true (default: `False`), then the output of dictionaries will be sorted by key.

    To use a custom `JSONEncoder` subclass (e.g. one that overrides the `.default()` method to serialize additional types), specify it with the `cls` kwarg; otherwise `JSONEncoder` is used.

exception jsonrpc.exceptions.JSONRPCException

  Bases: `Exception`

  JSON-RPC Exception.

class jsonrpc.exceptions.JSONRPCInternalError(code=None, message=None, data=None)

  Bases: `jsonrpc.exceptions.JSONRPCError`

  Internal error.

  Internal JSON-RPC error.

  CODE = -32603
  MESSAGE = 'Internal error'

class jsonrpc.exceptions.JSONRPCInvalidParams(code=None, message=None, data=None)

  Bases: `jsonrpc.exceptions.JSONRPCError`

  Invalid params.

  Invalid method parameter(s).

  CODE = -32602
  MESSAGE = 'Invalid params'

class jsonrpc.exceptions.JSONRPCInvalidRequest(code=None, message=None, data=None)

  Bases: `jsonrpc.exceptions.JSONRPCError`

  Invalid Request.

  The JSON sent is not a valid Request object.

  CODE = -32600
  MESSAGE = 'Invalid Request'

exception jsonrpc.exceptions.JSONRPCInvalidRequestException

  Bases: `jsonrpc.exceptions.JSONRPCException`

  Request is not valid.

class jsonrpc.exceptions.JSONRPCMethodNotFound(code=None, message=None, data=None)

  Bases: `jsonrpc.exceptions.JSONRPCError`

  Method not found.

  The method does not exist / is not available.

  CODE = -32601
  MESSAGE = 'Method not found'

class jsonrpc.exceptions.JSONRPCParseError(code=None, message=None, data=None)

  Bases: `jsonrpc.exceptions.JSONRPCError`

  Parse Error.

  Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.

  CODE = -32700
  MESSAGE = 'Parse error'

class jsonrpc.exceptions.JSONRPCServerError(code=None, message=None, data=None)

  Bases: `jsonrpc.exceptions.JSONRPCError`

  Server error.

  Reserved for implementation-defined server-errors.

  CODE = -32000
  MESSAGE = 'Server error'

## Manager

class jsonrpc.manager.JSONRPCResponseManager

  Bases: `object`

  JSON-RPC response manager.

  Method brings syntactic sugar into library. Given dispatcher it handles request (both single and batch) and handles errors. Request could be handled in parallel, it is server responsibility.

  Parameters

    - request_str (str) – json string. Will be converted into JSONRPC20Request, JSONRPC20BatchRequest or JSONRPC10Request

    - dispather (dict) – dict<function_name:function>.

  RESPONSE_CLASS_MAP = {'1.0': <class 'jsonrpc.jsonrpc1.JSONRPC10Response'>, '2.0': <class 'jsonrpc.jsonrpc2.JSONRPC20Response'>}
  classmethod handle(request_str, dispatcher)
  classmethod handle_request(request, dispatcher)

    Handle request data.

    At this moment request has correct jsonrpc format.

    Parameters

      - request (dict) – data parsed from request_str.

      - dispatcher (jsonrpc.dispatcher.Dispatcher) –

## jsonrpc.backend.django module

class jsonrpc.backend.django.JSONRPCAPI(dispatcher=None)

  Bases: `object`

  jsonrpc(request)

    JSON-RPC 2.0 handler.

  jsonrpc_map(request)

    Map of json-rpc available calls.

    Return str

  property urls

jsonrpc.backend.django.response_serialize(obj)

  Serializes response’s data object to JSON.