5 // ReadNil reads a json object as nil and
6 // returns whether it's a nil or not
7 func (iter *Iterator) ReadNil() (ret bool) {
10 iter.skipThreeBytes('u', 'l', 'l') // null
17 // ReadBool reads a json object as BoolValue
18 func (iter *Iterator) ReadBool() (ret bool) {
21 iter.skipThreeBytes('r', 'u', 'e')
25 iter.skipFourBytes('a', 'l', 's', 'e')
28 iter.ReportError("ReadBool", "expect t or f, but found "+string([]byte{c}))
32 // SkipAndReturnBytes skip next JSON element, and return its content as []byte.
33 // The []byte can be kept, it is a copy of data.
34 func (iter *Iterator) SkipAndReturnBytes() []byte {
35 iter.startCapture(iter.head)
37 return iter.stopCapture()
40 type captureBuffer struct {
45 func (iter *Iterator) startCapture(captureStartedAt int) {
46 if iter.captured != nil {
47 panic("already in capture mode")
49 iter.captureStartedAt = captureStartedAt
50 iter.captured = make([]byte, 0, 32)
53 func (iter *Iterator) stopCapture() []byte {
54 if iter.captured == nil {
55 panic("not in capture mode")
57 captured := iter.captured
58 remaining := iter.buf[iter.captureStartedAt:iter.head]
59 iter.captureStartedAt = -1
61 if len(captured) == 0 {
62 copied := make([]byte, len(remaining))
63 copy(copied, remaining)
66 captured = append(captured, remaining...)
70 // Skip skips a json object and positions to relatively the next json object
71 func (iter *Iterator) Skip() {
77 iter.skipThreeBytes('u', 'l', 'l') // null
79 iter.skipThreeBytes('r', 'u', 'e') // true
81 iter.skipFourBytes('a', 'l', 's', 'e') // false
85 case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
92 iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
97 func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) {
98 if iter.readByte() != b1 {
99 iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
102 if iter.readByte() != b2 {
103 iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
106 if iter.readByte() != b3 {
107 iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
110 if iter.readByte() != b4 {
111 iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
116 func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) {
117 if iter.readByte() != b1 {
118 iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
121 if iter.readByte() != b2 {
122 iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
125 if iter.readByte() != b3 {
126 iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))