-
Notifications
You must be signed in to change notification settings - Fork 4
/
jx.go
66 lines (57 loc) · 1.11 KB
/
jx.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package jx implements RFC 7159 json encoding and decoding.
package jx
import (
"sync"
)
// Valid reports whether data is valid json.
func Valid(data []byte) bool {
d := GetDecoder()
defer PutDecoder(d)
d.ResetBytes(data)
return d.Validate() == nil
}
var (
encPool = &sync.Pool{
New: func() interface{} {
return &Encoder{}
},
}
writerPool = &sync.Pool{
New: func() interface{} {
return &Writer{}
},
}
decPool = &sync.Pool{
New: func() interface{} {
return &Decoder{}
},
}
)
// GetDecoder gets *Decoder from pool.
func GetDecoder() *Decoder {
return decPool.Get().(*Decoder)
}
// PutDecoder puts *Decoder into pool.
func PutDecoder(d *Decoder) {
d.Reset(nil)
decPool.Put(d)
}
// GetEncoder returns *Encoder from pool.
func GetEncoder() *Encoder {
return encPool.Get().(*Encoder)
}
// PutEncoder puts *Encoder to pool
func PutEncoder(e *Encoder) {
e.Reset()
e.SetIdent(0)
encPool.Put(e)
}
// GetWriter returns *Writer from pool.
func GetWriter() *Writer {
return writerPool.Get().(*Writer)
}
// PutWriter puts *Writer to pool
func PutWriter(e *Writer) {
e.Reset()
writerPool.Put(e)
}