jsonrpc

package
v1.378.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package jsonrpc implements a JSON-RPC 2.0 client over an io.ReadWriter (e.g. stdio). It supports bidirectional RPC: both the caller and the remote peer can initiate requests, and either side may send notifications (messages without an id).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a bidirectional JSON-RPC 2.0 client that communicates over a pair of io.Reader/io.Writer (typically stdin/stdout of a subprocess).

func New

func New(r io.Reader, w io.Writer, verbose bool) *Client

New creates a new Client.

func (*Client) Call

func (c *Client) Call(ctx context.Context, method string, params interface{}) (json.RawMessage, error)

Call sends a JSON-RPC request and waits for the response.

func (*Client) Listen

func (c *Client) Listen(ctx context.Context) error

Listen starts the receive loop. It blocks until the context is cancelled or the underlying reader returns an error (e.g. EOF on process exit). Call this in a goroutine.

func (*Client) Notify

func (c *Client) Notify(method string, params interface{}) error

Notify sends a JSON-RPC notification (no id, no response expected).

func (*Client) RegisterNotificationHandler

func (c *Client) RegisterNotificationHandler(method string, h NotificationHandler)

RegisterNotificationHandler registers a handler for inbound notifications.

func (*Client) RegisterRequestHandler

func (c *Client) RegisterRequestHandler(method string, h RequestHandler)

RegisterRequestHandler registers a handler for inbound requests from the remote peer (bidirectional RPC – the peer initiates the call).

func (*Client) Respond

func (c *Client) Respond(id json.RawMessage, result interface{}, rpcErr *RPCError) error

Respond sends a JSON-RPC response to a remote request.

type Message

type Message struct {
	JSONRPC string           `json:"jsonrpc"`
	ID      *json.RawMessage `json:"id,omitempty"`
	Method  string           `json:"method,omitempty"`
	Params  json.RawMessage  `json:"params,omitempty"`
	Result  json.RawMessage  `json:"result,omitempty"`
	Error   *RPCError        `json:"error,omitempty"`
}

Message is the union type for all JSON-RPC 2.0 messages.

type NotificationHandler

type NotificationHandler func(params json.RawMessage)

NotificationHandler handles an inbound notification (no response expected).

type RPCError

type RPCError struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

RPCError is a JSON-RPC 2.0 error object.

func (*RPCError) Error

func (e *RPCError) Error() string

type RequestHandler

type RequestHandler func(ctx context.Context, params json.RawMessage) (interface{}, error)

RequestHandler handles an inbound request from the remote peer. It must return a result (to be serialised) or an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL