jsonrpc

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const ProtocolVersion = "2.0"

ProtocolVersion is the supported JSON-RPC protocol version.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyMessage

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

AnyMessage is a generic JSON-RPC message (request, notification, or response).

func (*AnyMessage) AsRequest

func (m *AnyMessage) AsRequest() *Request

AsRequest returns the message as a Request if it is a request message, otherwise nil

func (*AnyMessage) AsResponse

func (m *AnyMessage) AsResponse() *Response

AsResponse returns the message as a Response if it is a response message, otherwise nil

func (*AnyMessage) Type

func (m *AnyMessage) Type() string

Type returns "request" if the message is a request, "response" if it's a response, or "notification" if it's a notification

func (*AnyMessage) UnmarshalJSON

func (m *AnyMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for AnyMessage It enforces JSON-RPC 2.0 semantics and validates message structure

type Error

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

Error is a JSON-RPC error object.

type ErrorCode

type ErrorCode int

ErrorCode is a JSON-RPC 2.0 error code.

const (
	// ErrorCodeParseError indicates invalid JSON was received by the server.
	ErrorCodeParseError ErrorCode = -32700
	// ErrorCodeInvalidRequest indicates the JSON sent is not a valid Request object.
	ErrorCodeInvalidRequest ErrorCode = -32600
	// ErrorCodeMethodNotFound indicates the method does not exist / is not available.
	ErrorCodeMethodNotFound ErrorCode = -32601
	// ErrorCodeInvalidParams indicates invalid method parameters.
	ErrorCodeInvalidParams ErrorCode = -32602
	// ErrorCodeInternalError indicates an internal JSON-RPC error.
	ErrorCodeInternalError ErrorCode = -32603
)

type Message

type Message []byte

Message is the raw JSON representation of a JSON-RPC message.

type Request

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

Request represents a JSON-RPC request (with an ID) or notification (without ID).

type RequestID

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

RequestID represents a JSON-RPC ID that can be either a string or a number

func NewRequestID

func NewRequestID(value interface{}) *RequestID

NewRequestID creates a new JSONRPCId from a string or number

func (*RequestID) IsNil

func (id *RequestID) IsNil() bool

IsNil returns true if the ID is nil/empty

func (*RequestID) MarshalJSON

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

MarshalJSON implements json.Marshaler

func (*RequestID) String

func (id *RequestID) String() string

String returns the string representation of the ID

func (*RequestID) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler

func (*RequestID) Value

func (id *RequestID) Value() interface{}

Value returns the underlying value

type Response

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

Response represents a JSON-RPC response.

func NewErrorResponse

func NewErrorResponse(id *RequestID, code ErrorCode, message string, data any) *Response

NewErrorResponse builds an error JSON-RPC response with the given code.

func NewResultResponse

func NewResultResponse(id *RequestID, result any) (*Response, error)

NewResultResponse builds a successful JSON-RPC response object.

Jump to

Keyboard shortcuts

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