forked from json-iterator/go
/
tuyoo.go
45 lines (40 loc) · 1.1 KB
/
tuyoo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package jsoniter
import "io"
// Marshal2File
// 直接将结果写入到文件当中,避免二次的内存拷贝
func (cfg *frozenConfig) Marshal2File(lineDate int, writefun func(lineDate int, p []byte) error, v interface{}) error {
stream := cfg.BorrowStream(nil)
defer cfg.ReturnStream(stream)
stream.WriteVal(v)
if stream.Error != nil {
return stream.Error
}
// 添加换行符号
stream.writeByte('\n')
return writefun(lineDate, stream.Buffer())
}
func (cfg *frozenConfig) Marshal2Writer(writer io.Writer, v interface{}) error {
stream := cfg.BorrowStream(nil)
defer cfg.ReturnStream(stream)
stream.WriteVal(v)
if stream.Error != nil {
return stream.Error
}
// 添加换行符号
stream.writeByte('\n')
_, err := writer.Write(stream.Buffer())
return err
}
func (cfg *frozenConfig) Marshal2Lines(writer io.Writer, objs []interface{}) error {
stream := cfg.BorrowStream(nil)
defer cfg.ReturnStream(stream)
for _, obj := range objs {
stream.WriteVal(obj)
stream.writeByte('\n')
}
if stream.Error != nil {
return stream.Error
}
_, err := writer.Write(stream.Buffer())
return err
}