Skip to content

Commit a17acaf

Browse files
authored
Update go-tips.cn.md
1 parent d2c49ce commit a17acaf

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

go-tips.cn.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@ should.Equal(`{"Field":123}`, string(bytes))
203203
```
204204

205205
# 使用 RegisterTypeEncoder支持time.Time
206+
207+
jsoniter 能够对不是你定义的type自定义JSON编解码方式。比如对于 time.Time 可以用 epoch int64 来序列化
208+
209+
```golang
210+
import "github.com/json-iterator/go/extra"
211+
212+
extra.RegisterTimeAsInt64Codec(time.Microsecond)
213+
output, err := jsoniter.Marshal(time.Unix(1, 1002))
214+
should.Equal("1000001", string(output))
215+
```
216+
217+
如果要自定义的话,参见 `RegisterTimeAsInt64Codec` 的实现代码
218+
206219
# 使用 MarshalText支持非字符串作为key的map
207220
# 使用 json.RawMessage
208221
# 使用 json.Number

0 commit comments

Comments
 (0)