Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package json |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 9 | "fmt" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 10 | "io" |
| 11 | "regexp" |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 12 | "strconv" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 13 | "unicode/utf8" |
| 14 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 15 | "google.golang.org/protobuf/internal/errors" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 18 | // call specifies which Decoder method was invoked. |
| 19 | type call uint8 |
| 20 | |
| 21 | const ( |
| 22 | readCall call = iota |
| 23 | peekCall |
| 24 | ) |
| 25 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 26 | // Decoder is a token-based JSON decoder. |
| 27 | type Decoder struct { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 28 | // lastCall is last method called, either readCall or peekCall. |
| 29 | // Initial value is readCall. |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 30 | lastCall call |
| 31 | |
| 32 | // value contains the last read value. |
| 33 | value Value |
| 34 | |
| 35 | // err contains the last read error. |
| 36 | err error |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 37 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 38 | // startStack is a stack containing StartObject and StartArray types. The |
| 39 | // top of stack represents the object or the array the current value is |
| 40 | // directly located in. |
| 41 | startStack []Type |
| 42 | |
| 43 | // orig is used in reporting line and column. |
| 44 | orig []byte |
| 45 | // in contains the unconsumed input. |
| 46 | in []byte |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 49 | // NewDecoder returns a Decoder to read the given []byte. |
| 50 | func NewDecoder(b []byte) *Decoder { |
| 51 | return &Decoder{orig: b, in: b} |
| 52 | } |
| 53 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 54 | // Peek looks ahead and returns the next JSON type without advancing a read. |
| 55 | func (d *Decoder) Peek() Type { |
| 56 | defer func() { d.lastCall = peekCall }() |
| 57 | if d.lastCall == readCall { |
| 58 | d.value, d.err = d.Read() |
| 59 | } |
| 60 | return d.value.typ |
| 61 | } |
| 62 | |
| 63 | // Read returns the next JSON value. It will return an error if there is no |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 64 | // valid value. For String types containing invalid UTF8 characters, a non-fatal |
| 65 | // error is returned and caller can call Read for the next value. |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 66 | func (d *Decoder) Read() (Value, error) { |
| 67 | defer func() { d.lastCall = readCall }() |
| 68 | if d.lastCall == peekCall { |
| 69 | return d.value, d.err |
| 70 | } |
| 71 | |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 72 | value, err := d.parseNext() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 73 | if err != nil { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 74 | return Value{}, err |
| 75 | } |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 76 | n := value.size |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 77 | |
| 78 | switch value.typ { |
| 79 | case EOF: |
| 80 | if len(d.startStack) != 0 || |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 81 | d.value.typ&Null|Bool|Number|String|EndObject|EndArray == 0 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 82 | return Value{}, io.ErrUnexpectedEOF |
| 83 | } |
| 84 | |
| 85 | case Null: |
| 86 | if !d.isValueNext() { |
| 87 | return Value{}, d.newSyntaxError("unexpected value null") |
| 88 | } |
| 89 | |
| 90 | case Bool, Number: |
| 91 | if !d.isValueNext() { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 92 | return Value{}, d.newSyntaxError("unexpected value %v", value.Raw()) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | case String: |
| 96 | if d.isValueNext() { |
| 97 | break |
| 98 | } |
| 99 | // Check if this is for an object name. |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 100 | if d.value.typ&(StartObject|comma) == 0 { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 101 | return Value{}, d.newSyntaxError("unexpected value %v", value.Raw()) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 102 | } |
| 103 | d.in = d.in[n:] |
| 104 | d.consume(0) |
Damien Neil | 5ba0c29 | 2019-12-21 09:45:00 -0800 | [diff] [blame] | 105 | if len(d.in) == 0 { |
| 106 | return Value{}, d.newSyntaxError(`unexpected EOF, missing ":" after object name`) |
| 107 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 108 | if c := d.in[0]; c != ':' { |
| 109 | return Value{}, d.newSyntaxError(`unexpected character %v, missing ":" after object name`, string(c)) |
| 110 | } |
| 111 | n = 1 |
| 112 | value.typ = Name |
| 113 | |
| 114 | case StartObject, StartArray: |
| 115 | if !d.isValueNext() { |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 116 | return Value{}, d.newSyntaxError("unexpected character %v", value.Raw()) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 117 | } |
| 118 | d.startStack = append(d.startStack, value.typ) |
| 119 | |
| 120 | case EndObject: |
| 121 | if len(d.startStack) == 0 || |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 122 | d.value.typ == comma || |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 123 | d.startStack[len(d.startStack)-1] != StartObject { |
| 124 | return Value{}, d.newSyntaxError("unexpected character }") |
| 125 | } |
| 126 | d.startStack = d.startStack[:len(d.startStack)-1] |
| 127 | |
| 128 | case EndArray: |
| 129 | if len(d.startStack) == 0 || |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 130 | d.value.typ == comma || |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 131 | d.startStack[len(d.startStack)-1] != StartArray { |
| 132 | return Value{}, d.newSyntaxError("unexpected character ]") |
| 133 | } |
| 134 | d.startStack = d.startStack[:len(d.startStack)-1] |
| 135 | |
| 136 | case comma: |
| 137 | if len(d.startStack) == 0 || |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 138 | d.value.typ&(Null|Bool|Number|String|EndObject|EndArray) == 0 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 139 | return Value{}, d.newSyntaxError("unexpected character ,") |
| 140 | } |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 141 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 142 | |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 143 | // Update d.value only after validating value to be in the right sequence. |
| 144 | d.value = value |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 145 | d.in = d.in[n:] |
| 146 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 147 | if d.value.typ == comma { |
| 148 | return d.Read() |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 149 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 150 | return value, nil |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 153 | // Any sequence that looks like a non-delimiter (for error reporting). |
| 154 | var errRegexp = regexp.MustCompile(`^([-+._a-zA-Z0-9]{1,32}|.)`) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 155 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 156 | // parseNext parses for the next JSON value. It returns a Value object for |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 157 | // different types, except for Name. It does not handle whether the next value |
| 158 | // is in a valid sequence or not. |
| 159 | func (d *Decoder) parseNext() (value Value, err error) { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 160 | // Trim leading spaces. |
| 161 | d.consume(0) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 162 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 163 | in := d.in |
| 164 | if len(in) == 0 { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 165 | return d.newValue(EOF, nil, 0), nil |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 166 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 167 | |
| 168 | switch in[0] { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 169 | case 'n': |
| 170 | n := matchWithDelim("null", in) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 171 | if n == 0 { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 172 | return Value{}, d.newSyntaxError("invalid value %s", errRegexp.Find(in)) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 173 | } |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 174 | return d.newValue(Null, in, n), nil |
| 175 | |
| 176 | case 't': |
| 177 | n := matchWithDelim("true", in) |
| 178 | if n == 0 { |
| 179 | return Value{}, d.newSyntaxError("invalid value %s", errRegexp.Find(in)) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 180 | } |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 181 | return d.newBoolValue(in, n, true), nil |
| 182 | |
| 183 | case 'f': |
| 184 | n := matchWithDelim("false", in) |
| 185 | if n == 0 { |
| 186 | return Value{}, d.newSyntaxError("invalid value %s", errRegexp.Find(in)) |
| 187 | } |
| 188 | return d.newBoolValue(in, n, false), nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 189 | |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 190 | case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 191 | n, ok := consumeNumber(in) |
| 192 | if !ok { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 193 | return Value{}, d.newSyntaxError("invalid number %s", errRegexp.Find(in)) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 194 | } |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 195 | return d.newValue(Number, in, n), nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 196 | |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 197 | case '"': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 198 | s, n, err := d.parseString(in) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 199 | if err != nil { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 200 | return Value{}, err |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 201 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 202 | return d.newStringValue(in, n, s), nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 203 | |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 204 | case '{': |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 205 | return d.newValue(StartObject, in, 1), nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 206 | |
| 207 | case '}': |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 208 | return d.newValue(EndObject, in, 1), nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 209 | |
| 210 | case '[': |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 211 | return d.newValue(StartArray, in, 1), nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 212 | |
| 213 | case ']': |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 214 | return d.newValue(EndArray, in, 1), nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 215 | |
| 216 | case ',': |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 217 | return d.newValue(comma, in, 1), nil |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 218 | } |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 219 | return Value{}, d.newSyntaxError("invalid value %s", errRegexp.Find(in)) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 222 | // position returns line and column number of index in given orig slice. |
| 223 | func position(orig []byte, idx int) (int, int) { |
| 224 | b := orig[:idx] |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 225 | line := bytes.Count(b, []byte("\n")) + 1 |
| 226 | if i := bytes.LastIndexByte(b, '\n'); i >= 0 { |
| 227 | b = b[i+1:] |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 228 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 229 | column := utf8.RuneCount(b) + 1 // ignore multi-rune characters |
| 230 | return line, column |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 233 | // newSyntaxError returns an error with line and column information useful for |
| 234 | // syntax errors. |
| 235 | func (d *Decoder) newSyntaxError(f string, x ...interface{}) error { |
| 236 | e := errors.New(f, x...) |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 237 | line, column := position(d.orig, len(d.orig)-len(d.in)) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 238 | return errors.New("syntax error (line %d:%d): %v", line, column, e) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 241 | // matchWithDelim matches s with the input b and verifies that the match |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 242 | // terminates with a delimiter of some form (e.g., r"[^-+_.a-zA-Z0-9]"). |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 243 | // As a special case, EOF is considered a delimiter. It returns the length of s |
| 244 | // if there is a match, else 0. |
| 245 | func matchWithDelim(s string, b []byte) int { |
| 246 | if !bytes.HasPrefix(b, []byte(s)) { |
| 247 | return 0 |
| 248 | } |
| 249 | |
| 250 | n := len(s) |
| 251 | if n < len(b) && isNotDelim(b[n]) { |
| 252 | return 0 |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 253 | } |
| 254 | return n |
| 255 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 256 | |
| 257 | // isNotDelim returns true if given byte is a not delimiter character. |
| 258 | func isNotDelim(c byte) bool { |
| 259 | return (c == '-' || c == '+' || c == '.' || c == '_' || |
| 260 | ('a' <= c && c <= 'z') || |
| 261 | ('A' <= c && c <= 'Z') || |
| 262 | ('0' <= c && c <= '9')) |
| 263 | } |
| 264 | |
| 265 | // consume consumes n bytes of input and any subsequent whitespace. |
| 266 | func (d *Decoder) consume(n int) { |
| 267 | d.in = d.in[n:] |
| 268 | for len(d.in) > 0 { |
| 269 | switch d.in[0] { |
| 270 | case ' ', '\n', '\r', '\t': |
| 271 | d.in = d.in[1:] |
| 272 | default: |
| 273 | return |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // isValueNext returns true if next type should be a JSON value: Null, |
| 279 | // Number, String or Bool. |
| 280 | func (d *Decoder) isValueNext() bool { |
| 281 | if len(d.startStack) == 0 { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 282 | return d.value.typ == 0 |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | start := d.startStack[len(d.startStack)-1] |
| 286 | switch start { |
| 287 | case StartObject: |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 288 | return d.value.typ&Name != 0 |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 289 | case StartArray: |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 290 | return d.value.typ&(StartArray|comma) != 0 |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 291 | } |
| 292 | panic(fmt.Sprintf( |
| 293 | "unreachable logic in Decoder.isValueNext, lastType: %v, startStack: %v", |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 294 | d.value.typ, start)) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 297 | // newValue constructs a Value for given Type. |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 298 | func (d *Decoder) newValue(typ Type, input []byte, size int) Value { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 299 | return Value{ |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 300 | typ: typ, |
| 301 | input: d.orig, |
| 302 | start: len(d.orig) - len(input), |
| 303 | size: size, |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | // newBoolValue constructs a Value for a JSON boolean. |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 308 | func (d *Decoder) newBoolValue(input []byte, size int, b bool) Value { |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 309 | return Value{ |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 310 | typ: Bool, |
| 311 | input: d.orig, |
| 312 | start: len(d.orig) - len(input), |
| 313 | size: size, |
| 314 | boo: b, |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | // newStringValue constructs a Value for a JSON string. |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 319 | func (d *Decoder) newStringValue(input []byte, size int, s string) Value { |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 320 | return Value{ |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 321 | typ: String, |
| 322 | input: d.orig, |
| 323 | start: len(d.orig) - len(input), |
| 324 | size: size, |
| 325 | str: s, |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 329 | // Clone returns a copy of the Decoder for use in reading ahead the next JSON |
| 330 | // object, array or other values without affecting current Decoder. |
| 331 | func (d *Decoder) Clone() *Decoder { |
| 332 | ret := *d |
| 333 | ret.startStack = append([]Type(nil), ret.startStack...) |
| 334 | return &ret |
| 335 | } |
| 336 | |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 337 | // Value provides a parsed JSON type and value. |
| 338 | // |
| 339 | // The original input slice is stored in this struct in order to compute for |
| 340 | // position as needed. The raw JSON value is derived from the original input |
| 341 | // slice given start and size. |
| 342 | // |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 343 | // For JSON boolean and string, it holds the converted value in boo and str |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 344 | // fields respectively. For JSON number, the raw JSON value holds a valid number |
| 345 | // which is converted only in Int or Float. Other JSON types do not require any |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 346 | // additional data. |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 347 | type Value struct { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 348 | typ Type |
| 349 | input []byte |
| 350 | start int |
| 351 | size int |
| 352 | boo bool |
| 353 | str string |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | func (v Value) newError(f string, x ...interface{}) error { |
| 357 | e := errors.New(f, x...) |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 358 | line, col := v.Position() |
| 359 | return errors.New("error (line %d:%d): %v", line, col, e) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Type returns the JSON type. |
| 363 | func (v Value) Type() Type { |
| 364 | return v.typ |
| 365 | } |
| 366 | |
| 367 | // Position returns the line and column of the value. |
| 368 | func (v Value) Position() (int, int) { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 369 | return position(v.input, v.start) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // Bool returns the bool value if token is Bool, else it will return an error. |
| 373 | func (v Value) Bool() (bool, error) { |
| 374 | if v.typ != Bool { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 375 | return false, v.newError("%s is not a bool", v.Raw()) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 376 | } |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 377 | return v.boo, nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // String returns the string value for a JSON string token or the read value in |
| 381 | // string if token is not a string. |
| 382 | func (v Value) String() string { |
| 383 | if v.typ != String { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 384 | return v.Raw() |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 385 | } |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 386 | return v.str |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // Name returns the object name if token is Name, else it will return an error. |
| 390 | func (v Value) Name() (string, error) { |
| 391 | if v.typ != Name { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 392 | return "", v.newError("%s is not an object name", v.Raw()) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 393 | } |
Herbie Ong | 670d808 | 2019-03-31 19:10:33 -0700 | [diff] [blame] | 394 | return v.str, nil |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 397 | // Raw returns the read value in string. |
| 398 | func (v Value) Raw() string { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 399 | return string(v.input[v.start : v.start+v.size]) |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 402 | // Float returns the floating-point number if token is Number, else it will |
| 403 | // return an error. |
| 404 | // |
| 405 | // The floating-point precision is specified by the bitSize parameter: 32 for |
| 406 | // float32 or 64 for float64. If bitSize=32, the result still has type float64, |
| 407 | // but it will be convertible to float32 without changing its value. It will |
| 408 | // return an error if the number exceeds the floating point limits for given |
| 409 | // bitSize. |
| 410 | func (v Value) Float(bitSize int) (float64, error) { |
| 411 | if v.typ != Number { |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 412 | return 0, v.newError("%s is not a number", v.Raw()) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 413 | } |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 414 | f, err := strconv.ParseFloat(v.Raw(), bitSize) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 415 | if err != nil { |
| 416 | return 0, v.newError("%v", err) |
| 417 | } |
| 418 | return f, nil |
| 419 | } |
| 420 | |
| 421 | // Int returns the signed integer number if token is Number, else it will |
| 422 | // return an error. |
| 423 | // |
| 424 | // The given bitSize specifies the integer type that the result must fit into. |
| 425 | // It returns an error if the number is not an integer value or if the result |
| 426 | // exceeds the limits for given bitSize. |
| 427 | func (v Value) Int(bitSize int) (int64, error) { |
| 428 | s, err := v.getIntStr() |
| 429 | if err != nil { |
| 430 | return 0, err |
| 431 | } |
| 432 | n, err := strconv.ParseInt(s, 10, bitSize) |
| 433 | if err != nil { |
| 434 | return 0, v.newError("%v", err) |
| 435 | } |
| 436 | return n, nil |
| 437 | } |
| 438 | |
| 439 | // Uint returns the signed integer number if token is Number, else it will |
| 440 | // return an error. |
| 441 | // |
| 442 | // The given bitSize specifies the unsigned integer type that the result must |
| 443 | // fit into. It returns an error if the number is not an unsigned integer value |
| 444 | // or if the result exceeds the limits for given bitSize. |
| 445 | func (v Value) Uint(bitSize int) (uint64, error) { |
| 446 | s, err := v.getIntStr() |
| 447 | if err != nil { |
| 448 | return 0, err |
| 449 | } |
| 450 | n, err := strconv.ParseUint(s, 10, bitSize) |
| 451 | if err != nil { |
| 452 | return 0, v.newError("%v", err) |
| 453 | } |
| 454 | return n, nil |
| 455 | } |
| 456 | |
| 457 | func (v Value) getIntStr() (string, error) { |
| 458 | if v.typ != Number { |
| 459 | return "", v.newError("%s is not a number", v.input) |
| 460 | } |
Herbie Ong | decef41 | 2019-04-17 15:47:43 -0700 | [diff] [blame] | 461 | parts, ok := parseNumber(v.input[v.start : v.start+v.size]) |
Herbie Ong | a342195 | 2019-03-21 18:12:26 -0700 | [diff] [blame] | 462 | if !ok { |
| 463 | return "", v.newError("%s is not a number", v.input) |
| 464 | } |
| 465 | num, ok := normalizeToIntString(parts) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 466 | if !ok { |
| 467 | return "", v.newError("cannot convert %s to integer", v.input) |
| 468 | } |
| 469 | return num, nil |
| 470 | } |