jsonrpc2

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package jsonrpc2 provides a JSON-RPC 2.0-compatible stream implementation.

Index

Constants

View Source
const (
	// ErrorCodeParseError is the error code for parsing errors.
	ErrorCodeParseError = -32700
	// ErrorCodeInvalidRequest is the error code for invalid request errors.
	ErrorCodeInvalidRequest = -32600
	// ErrorCodeMethodNotFound is the error code for method not found errors.
	ErrorCodeMethodNotFound = -32601
	// ErrorCodeInvalidParams is the error code for invalid params errors.
	ErrorCodeInvalidParams = -32602
	// ErrorCodeInternalError is the error code for internal errors.
	ErrorCodeInternalError = -32603
	// ErrorCodeServerError is the error code for server errors.
	ErrorCodeServerError = -32000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Code    ErrorCode        `json:"code"`
	Message string           `json:"message"`
	Data    *json.RawMessage `json:"data,omitempty"`
}

Error represents a JSON-RPC error. For more information, see the specification.

func NewError

func NewError(code ErrorCode, message string, data *json.RawMessage) *Error

NewError creates a new JSON-RPC error.

func (*Error) Error

func (e *Error) Error() string

Error returns the error message.

type ErrorCode

type ErrorCode int

ErrorCode represents a JSON-RPC error code. For more information, see the specification.

type Handler

type Handler interface {
	Handle(method string, params json.RawMessage) (json.RawMessage, *Error)
	GetShutdownChan() chan struct{}
}

Handler represents a JSON-RPC handler.

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
	ID      RequestID       `json:"id"`
}

Request represents a JSON-RPC request. For more information, see the specification.

func NewRequest

func NewRequest(method string, params json.RawMessage) *Request

NewRequest creates a new JSON-RPC request.

type RequestID

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

RequestID represents a JSON-RPC request ID.

func NewRequestID

func NewRequestID(value json.RawMessage) *RequestID

NewRequestID creates a new JSON-RPC request ID.

func (*RequestID) IsNull

func (r *RequestID) IsNull() bool

IsNull returns true if the RequestID is null.

func (*RequestID) MarshalJSON

func (r *RequestID) MarshalJSON() ([]byte, error)

MarshalJSON marshals the RequestID to JSON.

func (*RequestID) String

func (r *RequestID) String() string

String returns the string representation of the RequestID.

func (*RequestID) UnmarshalJSON

func (r *RequestID) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals JSON into the RequestID.

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   json.RawMessage `json:"error,omitempty"`
	ID      RequestID       `json:"id"`
}

Response represents a JSON-RPC response. For more information, see the specification.

func NewErrorResponse

func NewErrorResponse(errData []byte, id RequestID) *Response

NewErrorResponse creates a new JSON-RPC error response.

func NewResponse

func NewResponse(result json.RawMessage, id RequestID) *Response

NewResponse creates a new JSON-RPC response.

type Server

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

Server represents the JSON-RPC server.

func NewServer

func NewServer(
	handler Handler,
	reader io.Reader,
	writer io.Writer,
) (*Server, error)

NewServer creates a new JSON-RPC server.

func (*Server) Start

func (s *Server) Start() error

Start starts the JSON-RPC server.

type Stream

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

Stream represents the JSON-RPC stream.

func NewStream

func NewStream(reader io.Reader, writer io.Writer) *Stream

NewStream creates a new JSON-RPC stream.

func (*Stream) ReadMessage

func (s *Stream) ReadMessage() ([]byte, error)

ReadMessage reads a message from the stream.

func (*Stream) WriteMessage

func (s *Stream) WriteMessage(data []byte) error

WriteMessage writes a message to the stream.

Jump to

Keyboard shortcuts

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