-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
48 lines (36 loc) · 866 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
package jq
import (
"errors"
"nikand.dev/go/json"
)
type (
// JSONEncoder struct{}
// JSONDecoder reads decoded string, parses it a JSON, and returns the raw value.
JSONDecoder struct {
Buf []byte
}
)
func (f *JSONDecoder) Next(w, r []byte, st int, state State) (_ []byte, i int, _ State, err error) {
var p json.Decoder
st = p.SkipSpaces(r, st)
if st == len(r) {
return w, st, nil, nil
}
f.Buf, i, err = p.DecodeString(r, st, f.Buf[:0])
if err != nil {
return w, i, state, pe(err, i)
}
// log.Printf("JSONDecoder string\n%s", f.Buf)
var raw []byte
for j := p.SkipSpaces(f.Buf, 0); j < len(f.Buf); j = p.SkipSpaces(f.Buf, j) {
raw, j, err = p.Raw(f.Buf, j)
if errors.Is(err, json.ErrEndOfBuffer) {
break
}
if err != nil {
return w, st, state, pe(pe(err, j), st)
}
w = append(w, raw...)
}
return w, i, nil, nil
}