rpc/protocol/json/server.go

39 lines
962 B
Go
Raw Normal View History

2017-10-25 14:52:47 +00:00
package json
import (
2018-03-23 03:22:16 +00:00
"encoding/json"
2017-10-26 07:21:35 +00:00
"git.loafle.net/commons_go/rpc/protocol"
2017-10-25 14:52:47 +00:00
)
var null = json.RawMessage([]byte("null"))
// ----------------------------------------------------------------------------
// Codec
// ----------------------------------------------------------------------------
2017-10-31 09:25:44 +00:00
// NewServerCodec returns a new JSON Codec.
2017-11-26 10:15:51 +00:00
func NewServerCodec() protocol.ServerCodec {
2018-03-23 07:43:05 +00:00
return &ServerCodec{}
2017-10-25 14:52:47 +00:00
}
2017-11-26 10:15:51 +00:00
// ServerCodec creates a ServerRequestCodec to process each request.
2017-10-31 09:25:44 +00:00
type ServerCodec struct {
2018-03-23 07:43:05 +00:00
}
2018-03-23 17:26:41 +00:00
func (sc *ServerCodec) NewRequest(buf []byte) (protocol.ServerRequestCodec, error) {
return newServerRequestCodec(buf)
2018-03-23 13:45:48 +00:00
}
2018-03-23 17:26:41 +00:00
func (sc *ServerCodec) NewNotification(method string, args []interface{}) ([]byte, error) {
2018-03-23 13:45:48 +00:00
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)
}