rpc-go/protocol/json/server.go

39 lines
962 B
Go
Raw Normal View History

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)
}
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)
}