jsonrpc

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package jsonrpc provides generic JSON-RPC 2.0 types and utilities that can be shared between LSP and other JSON-RPC based protocols.

Index

Constants

View Source
const (
	CodeParseError     int32 = -32700
	CodeInvalidRequest int32 = -32600
	CodeMethodNotFound int32 = -32601
	CodeInvalidParams  int32 = -32602
	CodeInternalError  int32 = -32603
)

Standard JSON-RPC error codes.

Variables

View Source
var (
	ErrInvalidHeader        = errors.New("jsonrpc: invalid header")
	ErrInvalidContentLength = errors.New("jsonrpc: invalid content length")
	ErrNoContentLength      = errors.New("jsonrpc: no content length")
)
View Source
var ErrInvalidJSONRPCVersion = errors.New("invalid JSON-RPC version")

Functions

This section is empty.

Types

type ID

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

ID represents a JSON-RPC message ID, which can be either a string or integer.

func NewID

func NewID(rawValue IntegerOrString) *ID

NewID creates an ID from an IntegerOrString value.

func NewIDInt

func NewIDInt(i int32) *ID

NewIDInt creates an integer ID.

func NewIDString

func NewIDString(str string) *ID

NewIDString creates a string ID.

func (*ID) MarshalJSON

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

func (*ID) MustInt

func (id *ID) MustInt() int32

func (*ID) String

func (id *ID) String() string

func (*ID) TryInt

func (id *ID) TryInt() (int32, bool)

func (*ID) UnmarshalJSON

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

type IntegerOrString

type IntegerOrString struct {
	Integer *int32
	String  *string
}

IntegerOrString is a helper type for creating IDs.

type JSONRPCVersion

type JSONRPCVersion struct{}

JSONRPCVersion represents the JSON-RPC version field, always "2.0".

func (JSONRPCVersion) MarshalJSON

func (JSONRPCVersion) MarshalJSON() ([]byte, error)

func (*JSONRPCVersion) UnmarshalJSON

func (*JSONRPCVersion) UnmarshalJSON(data []byte) error

type Message

type Message struct {
	JSONRPC JSONRPCVersion `json:"jsonrpc"`
	ID      *ID            `json:"id,omitzero"`
	Method  string         `json:"method,omitzero"`
	Params  json.Value     `json:"params,omitzero"`
	Result  json.Value     `json:"result,omitzero"`
	Error   *ResponseError `json:"error,omitzero"`
}

Message represents a raw JSON-RPC message that can be a request, notification, or response. Unlike lsproto.Message, this keeps params/result as raw JSON for generic handling.

func (*Message) IsNotification

func (m *Message) IsNotification() bool

IsNotification returns true if this message is a notification (has method but no ID).

func (*Message) IsRequest

func (m *Message) IsRequest() bool

IsRequest returns true if this message is a request (has ID and method).

func (*Message) IsResponse

func (m *Message) IsResponse() bool

IsResponse returns true if this message is a response (has ID but no method).

func (*Message) Kind

func (m *Message) Kind() MessageKind

Kind returns the kind of message this is.

type MessageKind

type MessageKind int

MessageKind indicates what type of message this is.

const (
	MessageKindNotification MessageKind = iota
	MessageKindRequest
	MessageKindResponse
)

type Reader

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

Reader reads JSON-RPC messages with Content-Length framing.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader creates a new Reader.

func (*Reader) Read

func (r *Reader) Read() ([]byte, error)

Read reads the next message payload.

type RequestMessage

type RequestMessage struct {
	JSONRPC JSONRPCVersion `json:"jsonrpc"`
	ID      *ID            `json:"id,omitzero"`
	Method  string         `json:"method"`
	Params  any            `json:"params,omitzero"`
}

RequestMessage is a convenience type for creating request/notification messages.

type ResponseError

type ResponseError struct {
	Code    int32  `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitzero"`
}

ResponseError represents a JSON-RPC error response.

func (*ResponseError) Error

func (r *ResponseError) Error() string

func (*ResponseError) String

func (r *ResponseError) String() string

type ResponseMessage

type ResponseMessage struct {
	JSONRPC JSONRPCVersion `json:"jsonrpc"`
	ID      *ID            `json:"id,omitzero"`
	Result  any            `json:"result,omitzero"`
	Error   *ResponseError `json:"error,omitzero"`
}

ResponseMessage is a convenience type for creating response messages.

type Writer

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

Writer writes JSON-RPC messages with Content-Length framing.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer.

func (*Writer) Write

func (w *Writer) Write(data []byte) error

Write writes a message payload with Content-Length header.

Jump to

Keyboard shortcuts

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