jsonrpc2

package module
v0.0.0-...-66cb45a Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: MIT Imports: 3 Imported by: 6

README

go-jsonrpc2

fast jsonrpc 2.0 implement for go

spec: https://www.jsonrpc.org/specification

Usage

If you wanna using jsonrpc2 over http, check the http folder

If you wanna using jsonrpc2 over tcp, check the tcp folder

Documentation

Overview

Package jsonrpc2 provides fast jsonrpc 2.0 message encoding and decoding based on jsoniter, with http/tcp implements and examples

Visit repo https://github.com/c0mm4nd/go-jsonrpc2 and get more details

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParseFailed    = errors.New("parse error")
	ErrInternalError  = errors.New("invalid params")
	ErrInvalidParams  = errors.New("invalid params")
	ErrMethodNotFound = errors.New("method not found")
	ErrInvalidRequest = errors.New("invalid request")
)

some built-in errors

View Source
var JSON = new(stdJSON)

JSON is the json parser inside the jsonrpc2, developers can change parser via assign another (un)marshaller into this symbol

Functions

func IsBatchMarshal

func IsBatchMarshal(raw []byte) bool

IsBatchMarshal is a helper to check whether the rawbytes are from a jsonrpc message batch

func MarshalMessageBatch

func MarshalMessageBatch(m *JsonRpcMessage) ([]byte, error)

Types

type Error

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

func NewError

func NewError(code int, err error, moreDataParams ...interface{}) *Error

if is built-in errors, code can be 0 or any.

type JsonMsgType

type JsonMsgType int
const (
	TypeInvalidMsg JsonMsgType = iota
	TypeNotificationMsg
	TypeRequestMsg
	TypeErrorMsg
	TypeSuccessMsg
)

type JsonRpcMessage

type JsonRpcMessage struct {
	Version 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 interface{} `json:"id,omitempty"`
}

func NewJsonRpcError

func NewJsonRpcError(id interface{}, errParams *Error) *JsonRpcMessage

NewJsonRpcError returns a JSON-RPC 2.0 error message structures.

func NewJsonRpcNotification

func NewJsonRpcNotification(method string, params []byte) *JsonRpcMessage

NewJsonRpcNotification returns a JSON-RPC 2.0 notification message structures which doesnt have id. params should be json marshaled

func NewJsonRpcRequest

func NewJsonRpcRequest(id interface{}, method string, params []byte) *JsonRpcMessage

NewJsonRpcRequest returns a JSON-RPC 2.0 request message structures. id must be string/int/nil type. params should be json marshaled

func NewJsonRpcSuccess

func NewJsonRpcSuccess(id interface{}, result []byte) *JsonRpcMessage

NewJsonRpcSuccess returns a JSON-RPC 2.0 success message structures. result should be json marshaled

func UnmarshalMessage

func UnmarshalMessage(raw []byte) (*JsonRpcMessage, error)

func (*JsonRpcMessage) GetType

func (m *JsonRpcMessage) GetType() JsonMsgType

func (*JsonRpcMessage) LoadParams

func (msg *JsonRpcMessage) LoadParams(paramsPtr interface{}) error

LoadParams reads the params from raw bytes to the pointer

func (*JsonRpcMessage) WithParams

func (msg *JsonRpcMessage) WithParams(params interface{}) error

WithParams converts the params into raw bytes and store in the message

type JsonRpcMessageBatch

type JsonRpcMessageBatch []*JsonRpcMessage

func NewJsonRpcMessageBatch

func NewJsonRpcMessageBatch(messages ...*JsonRpcMessage) JsonRpcMessageBatch

func UnmarshalMessageBatch

func UnmarshalMessageBatch(raw []byte) (JsonRpcMessageBatch, error)

func (*JsonRpcMessageBatch) Marshal

func (b *JsonRpcMessageBatch) Marshal() ([]byte, error)

func (*JsonRpcMessageBatch) Unmarshal

func (b *JsonRpcMessageBatch) Unmarshal(raw []byte) (*JsonRpcMessageBatch, error)

Directories

Path Synopsis
Package jsonrpc2http provides http implement of [jsonrpc2](https://godoc.org/github.com/c0mm4nd/go-jsonrpc2), the fast jsonrpc 2.0 message encoding and decoding based on jsoniter Visit repo https://godoc.org/github.com/c0mm4nd/go-jsonrpc2 and get example
Package jsonrpc2http provides http implement of [jsonrpc2](https://godoc.org/github.com/c0mm4nd/go-jsonrpc2), the fast jsonrpc 2.0 message encoding and decoding based on jsoniter Visit repo https://godoc.org/github.com/c0mm4nd/go-jsonrpc2 and get example
Package jsonrpc2net provides net(tcp, udp) implement of [jsonrpc2](https://godoc.org/github.com/c0mm4nd/go-jsonrpc2), the fast jsonrpc 2.0 message encoding and decoding Visit repo https://godoc.org/github.com/c0mm4nd/go-jsonrpc2 and get example
Package jsonrpc2net provides net(tcp, udp) implement of [jsonrpc2](https://godoc.org/github.com/c0mm4nd/go-jsonrpc2), the fast jsonrpc 2.0 message encoding and decoding Visit repo https://godoc.org/github.com/c0mm4nd/go-jsonrpc2 and get example
Package jsonrpc2ws provides websocket implement of [jsonrpc2](https://godoc.org/github.com/c0mm4nd/go-jsonrpc2), the fast jsonrpc 2.0 message encoding and decoding Visit repo https://godoc.org/github.com/c0mm4nd/go-jsonrpc2 and get example
Package jsonrpc2ws provides websocket implement of [jsonrpc2](https://godoc.org/github.com/c0mm4nd/go-jsonrpc2), the fast jsonrpc 2.0 message encoding and decoding Visit repo https://godoc.org/github.com/c0mm4nd/go-jsonrpc2 and get example

Jump to

Keyboard shortcuts

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