-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
59 lines (46 loc) · 966 Bytes
/
json.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
package pipeline
import (
"encoding/gob"
"encoding/json"
"io"
)
type JSONDeserializer struct {
constructor func() Emitter
decoder *json.Decoder
hasNext bool
lastError error
ignoreErrors bool
}
func (j *JSONDeserializer) HasNext() bool {
return j.hasNext
}
func (j *JSONDeserializer) Error() error {
return j.lastError
}
func (j *JSONDeserializer) Next() Emitter {
e := j.constructor()
j.lastError = j.decoder.Decode(e)
if j.lastError != nil {
j.hasNext = false
return nil
}
return e
}
func (j *JSONDeserializer) IgnoreMalformedJSON() *JSONDeserializer {
j.ignoreErrors = true
return j
}
// NewJSONDeserializer
func NewJSONDeserializer(fn func() Emitter) DeserializerFunc {
return func(r io.Reader) Deserializer {
gob.Register(fn())
// Create a new decoder
decoder := json.NewDecoder(r)
return &JSONDeserializer{
constructor: fn,
decoder: decoder,
hasNext: true,
lastError: nil,
}
}
}