rpc/protocol/json/server.go

39 lines
962 B
Go
Raw Normal View History

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