rpc-go/protocol/json/server.go
crusader 83215a8fd2 ing
2018-04-06 16:50:10 +09:00

55 lines
1.3 KiB
Go

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