Skip to content

Commit

Permalink
update README, add usage about transmit JSON format data
Browse files Browse the repository at this point in the history
  • Loading branch information
lim-yoona committed Oct 9, 2023
1 parent c9497bd commit 1b4f693
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,37 @@ func main() {
}
```

### 支持JSON

```go
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}

// Create a packager
mp := msgpack.NewMsgPack(8, tcpConn)

// data JSON Marshal
data := &Person{
Name: "jack",
Age: 20,
}
dataJSON, _ := json.Marshal(data)

// Pack a message
msgSend := msgpack.NewMessage(0, uint32(len(dataJSON)), dataJSON)
msgSendByte, _ := mpClient.Pack(msgSend)
num, err := tcpConn.Write(msgSendByte)

// Unpack a message
msgRsv, err := mp.Unpack()

// JSON UnMarshal
var dataRsv Person
json.Unmarshal(msgRsv.GetMsgData(), &dataRsv)
```

## 示例

这有一些 [示例](https://github.com/lim-yoona/msgpack/tree/main/example)
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ func main() {
}
```

### Support JSON

```go
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}

// Create a packager
mp := msgpack.NewMsgPack(8, tcpConn)

// data JSON Marshal
data := &Person{
Name: "jack",
Age: 20,
}
dataJSON, _ := json.Marshal(data)

// Pack a message
msgSend := msgpack.NewMessage(0, uint32(len(dataJSON)), dataJSON)
msgSendByte, _ := mpClient.Pack(msgSend)
num, err := tcpConn.Write(msgSendByte)

// Unpack a message
msgRsv, err := mp.Unpack()

// JSON UnMarshal
var dataRsv Person
json.Unmarshal(msgRsv.GetMsgData(), &dataRsv)
```

## Examples

Here are some [Examples](https://github.com/lim-yoona/msgpack/tree/main/example).
Expand Down

0 comments on commit 1b4f693

Please sign in to comment.