Newer
Older
// A value of this type can a JSON-RPC request, notification, successful response or
// error response. Which one it is depends on the fields.
ID *ID `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Error error `json:"error,omitempty"`
Extensions map[string]json.RawMessage `json:"-"`
func NewStringReader(x string) io.ReadCloser {
return io.NopCloser(strings.NewReader(x))
}
// use encoder
fail := enc.Obj(func(e *jx.Encoder) {
e.Field("jsonrpc", func(e *jx.Encoder) {
e.Str("2.0")
})
if m.ID != nil {
e.Field("id", func(e *jx.Encoder) {
e.Raw(m.ID.RawMessage())
})
}
if m.Method != "" {
e.Field("method", func(e *jx.Encoder) {
e.Str(m.Method)
})
}
if m.Error != nil {
e.Field("error", func(e *jx.Encoder) {
})
return
}
if len(m.Params) != 0 {
e.Field("params", func(e *jx.Encoder) {
e.Raw(m.Params)
})
}
var n int64
n, err = io.Copy(e, m.Result)
if n == 0 {
e.Null()
}
if m.Extensions != nil {
for k, v := range m.Extensions {
e.Field(k, func(e *jx.Encoder) {
e.Raw(v)
})
}
}
if fail {
return fmt.Errorf("jx encoding error")
}
// output
return nil
}
func UnmarshalMessage(m *Message, dec *jx.Decoder) error {
default:
raw, err := d.Raw()
if err != nil {
return err
}
if m.Extensions == nil {
m.Extensions = make(map[string]json.RawMessage)
}
m.Extensions[key] = json.RawMessage(raw)
case "jsonrpc":
value, err := d.Str()
if err != nil {
return err
}
if value != VersionString {
return NewInvalidRequestError("Invalid Version")
}
case "id":
raw, err := d.Raw()
if err != nil {
return err
}
id := &ID{}
err = id.UnmarshalJSON(raw)
m.ID = id
if err != nil {
return err
}
case "method":
case "error":
val, err := d.Raw()
if err != nil {
return err
}
m.Error = &JsonError{}
err = json.Unmarshal(val, m.Error)
if err != nil {
return err
}
}
func (m *Message) UnmarshalJSON(xs []byte) error {
dec := jx.GetDecoder()
defer jx.PutDecoder(dec)
dec.ResetBytes(xs)
return UnmarshalMessage(m, dec)
}
if err != nil {
return nil, err
}
err = enc.Close()
if err != nil {
return nil, err
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
// isBatch returns true when the first non-whitespace characters is '['
for _, c := range raw {
// skip insignificant whitespace (http://www.ietf.org/rfc/rfc4627.txt)
switch c {
case 0x20, 0x09, 0x0a, 0x0d:
continue
// parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error
// checks in this function because the raw message has already been syntax-checked when it
// is called. Any non-JSON-RPC messages in the input return the zero value of
// Message.
func ParseMessage(in json.RawMessage) ([]*Message, bool) {
return ReadMessage(jx.DecodeBytes(in))
}
// parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error
// checks in this function because the raw message has already been syntax-checked when it
// is called. Any non-JSON-RPC messages in the input return the zero value of
// Message.
func ReadMessage(dec *jx.Decoder) ([]*Message, bool) {
msgs := []*Message{{}}
switch dec.Next() {
case jx.Object:
_ = UnmarshalMessage(msgs[0], dec)
return msgs, false
default:
return msgs, false
case jx.Array:
msgs = []*Message{}
dec.Arr(func(d *jx.Decoder) error {
msg := new(Message)