or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

backends.mdcore-jsonrpc.mddispatcher.mdexceptions.mdindex.mdrequests-responses.md
tile.json

tessl/pypi-json-rpc

JSON-RPC transport implementation for Python supporting both 1.0 and 2.0 protocols with Django and Flask backends

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/json-rpc@1.15.x

To install, run

npx @tessl/cli install tessl/pypi-json-rpc@1.15.0

index.mddocs/

JSON-RPC

A comprehensive Python implementation of JSON-RPC 1.0 and 2.0 transport specification. This library provides protocol implementation only, without transport functionality, making it transport-agnostic and suitable for integration with any transport mechanism like HTTP, WebSockets, or message queues.

Package Information

  • Package Name: json-rpc
  • Language: Python
  • Installation: pip install json-rpc
  • Python Support: 2.6+, 3.3+, PyPy
  • Optional Integrations: Django, Flask

Core Imports

from jsonrpc import JSONRPCResponseManager, dispatcher

For specific components:

from jsonrpc import Dispatcher
from jsonrpc.manager import JSONRPCResponseManager
from jsonrpc.exceptions import JSONRPCError, JSONRPCDispatchException
from jsonrpc.jsonrpc import JSONRPCRequest

Basic Usage

from jsonrpc import JSONRPCResponseManager, dispatcher

# Method 1: Using the global dispatcher
@dispatcher.add_method
def add(a, b):
    return a + b

@dispatcher.add_method
def subtract(a, b):
    return a - b

# Handle JSON-RPC request
request_data = '{"jsonrpc": "2.0", "method": "add", "params": [5, 3], "id": 1}'
response = JSONRPCResponseManager.handle(request_data, dispatcher)
print(response.json)
# Output: {"jsonrpc": "2.0", "result": 8, "id": 1}

# Method 2: Creating a custom dispatcher
from jsonrpc import Dispatcher

my_dispatcher = Dispatcher()
my_dispatcher.add_method(lambda x: x * 2, name="double")
my_dispatcher["echo"] = lambda s: s

request_data = '{"jsonrpc": "2.0", "method": "double", "params": [21], "id": 2}'
response = JSONRPCResponseManager.handle(request_data, my_dispatcher)
print(response.json)
# Output: {"jsonrpc": "2.0", "result": 42, "id": 2}

Architecture

The json-rpc library follows a clean separation of concerns:

  • Protocol Layer: Handles JSON-RPC 1.0 and 2.0 protocol specifics (request/response format validation)
  • Dispatcher: Maps method names to callable functions with flexible registration patterns
  • Manager: Orchestrates request processing, method dispatch, and error handling
  • Transport Agnostic: No built-in transport - integrates with any HTTP framework, WebSocket library, or messaging system
  • Backend Integrations: Ready-to-use Django and Flask integrations for web applications

This design enables the library to serve as a foundation for JSON-RPC implementations across different transport mechanisms and frameworks.

Capabilities

Core JSON-RPC Protocol

Complete implementation of JSON-RPC 1.0 and 2.0 protocols with automatic version detection, batch request support, and comprehensive error handling.

class JSONRPCResponseManager:
    @classmethod
    def handle(cls, request_str: str, dispatcher: dict, context: dict = None): ...
    
    @classmethod 
    def handle_request(cls, request, dispatcher: dict, context: dict = None): ...

Core JSON-RPC Protocol

Request and Response Objects

Structured request and response objects for both JSON-RPC versions with automatic serialization, validation, and batch processing support.

class JSONRPCRequest:
    @classmethod
    def from_json(cls, json_str: str): ...
    
    @classmethod
    def from_data(cls, data: dict): ...

class JSONRPC20Request:
    def __init__(self, method: str = None, params = None, _id = None, is_notification: bool = None): ...
    
class JSONRPC10Request:
    def __init__(self, method: str = None, params = None, _id = None, is_notification: bool = None): ...

Requests and Responses

Method Dispatching

Flexible method registration and dispatch system supporting functions, classes, objects, and decorators with context injection.

class Dispatcher:
    def __init__(self, prototype = None): ...
    def add_method(self, f = None, name: str = None, context_arg: str = None): ...
    def add_class(self, cls): ...
    def add_object(self, obj): ...
    def add_dict(self, dict: dict, prefix: str = ''): ...

Method Dispatching

Exception and Error Handling

Comprehensive error handling with predefined JSON-RPC error types and custom exception support for robust error reporting.

class JSONRPCError:
    def __init__(self, code: int = None, message: str = None, data = None): ...

class JSONRPCDispatchException(Exception):
    def __init__(self, code: int = None, message: str = None, data = None, *args, **kwargs): ...

Exceptions and Error Handling

Backend Integrations

Ready-to-use integrations for Django and Flask frameworks providing HTTP endpoints, URL routing, and request handling.

# Django backend
from jsonrpc.backend.django import JSONRPCAPI

# Flask backend  
from jsonrpc.backend.flask import JSONRPCAPI

Backend Integrations

Types

# Core dispatcher type
dispatcher: Dispatcher

# Version information
__version__: str
version: str

# Project information
__project__: str
PROJECT: str