Next returns a new tokenizer pointing at the next token, or the zero-value of Tokenizer if the end of the json input has been reached. If the tokenizer encounters malformed json while reading the input the method sets t.Err to an error describing the issue, and returns false. Once an error has been
()
| 109 | // has been encountered, the tokenizer will always fail until its input is |
| 110 | // cleared by a call to its Reset method. |
| 111 | func (t *Tokenizer) Next() bool { |
| 112 | if t.Err != nil { |
| 113 | return false |
| 114 | } |
| 115 | |
| 116 | // Inlined code of the skipSpaces function, this give a ~15% speed boost. |
| 117 | i := 0 |
| 118 | skipLoop: |
| 119 | for _, c := range t.json { |
| 120 | switch c { |
| 121 | case sp, ht, nl, cr: |
| 122 | i++ |
| 123 | default: |
| 124 | break skipLoop |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if i > 0 { |
| 129 | t.json = t.json[i:] |
| 130 | } |
| 131 | |
| 132 | if len(t.json) == 0 { |
| 133 | t.Reset(nil) |
| 134 | return false |
| 135 | } |
| 136 | |
| 137 | var kind Kind |
| 138 | switch t.json[0] { |
| 139 | case '"': |
| 140 | t.Delim = 0 |
| 141 | t.Value, t.json, kind, t.Err = t.parseString(t.json) |
| 142 | case 'n': |
| 143 | t.Delim = 0 |
| 144 | t.Value, t.json, kind, t.Err = t.parseNull(t.json) |
| 145 | case 't': |
| 146 | t.Delim = 0 |
| 147 | t.Value, t.json, kind, t.Err = t.parseTrue(t.json) |
| 148 | case 'f': |
| 149 | t.Delim = 0 |
| 150 | t.Value, t.json, kind, t.Err = t.parseFalse(t.json) |
| 151 | case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 152 | t.Delim = 0 |
| 153 | t.Value, t.json, kind, t.Err = t.parseNumber(t.json) |
| 154 | case '{', '}', '[', ']', ':', ',': |
| 155 | t.Delim, t.Value, t.json = Delim(t.json[0]), t.json[:1], t.json[1:] |
| 156 | switch t.Delim { |
| 157 | case '{': |
| 158 | kind = Object |
| 159 | case '[': |
| 160 | kind = Array |
| 161 | } |
| 162 | default: |
| 163 | t.Delim = 0 |
| 164 | t.Value, t.json, t.Err = t.json[:1], t.json[1:], syntaxError(t.json, "expected token but found '%c'", t.json[0]) |
| 165 | } |
| 166 | |
| 167 | t.Depth = t.depth() |
| 168 | t.Index = t.index() |