jsonrpc2

package module
v0.0.0-...-a413889 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2020 License: MIT Imports: 2 Imported by: 0

README

go-jsonrpc2

fast jsonrpc 2.0 implement for go

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/maoxs2/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 (
	EmptyArrayBytes, _ = json.Marshal(map[string]interface{}{}) // {}
	EmptyListBytes, _  = json.Marshal([]interface{}{})          // []
)

some helper variables

Functions

func IsBatchMarshal

func IsBatchMarshal(raw []byte) bool

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

func MarshalMessage

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

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 JsonRpcMessage

type JsonRpcMessage struct {
	Version string `json:"jsonrpc"`

	Method string `json:"method,omitempty"`

	Params jsoniter.RawMessage `json:"params,omitempty"`
	Result jsoniter.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() MsgType

func (*JsonRpcMessage) Marshal

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

func (*JsonRpcMessage) Unmarshal

func (m *JsonRpcMessage) Unmarshal(raw []byte) (*JsonRpcMessage, error)

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)

type MsgType

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

Directories

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

Jump to

Keyboard shortcuts

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