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