jsonrpc

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsonrpc reads, writes and inspects newline-delimited JSON-RPC 2.0 messages without decoding fields actp does not need, so unknown fields survive.

Index

Constants

View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
	CodeInternalError  = -32603
	CodeServerError    = -32000
	CodeCancelled      = -32800
)

Standard and ACP error codes.

Variables

This section is empty.

Functions

func CheckEnvelope added in v0.1.1

func CheckEnvelope(raw []byte) error

CheckEnvelope refuses a message that Go's decoding (case-insensitive keys, last one wins) and a peer's exact-key decoding could read differently: a key that differs from an envelope field only in case, or an envelope field given twice. Messages that are not JSON objects are left to Parse.

Types

type Ambiguous added in v0.1.1

type Ambiguous struct {
	Reason string
	// ID is the message's id when it has exactly one non-null "id" key, else nil.
	ID json.RawMessage
	// Request reports whether a method key is present in any spelling.
	Request bool
}

Ambiguous is a message whose envelope two JSON decoders can read differently.

func (*Ambiguous) Error added in v0.1.1

func (a *Ambiguous) Error() string

type Error

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

Error is a JSON-RPC error object.

func (*Error) Error

func (e *Error) Error() string

type Kind

type Kind int

Kind is the shape of a message.

const (
	KindRequest Kind = iota + 1
	KindNotification
	KindResponse
)

type Message

type Message struct {
	Raw    json.RawMessage
	ID     json.RawMessage // nil when absent
	Method string
	Params json.RawMessage
	Result json.RawMessage
	Error  *Error
}

Message is one JSON-RPC message with its header fields peeked.

func NewError

func NewError(id json.RawMessage, code int, message string, data any) *Message

NewError builds an error response.

func NewNotification

func NewNotification(method string, params any) (*Message, error)

NewNotification builds a notification.

func NewRequest

func NewRequest(id json.RawMessage, method string, params any) (*Message, error)

NewRequest builds a request.

func NewResult

func NewResult(id json.RawMessage, result any) *Message

NewResult builds a success response.

func Parse

func Parse(raw []byte) (*Message, error)

Parse peeks a raw message. The raw bytes are kept as is.

func (*Message) IDKey

func (m *Message) IDKey() string

IDKey is a map key for the message id.

func (*Message) Kind

func (m *Message) Kind() Kind

Kind reports whether the message is a request, notification or response.

func (*Message) WithParams

func (m *Message) WithParams(params any) (*Message, error)

WithParams returns a copy of the message with new params.

func (*Message) WithResult

func (m *Message) WithResult(result any) (*Message, error)

WithResult returns a copy of the message with a new result.

type Reader

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

Reader reads one message per line.

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) Read

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

Read returns the next non-empty line. Lines that are not JSON-RPC return an error together with the raw line, so callers can log and skip them.

type Writer

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

Writer writes one message per line. Safe for concurrent use.

func NewWriter

func NewWriter(w io.Writer) *Writer

func (*Writer) Write

func (w *Writer) Write(m *Message) error

func (*Writer) WriteRaw

func (w *Writer) WriteRaw(raw []byte) error

WriteRaw writes an already compact JSON message.

Jump to

Keyboard shortcuts

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