1 Star 0 Fork 0

wuzpdev/goworld

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
JSONMsgPacker.go 695 Bytes
Copy Edit Raw Blame History
seis authored 2017-09-25 16:06 . tested jsoniter
package netutil
import (
"bytes"
"encoding/json"
)
// JsoniterMsgPacker packs and unpacks messages in JSON format
type JSONMsgPacker struct{}
// PackMsg packs message to bytes of JSON format
func (mp JSONMsgPacker) PackMsg(msg interface{}, buf []byte) ([]byte, error) {
buffer := bytes.NewBuffer(buf)
jsonEncoder := json.NewEncoder(buffer)
err := jsonEncoder.Encode(msg)
if err != nil {
return buf, err
}
buf = buffer.Bytes()
return buf[:len(buf)-1], nil // encoder always put '\n' at the end, we trim it
}
// UnpackMsg unpacks bytes of JSON format to message
func (mp JSONMsgPacker) UnpackMsg(data []byte, msg interface{}) error {
err := json.Unmarshal(data, msg)
return err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzpdev/goworld.git
git@gitee.com:wuzpdev/goworld.git
wuzpdev
goworld
goworld
v0.1.2

Search

0d507c66 1850385 C8b1a773 1850385