rpc/protocol/json/server.go
crusader 0b1486a3bd ing
2018-03-24 02:26:41 +09:00

39 lines
962 B
Go

package json
import (
"encoding/json"
"git.loafle.net/commons_go/rpc/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)
}