2018-04-03 08:58:26 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"git.loafle.net/commons/rpc-go/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
var null = json.RawMessage([]byte("null"))
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Codec
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// NewServerCodec returns a new JSON Codec.
|
|
|
|
func NewServerCodec() protocol.ServerCodec {
|
|
|
|
return &ServerCodec{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerCodec creates a ServerRequestCodec to process each request.
|
|
|
|
type ServerCodec struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *ServerCodec) NewRequest(buf []byte) (protocol.ServerRequestCodec, error) {
|
|
|
|
return newServerRequestCodec(buf)
|
|
|
|
}
|
|
|
|
|
2018-04-06 07:50:10 +00:00
|
|
|
func (sc *ServerCodec) NewRequestWithString(method string, params []string, id interface{}) (protocol.ServerRequestCodec, error) {
|
|
|
|
req := &clientRequest{
|
|
|
|
Version: Version,
|
|
|
|
Method: method,
|
|
|
|
Params: params,
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := json.Marshal(req)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc.NewRequest(buf)
|
|
|
|
}
|
|
|
|
|
2018-04-03 08:58:26 +00:00
|
|
|
func (sc *ServerCodec) NewNotification(method string, args []interface{}) ([]byte, error) {
|
|
|
|
params, err := convertParamsToStringArray(args)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
noti := &serverNotification{Method: method, Params: params}
|
|
|
|
res := &serverResponse{Version: Version, Result: noti}
|
|
|
|
|
|
|
|
return json.Marshal(res)
|
|
|
|
}
|