{"modulePath":"github.com/sourcegraph/jsonrpc2","version":"v0.2.3","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package jsonrpc2\n\nPackage jsonrpc2 provides a client and server implementation of \\[JSON-RPC 2.0]([http://www.jsonrpc.org/specification](http://www.jsonrpc.org/specification)).\n\n## Constants\n\n```go\nconst (\n\tCodeParseError     = -32700\n\tCodeInvalidRequest = -32600\n\tCodeMethodNotFound = -32601\n\tCodeInvalidParams  = -32602\n\tCodeInternalError  = -32603\n)\n```\nErrors defined in the JSON-RPC spec. See [http://www.jsonrpc.org/specification#error\\_object](http://www.jsonrpc.org/specification#error_object).\n\n## Variables\n\n```go\nvar ErrClosed = errors.New(\"jsonrpc2: connection is closed\")\n```\nErrClosed indicates that the JSON-RPC connection is closed (or in the process of closing).\n\n## Types\n\n```go\ntype CallOption interface {\n\t// contains filtered or unexported methods\n}\n```\nCallOption is an option that can be provided to (\\*Conn).Call to configure custom behavior. See Meta.\n\n```go\nfunc ExtraField(name string, value interface{}) CallOption\n```\nExtraField returns a call option which attaches the given name/value pair to the JSON-RPC 2.0 request. This can be used to add arbitrary extensions to JSON RPC 2.0.\n\n```go\nfunc Meta(meta interface{}) CallOption\n```\nMeta returns a call option which attaches the given meta object to the JSON-RPC 2.0 request (this is a Sourcegraph extension to JSON RPC 2.0 for carrying metadata).\n\n```go\nfunc PickID(id ID) CallOption\n```\nPickID returns a call option which sets the ID on a request. Care must be taken to ensure there are no conflicts with any previously picked ID, nor with the default sequence ID.\n\n```go\nfunc StringID() CallOption\n```\nStringID returns a call option that instructs the request ID to be set as a string.\n\n```go\ntype Conn struct {\n\t// contains filtered or unexported fields\n}\n```\nConn is a JSON-RPC client/server connection. The JSON-RPC protocol is symmetric, so a Conn runs on both ends of a client-server connection.\n\n```go\nfunc NewConn(ctx context.Context, stream ObjectStream, h Handler, opts ...ConnOpt) *Conn\n```\nNewConn creates a new JSON-RPC client/server connection using the given ReadWriteCloser (typically a TCP connection or stdio). The JSON-RPC protocol is symmetric, so a Conn runs on both ends of a client-server connection.\n\nNewConn consumes stream, so you should call Close on the returned Conn not on the given stream or its underlying connection.\n\nConn is closed when the given context's Done channel is closed.\n\n```go\nfunc (c *Conn) Call(ctx context.Context, method string, params, result interface{}, opts ...CallOption) error\n```\nCall initiates a JSON-RPC call using the specified method and params, and waits for the response. If the response is successful, its result is stored in result (a pointer to a value that can be JSON-unmarshaled into); otherwise, a non-nil error is returned. See DispatchCall for more details.\n\n```go\nfunc (c *Conn) Close() error\n```\nClose closes the JSON-RPC connection. The connection may not be used after it has been closed.\n\n```go\nfunc (c *Conn) DisconnectNotify() \u003c-chan struct{}\n```\nDisconnectNotify returns a channel that is closed when the underlying connection is disconnected.\n\n```go\nfunc (c *Conn) DispatchCall(ctx context.Context, method string, params interface{}, opts ...CallOption) (Waiter, error)\n```\nDispatchCall dispatches a JSON-RPC call using the specified method and params, and returns a call proxy or an error. Call Wait() on the returned proxy to receive the response. Only use this function if you need to do work after dispatching the request, otherwise use Call.\n\nThe params member is omitted from the JSON-RPC request if the given params is nil. Use json.RawMessage(\"null\") to send a JSON-RPC request with its params member set to null.\n\n```go\nfunc (c *Conn) Notify(ctx context.Context, method string, params interface{}, opts ...CallOption) error\n```\nNotify is like Call, but it returns when the notification request is sent (without waiting for a response, because JSON-RPC notifications do not have responses).\n\nThe params member is omitted from the JSON-RPC request if the given params is nil. Use json.RawMessage(\"null\") to send a JSON-RPC request with its params member set to null.\n\n```go\nfunc (c *Conn) Reply(ctx context.Context, id ID, result interface{}) error\n```\nReply sends a successful response with a result. It returns after writing the response to the ObjectStream; it does not wait for the peer to receive or process the response.\n\n```go\nfunc (c *Conn) ReplyWithError(ctx context.Context, id ID, respErr *Error) error\n```\nReplyWithError sends a response with an error.\n\n```go\nfunc (c *Conn) SendResponse(ctx context.Context, resp *Response) error\n```\nSendResponse sends resp to the peer. It is lower level than (\\*Conn).Reply.\n\n```go\ntype ConnOpt func(*Conn)\n```\nConnOpt is the type of function that can be passed to NewConn to customize the Conn before it is created.\n\n```go\nfunc LogMessages(logger Logger) ConnOpt\n```\nLogMessages causes all messages sent and received on conn to be logged using the provided logger.\n\n```go\nfunc OnRecv(f func(*Request, *Response)) ConnOpt\n```\nOnRecv causes all requests received on conn to invoke f(req, nil) and all responses to invoke f(req, resp),\n\n```go\nfunc OnSend(f func(*Request, *Response)) ConnOpt\n```\nOnSend causes all requests sent on conn to invoke f(req, nil) and all responses to invoke f(nil, resp),\n\n```go\nfunc SetLogger(logger Logger) ConnOpt\n```\nSetLogger sets the logger for the connection.\n\n```go\ntype Error struct {\n\tCode    int64            `json:\"code\"`\n\tMessage string           `json:\"message\"`\n\tData    *json.RawMessage `json:\"data,omitempty\"`\n}\n```\nError represents a JSON-RPC response error.\n\n```go\nfunc (e *Error) Error() string\n```\nError implements the Go error interface.\n\n```go\nfunc (e *Error) SetError(v interface{})\n```\nSetError sets e.Data to the JSON encoding of v. If JSON marshaling fails, it panics.\n\n```go\ntype Handler interface {\n\t// Handle is called to handle a request. While it is running, the connection\n\t// does not process any other requests or responses. A handler that waits for\n\t// a call's response must therefore run asynchronously. If you do not require\n\t// strict ordering behavior of received RPCs, it is suggested to wrap your\n\t// handler in AsyncHandler. The context is automatically canceled when the\n\t// connection closes.\n\tHandle(context.Context, *Conn, *Request)\n}\n```\nHandler handles JSON-RPC requests and notifications.\n\n```go\nfunc AsyncHandler(h Handler) Handler\n```\nAsyncHandler wraps a Handler such that each request is handled in its own goroutine. Handlers may therefore start or finish in a different order than requests are received. It is a convenience wrapper.\n\n```go\ntype HandlerWithErrorConfigurer struct {\n\t// contains filtered or unexported fields\n}\n```\nHandlerWithErrorConfigurer is a handler created by HandlerWithError.\n\n```go\nfunc HandlerWithError(handleFunc func(context.Context, *Conn, *Request) (result interface{}, err error)) *HandlerWithErrorConfigurer\n```\nHandlerWithError implements Handler by calling the func for each request and handling returned errors and results.\n\n```go\nfunc (h *HandlerWithErrorConfigurer) Handle(ctx context.Context, conn *Conn, req *Request)\n```\nHandle implements Handler.\n\n```go\nfunc (h *HandlerWithErrorConfigurer) SuppressErrClosed() Handler\n```\nSuppressErrClosed makes the handler suppress jsonrpc2.ErrClosed errors from being logged. The original handler \\`h\\` is returned.\n\nThis is optional because only in some cases is this behavior desired. For example, a handler that serves end-user connections may not want to log ErrClosed because it just indicates the end-user connection has gone away for any reason (they could have lost wifi connection, are no longer interested in the request and closed the connection, etc) and as such it would be log spam, whereas a handler that serves internal connections would never expect connections to go away unexpectedly (which could indicate service degradation, etc) and as such ErrClosed should always be logged.\n\n```go\ntype ID struct {\n\t// At most one of Num or Str may be nonzero. If both are zero\n\t// valued, then IsNum specifies which field's value is to be used\n\t// as the ID.\n\tNum uint64\n\tStr string\n\n\t// IsString controls whether the Num or Str field's value should be\n\t// used as the ID, when both are zero valued. It must always be\n\t// set to true if the request ID is a string.\n\tIsString bool\n}\n```\nID represents a JSON-RPC 2.0 request ID, which may be either a string or number (or null, which is unsupported).\n\n```go\nfunc (id ID) MarshalJSON() ([]byte, error)\n```\nMarshalJSON implements json.Marshaler.\n\n```go\nfunc (id ID) String() string\n```\n\n```go\nfunc (id *ID) UnmarshalJSON(data []byte) error\n```\nUnmarshalJSON implements json.Unmarshaler.\n\n```go\ntype JSONRPC2 interface {\n\t// Call issues a standard request (http://www.jsonrpc.org/specification#request_object).\n\tCall(ctx context.Context, method string, params, result interface{}, opt ...CallOption) error\n\n\t// Notify issues a notification request (http://www.jsonrpc.org/specification#notification).\n\tNotify(ctx context.Context, method string, params interface{}, opt ...CallOption) error\n\n\t// Close closes the underlying connection, if it exists.\n\tClose() error\n}\n```\nJSONRPC2 describes an interface for issuing requests that speak the JSON-RPC 2 protocol.  It isn't really necessary for this package itself, but is useful for external users that use the interface as an API boundary.\n\n```go\ntype Logger interface {\n\tPrintf(format string, v ...interface{})\n}\n```\nLogger interface implements one method - Printf. You can use the stdlib logger \\*log.Logger\n\n```go\ntype ObjectCodec interface {\n\t// WriteObject writes a JSON-RPC 2.0 object to the stream.\n\tWriteObject(stream io.Writer, obj interface{}) error\n\n\t// ReadObject reads the next JSON-RPC 2.0 object from the stream\n\t// and stores it in the value pointed to by v.\n\tReadObject(stream *bufio.Reader, v interface{}) error\n}\n```\nAn ObjectCodec specifies how to encode and decode a JSON-RPC 2.0 object in a stream.\n\n```go\ntype ObjectStream interface {\n\t// WriteObject writes a JSON-RPC 2.0 object to the stream.\n\tWriteObject(obj interface{}) error\n\n\t// ReadObject reads the next JSON-RPC 2.0 object from the stream\n\t// and stores it in the value pointed to by v.\n\tReadObject(v interface{}) error\n\n\tio.Closer\n}\n```\nAn ObjectStream is a bidirectional stream of JSON-RPC 2.0 objects.\n\n```go\nfunc NewBufferedStream(conn io.ReadWriteCloser, codec ObjectCodec) ObjectStream\n```\nNewBufferedStream creates a buffered stream from a network connection (or other similar interface). The underlying objectStream is used to produce the bytes to write to the stream for the JSON-RPC 2.0 objects.\n\n```go\nfunc NewPlainObjectStream(conn io.ReadWriteCloser) ObjectStream\n```\nNewPlainObjectStream creates a buffered stream from a network connection (or other similar interface). The underlying objectStream produces plain JSON-RPC 2.0 objects without a header.\n\n```go\ntype PlainObjectCodec struct {\n\t// contains filtered or unexported fields\n}\n```\nPlainObjectCodec reads/writes plain JSON-RPC 2.0 objects without a header.\n\nDeprecated: use NewPlainObjectStream\n\n```go\nfunc (c PlainObjectCodec) ReadObject(stream *bufio.Reader, v interface{}) error\n```\nReadObject implements ObjectCodec.\n\n```go\nfunc (c PlainObjectCodec) WriteObject(stream io.Writer, v interface{}) error\n```\nWriteObject implements ObjectCodec.\n\n```go\ntype Request struct {\n\tMethod string           `json:\"method\"`\n\tParams *json.RawMessage `json:\"params,omitempty\"`\n\tID     ID               `json:\"id\"`\n\tNotif  bool             `json:\"-\"`\n\n\t// Meta optionally provides metadata to include in the request.\n\t//\n\t// NOTE: It is not part of spec. However, it is useful for propagating\n\t// tracing context, etc.\n\tMeta *json.RawMessage `json:\"meta,omitempty\"`\n\n\t// ExtraFields optionally adds fields to the root of the JSON-RPC request.\n\t//\n\t// NOTE: It is not part of the spec, but there are other protocols based on\n\t// JSON-RPC 2 that require it.\n\tExtraFields []RequestField `json:\"-\"`\n}\n```\nRequest represents a JSON-RPC request or notification. See [http://www.jsonrpc.org/specification#request\\_object](http://www.jsonrpc.org/specification#request_object) and [http://www.jsonrpc.org/specification#notification](http://www.jsonrpc.org/specification#notification).\n\n```go\nfunc (r Request) MarshalJSON() ([]byte, error)\n```\nMarshalJSON implements json.Marshaler and adds the \"jsonrpc\":\"2.0\" property.\n\n```go\nfunc (r *Request) SetExtraField(name string, v interface{}) error\n```\nSetExtraField adds an entry to r.ExtraFields, so that it is added to the JSON encoding of the request, as a way to add arbitrary extensions to JSON RPC 2.0. If JSON marshaling fails, it returns an error.\n\n```go\nfunc (r *Request) SetMeta(v interface{}) error\n```\nSetMeta sets r.Meta to the JSON encoding of v. If JSON marshaling fails, it returns an error.\n\n```go\nfunc (r *Request) SetParams(v interface{}) error\n```\nSetParams sets r.Params to the JSON encoding of v. If JSON marshaling fails, it returns an error.\n\n```go\nfunc (r *Request) UnmarshalJSON(data []byte) error\n```\nUnmarshalJSON implements json.Unmarshaler.\n\n```go\ntype RequestField struct {\n\tName  string\n\tValue interface{}\n}\n```\nRequestField is a top-level field that can be added to the JSON-RPC request.\n\n```go\ntype Response struct {\n\tID     ID               `json:\"id\"`\n\tResult *json.RawMessage `json:\"result,omitempty\"`\n\tError  *Error           `json:\"error,omitempty\"`\n\n\t// Meta optionally provides metadata to include in the response.\n\t//\n\t// NOTE: It is not part of spec. However, it is useful for propagating\n\t// tracing context, etc.\n\tMeta *json.RawMessage `json:\"meta,omitempty\"`\n}\n```\nResponse represents a JSON-RPC response. See [http://www.jsonrpc.org/specification#response\\_object](http://www.jsonrpc.org/specification#response_object).\n\n```go\nfunc (r Response) MarshalJSON() ([]byte, error)\n```\nMarshalJSON implements json.Marshaler and adds the \"jsonrpc\":\"2.0\" property.\n\n```go\nfunc (r *Response) SetResult(v interface{}) error\n```\nSetResult sets r.Result to the JSON representation of v. If JSON marshaling fails, it returns an error.\n\n```go\nfunc (r *Response) UnmarshalJSON(data []byte) error\n```\nUnmarshalJSON implements json.Unmarshaler.\n\n```go\ntype VSCodeObjectCodec struct{}\n```\nVSCodeObjectCodec reads/writes JSON-RPC 2.0 objects with Content-Length and Content-Type headers, as specified by [https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#base-protocol](https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#base-protocol).\n\n```go\nfunc (VSCodeObjectCodec) ReadObject(stream *bufio.Reader, v interface{}) error\n```\nReadObject implements ObjectCodec.\n\n```go\nfunc (VSCodeObjectCodec) WriteObject(stream io.Writer, obj interface{}) error\n```\nWriteObject implements ObjectCodec.\n\n```go\ntype VarintObjectCodec struct{}\n```\nVarintObjectCodec reads/writes JSON-RPC 2.0 objects with a varint header that encodes the byte length.\n\n```go\nfunc (VarintObjectCodec) ReadObject(stream *bufio.Reader, v interface{}) error\n```\nReadObject implements ObjectCodec.\n\n```go\nfunc (VarintObjectCodec) WriteObject(stream io.Writer, obj interface{}) error\n```\nWriteObject implements ObjectCodec.\n\n```go\ntype Waiter struct {\n\t// contains filtered or unexported fields\n}\n```\nWaiter proxies an ongoing JSON-RPC call.\n\n```go\nfunc (w Waiter) Wait(ctx context.Context, result interface{}) error\n```\nWait for the result of an ongoing JSON-RPC call. If the response is successful, its result is stored in result (a pointer to a value that can be JSON-unmarshaled into); otherwise, a non-nil error is returned.\n\n","path":"github.com/sourcegraph/jsonrpc2","name":"jsonrpc2","synopsis":"Package jsonrpc2 provides a client and server implementation of [JSON-RPC 2.0](http://www.jsonrpc.org/specification).","isRedistributable":true}
