-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.go
47 lines (40 loc) · 849 Bytes
/
stream.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
package current
import (
"encoding/json"
"fmt"
)
type UnexpectedTokenError struct {
offset int64
str Streamer
msg string
}
func (ut UnexpectedTokenError) Error() string {
return fmt.Sprintf("invalid token at offset %d decoded by %s, %s", ut.offset, ut.str, ut.msg)
}
var (
arrayStart = json.Delim('[')
arrayEnd = json.Delim(']')
mapStart = json.Delim('{')
mapEnd = json.Delim('}')
)
func requireToken(dec *json.Decoder, expected json.Token, str Streamer) error {
got, err := dec.Token()
if err != nil {
return err
}
if got != expected {
return UnexpectedTokenError{
offset: dec.InputOffset(),
str: str,
msg: fmt.Sprintf("expected %s, got %v", expected, got),
}
}
return nil
}
type Streamer interface {
Stream(*json.Decoder) error
}
type NamedStreamer interface {
Name() string
Streamer
}