blob: f58beb9137bf5c0ae15ce76618c7e7ca6f5510be [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -05001package jsoniter
2
3import "fmt"
4
5// ReadNil reads a json object as nil and
6// returns whether it's a nil or not
7func (iter *Iterator) ReadNil() (ret bool) {
8 c := iter.nextToken()
9 if c == 'n' {
10 iter.skipThreeBytes('u', 'l', 'l') // null
11 return true
12 }
13 iter.unreadByte()
14 return false
15}
16
17// ReadBool reads a json object as BoolValue
18func (iter *Iterator) ReadBool() (ret bool) {
19 c := iter.nextToken()
20 if c == 't' {
21 iter.skipThreeBytes('r', 'u', 'e')
22 return true
23 }
24 if c == 'f' {
25 iter.skipFourBytes('a', 'l', 's', 'e')
26 return false
27 }
28 iter.ReportError("ReadBool", "expect t or f, but found "+string([]byte{c}))
29 return
30}
31
32// SkipAndReturnBytes skip next JSON element, and return its content as []byte.
33// The []byte can be kept, it is a copy of data.
34func (iter *Iterator) SkipAndReturnBytes() []byte {
35 iter.startCapture(iter.head)
36 iter.Skip()
37 return iter.stopCapture()
38}
39
40type captureBuffer struct {
41 startedAt int
42 captured []byte
43}
44
45func (iter *Iterator) startCapture(captureStartedAt int) {
46 if iter.captured != nil {
47 panic("already in capture mode")
48 }
49 iter.captureStartedAt = captureStartedAt
50 iter.captured = make([]byte, 0, 32)
51}
52
53func (iter *Iterator) stopCapture() []byte {
54 if iter.captured == nil {
55 panic("not in capture mode")
56 }
57 captured := iter.captured
58 remaining := iter.buf[iter.captureStartedAt:iter.head]
59 iter.captureStartedAt = -1
60 iter.captured = nil
61 if len(captured) == 0 {
62 copied := make([]byte, len(remaining))
63 copy(copied, remaining)
64 return copied
65 }
66 captured = append(captured, remaining...)
67 return captured
68}
69
70// Skip skips a json object and positions to relatively the next json object
71func (iter *Iterator) Skip() {
72 c := iter.nextToken()
73 switch c {
74 case '"':
75 iter.skipString()
76 case 'n':
77 iter.skipThreeBytes('u', 'l', 'l') // null
78 case 't':
79 iter.skipThreeBytes('r', 'u', 'e') // true
80 case 'f':
81 iter.skipFourBytes('a', 'l', 's', 'e') // false
82 case '0':
83 iter.unreadByte()
84 iter.ReadFloat32()
85 case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
86 iter.skipNumber()
87 case '[':
88 iter.skipArray()
89 case '{':
90 iter.skipObject()
91 default:
92 iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
93 return
94 }
95}
96
97func (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})))
100 return
101 }
102 if iter.readByte() != b2 {
103 iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
104 return
105 }
106 if iter.readByte() != b3 {
107 iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
108 return
109 }
110 if iter.readByte() != b4 {
111 iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
112 return
113 }
114}
115
116func (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})))
119 return
120 }
121 if iter.readByte() != b2 {
122 iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
123 return
124 }
125 if iter.readByte() != b3 {
126 iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
127 return
128 }
129}