package json import ( "encoding/json" "git.loafle.net/overflow/rpc-go/codec" "git.loafle.net/overflow/rpc-go/protocol" ) // ---------------------------------------------------------------------------- // Codec // ---------------------------------------------------------------------------- // NewClientCodec returns a new JSON Codec. func NewCustomClientCodec(codecSelector codec.CodecSelector) protocol.ClientCodec { return &ClientCodec{ codecSelector: codecSelector, } } // NewClientCodec returns a new JSON Codec. func NewClientCodec() protocol.ClientCodec { return NewCustomClientCodec(codec.DefaultCodecSelector) } // ClientCodec creates a ClientCodecRequest to process each request. type ClientCodec struct { codecSelector codec.CodecSelector } func (cc *ClientCodec) NewRequest(method string, args []interface{}, id interface{}) (messageType int, message []byte, err error) { params, err := convertParamsToStringArray(args) if nil != err { return 0, nil, err } req := &clientRequest{ Version: Version, Method: method, Params: params, ID: id, } buf, err := json.Marshal(req) if nil != err { return 0, nil, err } return cc.codecSelector.Encode(buf) } func (cc *ClientCodec) NewResponse(messageType int, message []byte) (protocol.ClientResponseCodec, error) { buf, err := cc.codecSelector.Decode(messageType, message) if nil != err { return nil, err } return newClientResponseCodec(buf) }