| 1 |
package json |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"math" |
| 7 |
"math/bits" |
| 8 |
"reflect" |
| 9 |
"unicode" |
| 10 |
"unicode/utf16" |
| 11 |
"unicode/utf8" |
| 12 |
|
| 13 |
"github.com/segmentio/encoding/ascii" |
| 14 |
) |
| 15 |
|
| 16 |
// All spaces characters defined in the json specification. |
| 17 |
const ( |
| 18 |
sp = ' ' |
| 19 |
ht = '\t' |
| 20 |
nl = '\n' |
| 21 |
cr = '\r' |
| 22 |
) |
| 23 |
|
| 24 |
func internalParseFlags(b []byte) (flags ParseFlags) { |
| 25 |
// Don't consider surrounding whitespace |
| 26 |
b = skipSpaces(b) |
| 27 |
b = trimTrailingSpaces(b) |
| 28 |
if ascii.ValidPrint(b) { |
| 29 |
flags |= validAsciiPrint |
| 30 |
} |
| 31 |
if bytes.IndexByte(b, '\\') == -1 { |
| 32 |
flags |= noBackslash |
| 33 |
} |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
func skipSpaces(b []byte) []byte { |
| 38 |
if len(b) > 0 && b[0] <= 0x20 { |
| 39 |
b, _ = skipSpacesN(b) |
| 40 |
} |
| 41 |
return b |
| 42 |
} |
| 43 |
|
| 44 |
func skipSpacesN(b []byte) ([]byte, int) { |
| 45 |
for i := range b { |
| 46 |
switch b[i] { |
| 47 |
case sp, ht, nl, cr: |
| 48 |
default: |
| 49 |
return b[i:], i |
| 50 |
} |
| 51 |
} |
| 52 |
return nil, 0 |
| 53 |
} |
| 54 |
|
| 55 |
func trimTrailingSpaces(b []byte) []byte { |
| 56 |
if len(b) > 0 && b[len(b)-1] <= 0x20 { |
| 57 |
b = trimTrailingSpacesN(b) |
| 58 |
} |
| 59 |
return b |
| 60 |
} |
| 61 |
|
| 62 |
func trimTrailingSpacesN(b []byte) []byte { |
| 63 |
i := len(b) - 1 |
| 64 |
loop: |
| 65 |
for ; i >= 0; i-- { |
| 66 |
switch b[i] { |
| 67 |
case sp, ht, nl, cr: |
| 68 |
default: |
| 69 |
break loop |
| 70 |
} |
| 71 |
} |
| 72 |
return b[:i+1] |
| 73 |
} |
| 74 |
|
| 75 |
// parseInt parses a decimal representation of an int64 from b. |
| 76 |
// |
| 77 |
// The function is equivalent to calling strconv.ParseInt(string(b), 10, 64) but |
| 78 |
// it prevents Go from making a memory allocation for converting a byte slice to |
| 79 |
// a string (escape analysis fails due to the error returned by strconv.ParseInt). |
| 80 |
// |
| 81 |
// Because it only works with base 10 the function is also significantly faster |
| 82 |
// than strconv.ParseInt. |
| 83 |
func (d decoder) parseInt(b []byte, t reflect.Type) (int64, []byte, error) { |
| 84 |
var value int64 |
| 85 |
var count int |
| 86 |
|
| 87 |
if len(b) == 0 { |
| 88 |
return 0, b, syntaxError(b, "cannot decode integer from an empty input") |
| 89 |
} |
| 90 |
|
| 91 |
if b[0] == '-' { |
| 92 |
const max = math.MinInt64 |
| 93 |
const lim = max / 10 |
| 94 |
|
| 95 |
if len(b) == 1 { |
| 96 |
return 0, b, syntaxError(b, "cannot decode integer from '-'") |
| 97 |
} |
| 98 |
|
| 99 |
if len(b) > 2 && b[1] == '0' && '0' <= b[2] && b[2] <= '9' { |
| 100 |
return 0, b, syntaxError(b, "invalid leading character '0' in integer") |
| 101 |
} |
| 102 |
|
| 103 |
for _, c := range b[1:] { |
| 104 |
if c < '0' || c > '9' { |
| 105 |
if count == 0 { |
| 106 |
b, err := d.inputError(b, t) |
| 107 |
return 0, b, err |
| 108 |
} |
| 109 |
break |
| 110 |
} |
| 111 |
|
| 112 |
if value < lim { |
| 113 |
return 0, b, unmarshalOverflow(b, t) |
| 114 |
} |
| 115 |
|
| 116 |
value *= 10 |
| 117 |
x := int64(c - '0') |
| 118 |
|
| 119 |
if value < (max + x) { |
| 120 |
return 0, b, unmarshalOverflow(b, t) |
| 121 |
} |
| 122 |
|
| 123 |
value -= x |
| 124 |
count++ |
| 125 |
} |
| 126 |
|
| 127 |
count++ |
| 128 |
} else { |
| 129 |
if len(b) > 1 && b[0] == '0' && '0' <= b[1] && b[1] <= '9' { |
| 130 |
return 0, b, syntaxError(b, "invalid leading character '0' in integer") |
| 131 |
} |
| 132 |
|
| 133 |
for ; count < len(b) && b[count] >= '0' && b[count] <= '9'; count++ { |
| 134 |
x := int64(b[count] - '0') |
| 135 |
next := value*10 + x |
| 136 |
if next < value { |
| 137 |
return 0, b, unmarshalOverflow(b, t) |
| 138 |
} |
| 139 |
value = next |
| 140 |
} |
| 141 |
|
| 142 |
if count == 0 { |
| 143 |
b, err := d.inputError(b, t) |
| 144 |
return 0, b, err |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if count < len(b) { |
| 149 |
switch b[count] { |
| 150 |
case '.', 'e', 'E': // was this actually a float? |
| 151 |
v, r, _, err := d.parseNumber(b) |
| 152 |
if err != nil { |
| 153 |
v, r = b[:count+1], b[count+1:] |
| 154 |
} |
| 155 |
return 0, r, unmarshalTypeError(v, t) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return value, b[count:], nil |
| 160 |
} |
| 161 |
|
| 162 |
// parseUint is like parseInt but for unsigned integers. |
| 163 |
func (d decoder) parseUint(b []byte, t reflect.Type) (uint64, []byte, error) { |
| 164 |
var value uint64 |
| 165 |
var count int |
| 166 |
|
| 167 |
if len(b) == 0 { |
| 168 |
return 0, b, syntaxError(b, "cannot decode integer value from an empty input") |
| 169 |
} |
| 170 |
|
| 171 |
if len(b) > 1 && b[0] == '0' && '0' <= b[1] && b[1] <= '9' { |
| 172 |
return 0, b, syntaxError(b, "invalid leading character '0' in integer") |
| 173 |
} |
| 174 |
|
| 175 |
for ; count < len(b) && b[count] >= '0' && b[count] <= '9'; count++ { |
| 176 |
x := uint64(b[count] - '0') |
| 177 |
next := value*10 + x |
| 178 |
if next < value { |
| 179 |
return 0, b, unmarshalOverflow(b, t) |
| 180 |
} |
| 181 |
value = next |
| 182 |
} |
| 183 |
|
| 184 |
if count == 0 { |
| 185 |
b, err := d.inputError(b, t) |
| 186 |
return 0, b, err |
| 187 |
} |
| 188 |
|
| 189 |
if count < len(b) { |
| 190 |
switch b[count] { |
| 191 |
case '.', 'e', 'E': // was this actually a float? |
| 192 |
v, r, _, err := d.parseNumber(b) |
| 193 |
if err != nil { |
| 194 |
v, r = b[:count+1], b[count+1:] |
| 195 |
} |
| 196 |
return 0, r, unmarshalTypeError(v, t) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return value, b[count:], nil |
| 201 |
} |
| 202 |
|
| 203 |
// parseUintHex parses a hexadecimanl representation of a uint64 from b. |
| 204 |
// |
| 205 |
// The function is equivalent to calling strconv.ParseUint(string(b), 16, 64) but |
| 206 |
// it prevents Go from making a memory allocation for converting a byte slice to |
| 207 |
// a string (escape analysis fails due to the error returned by strconv.ParseUint). |
| 208 |
// |
| 209 |
// Because it only works with base 16 the function is also significantly faster |
| 210 |
// than strconv.ParseUint. |
| 211 |
func (d decoder) parseUintHex(b []byte) (uint64, []byte, error) { |
| 212 |
const max = math.MaxUint64 |
| 213 |
const lim = max / 0x10 |
| 214 |
|
| 215 |
var value uint64 |
| 216 |
var count int |
| 217 |
|
| 218 |
if len(b) == 0 { |
| 219 |
return 0, b, syntaxError(b, "cannot decode hexadecimal value from an empty input") |
| 220 |
} |
| 221 |
|
| 222 |
parseLoop: |
| 223 |
for i, c := range b { |
| 224 |
var x uint64 |
| 225 |
|
| 226 |
switch { |
| 227 |
case c >= '0' && c <= '9': |
| 228 |
x = uint64(c - '0') |
| 229 |
|
| 230 |
case c >= 'A' && c <= 'F': |
| 231 |
x = uint64(c-'A') + 0xA |
| 232 |
|
| 233 |
case c >= 'a' && c <= 'f': |
| 234 |
x = uint64(c-'a') + 0xA |
| 235 |
|
| 236 |
default: |
| 237 |
if i == 0 { |
| 238 |
return 0, b, syntaxError(b, "expected hexadecimal digit but found '%c'", c) |
| 239 |
} |
| 240 |
break parseLoop |
| 241 |
} |
| 242 |
|
| 243 |
if value > lim { |
| 244 |
return 0, b, syntaxError(b, "hexadecimal value out of range") |
| 245 |
} |
| 246 |
|
| 247 |
if value *= 0x10; value > (max - x) { |
| 248 |
return 0, b, syntaxError(b, "hexadecimal value out of range") |
| 249 |
} |
| 250 |
|
| 251 |
value += x |
| 252 |
count++ |
| 253 |
} |
| 254 |
|
| 255 |
return value, b[count:], nil |
| 256 |
} |
| 257 |
|
| 258 |
func (d decoder) parseNull(b []byte) ([]byte, []byte, Kind, error) { |
| 259 |
if hasNullPrefix(b) { |
| 260 |
return b[:4], b[4:], Null, nil |
| 261 |
} |
| 262 |
if len(b) < 4 { |
| 263 |
return nil, b[len(b):], Undefined, unexpectedEOF(b) |
| 264 |
} |
| 265 |
return nil, b, Undefined, syntaxError(b, "expected 'null' but found invalid token") |
| 266 |
} |
| 267 |
|
| 268 |
func (d decoder) parseTrue(b []byte) ([]byte, []byte, Kind, error) { |
| 269 |
if hasTruePrefix(b) { |
| 270 |
return b[:4], b[4:], True, nil |
| 271 |
} |
| 272 |
if len(b) < 4 { |
| 273 |
return nil, b[len(b):], Undefined, unexpectedEOF(b) |
| 274 |
} |
| 275 |
return nil, b, Undefined, syntaxError(b, "expected 'true' but found invalid token") |
| 276 |
} |
| 277 |
|
| 278 |
func (d decoder) parseFalse(b []byte) ([]byte, []byte, Kind, error) { |
| 279 |
if hasFalsePrefix(b) { |
| 280 |
return b[:5], b[5:], False, nil |
| 281 |
} |
| 282 |
if len(b) < 5 { |
| 283 |
return nil, b[len(b):], Undefined, unexpectedEOF(b) |
| 284 |
} |
| 285 |
return nil, b, Undefined, syntaxError(b, "expected 'false' but found invalid token") |
| 286 |
} |
| 287 |
|
| 288 |
func (d decoder) parseNumber(b []byte) (v, r []byte, kind Kind, err error) { |
| 289 |
if len(b) == 0 { |
| 290 |
r, err = b, unexpectedEOF(b) |
| 291 |
return |
| 292 |
} |
| 293 |
|
| 294 |
// Assume it's an unsigned integer at first. |
| 295 |
kind = Uint |
| 296 |
|
| 297 |
i := 0 |
| 298 |
// sign |
| 299 |
if b[i] == '-' { |
| 300 |
kind = Int |
| 301 |
i++ |
| 302 |
} |
| 303 |
|
| 304 |
if i == len(b) { |
| 305 |
r, err = b[i:], syntaxError(b, "missing number value after sign") |
| 306 |
return |
| 307 |
} |
| 308 |
|
| 309 |
if b[i] < '0' || b[i] > '9' { |
| 310 |
r, err = b[i:], syntaxError(b, "expected digit but got '%c'", b[i]) |
| 311 |
return |
| 312 |
} |
| 313 |
|
| 314 |
// integer part |
| 315 |
if b[i] == '0' { |
| 316 |
i++ |
| 317 |
if i == len(b) || (b[i] != '.' && b[i] != 'e' && b[i] != 'E') { |
| 318 |
v, r = b[:i], b[i:] |
| 319 |
return |
| 320 |
} |
| 321 |
if '0' <= b[i] && b[i] <= '9' { |
| 322 |
r, err = b[i:], syntaxError(b, "cannot decode number with leading '0' character") |
| 323 |
return |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
for i < len(b) && '0' <= b[i] && b[i] <= '9' { |
| 328 |
i++ |
| 329 |
} |
| 330 |
|
| 331 |
// decimal part |
| 332 |
if i < len(b) && b[i] == '.' { |
| 333 |
kind = Float |
| 334 |
i++ |
| 335 |
decimalStart := i |
| 336 |
|
| 337 |
for i < len(b) { |
| 338 |
if c := b[i]; '0' > c || c > '9' { |
| 339 |
if i == decimalStart { |
| 340 |
r, err = b[i:], syntaxError(b, "expected digit but found '%c'", c) |
| 341 |
return |
| 342 |
} |
| 343 |
break |
| 344 |
} |
| 345 |
i++ |
| 346 |
} |
| 347 |
|
| 348 |
if i == decimalStart { |
| 349 |
r, err = b[i:], syntaxError(b, "expected decimal part after '.'") |
| 350 |
return |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
// exponent part |
| 355 |
if i < len(b) && (b[i] == 'e' || b[i] == 'E') { |
| 356 |
kind = Float |
| 357 |
i++ |
| 358 |
|
| 359 |
if i < len(b) { |
| 360 |
if c := b[i]; c == '+' || c == '-' { |
| 361 |
i++ |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if i == len(b) { |
| 366 |
r, err = b[i:], syntaxError(b, "missing exponent in number") |
| 367 |
return |
| 368 |
} |
| 369 |
|
| 370 |
exponentStart := i |
| 371 |
|
| 372 |
for i < len(b) { |
| 373 |
if c := b[i]; '0' > c || c > '9' { |
| 374 |
if i == exponentStart { |
| 375 |
err = syntaxError(b, "expected digit but found '%c'", c) |
| 376 |
return |
| 377 |
} |
| 378 |
break |
| 379 |
} |
| 380 |
i++ |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
v, r = b[:i], b[i:] |
| 385 |
return |
| 386 |
} |
| 387 |
|
| 388 |
func (d decoder) parseUnicode(b []byte) (rune, int, error) { |
| 389 |
if len(b) < 4 { |
| 390 |
return 0, len(b), syntaxError(b, "unicode code point must have at least 4 characters") |
| 391 |
} |
| 392 |
|
| 393 |
u, r, err := d.parseUintHex(b[:4]) |
| 394 |
if err != nil { |
| 395 |
return 0, 4, syntaxError(b, "parsing unicode code point: %s", err) |
| 396 |
} |
| 397 |
|
| 398 |
if len(r) != 0 { |
| 399 |
return 0, 4, syntaxError(b, "invalid unicode code point") |
| 400 |
} |
| 401 |
|
| 402 |
return rune(u), 4, nil |
| 403 |
} |
| 404 |
|
| 405 |
func (d decoder) parseString(b []byte) ([]byte, []byte, Kind, error) { |
| 406 |
if len(b) < 2 { |
| 407 |
return nil, b[len(b):], Undefined, unexpectedEOF(b) |
| 408 |
} |
| 409 |
if b[0] != '"' { |
| 410 |
return nil, b, Undefined, syntaxError(b, "expected '\"' at the beginning of a string value") |
| 411 |
} |
| 412 |
|
| 413 |
var n int |
| 414 |
if len(b) >= 9 { |
| 415 |
// This is an optimization for short strings. We read 8/16 bytes, |
| 416 |
// and XOR each with 0x22 (") so that these bytes (and only |
| 417 |
// these bytes) are now zero. We use the hasless(u,1) trick |
| 418 |
// from https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord |
| 419 |
// to determine whether any bytes are zero. Finally, we CTZ |
| 420 |
// to find the index of that byte. |
| 421 |
const mask1 = 0x2222222222222222 |
| 422 |
const mask2 = 0x0101010101010101 |
| 423 |
const mask3 = 0x8080808080808080 |
| 424 |
u := binary.LittleEndian.Uint64(b[1:]) ^ mask1 |
| 425 |
if mask := (u - mask2) & ^u & mask3; mask != 0 { |
| 426 |
n = bits.TrailingZeros64(mask)/8 + 2 |
| 427 |
goto found |
| 428 |
} |
| 429 |
if len(b) >= 17 { |
| 430 |
u = binary.LittleEndian.Uint64(b[9:]) ^ mask1 |
| 431 |
if mask := (u - mask2) & ^u & mask3; mask != 0 { |
| 432 |
n = bits.TrailingZeros64(mask)/8 + 10 |
| 433 |
goto found |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
n = bytes.IndexByte(b[1:], '"') + 2 |
| 438 |
if n <= 1 { |
| 439 |
return nil, b[len(b):], Undefined, syntaxError(b, "missing '\"' at the end of a string value") |
| 440 |
} |
| 441 |
found: |
| 442 |
if (d.flags.has(noBackslash) || bytes.IndexByte(b[1:n], '\\') < 0) && |
| 443 |
(d.flags.has(validAsciiPrint) || ascii.ValidPrint(b[1:n])) { |
| 444 |
return b[:n], b[n:], Unescaped, nil |
| 445 |
} |
| 446 |
|
| 447 |
for i := 1; i < len(b); i++ { |
| 448 |
switch b[i] { |
| 449 |
case '\\': |
| 450 |
if i++; i < len(b) { |
| 451 |
switch b[i] { |
| 452 |
case '"', '\\', '/', 'n', 'r', 't', 'f', 'b': |
| 453 |
case 'u': |
| 454 |
_, n, err := d.parseUnicode(b[i+1:]) |
| 455 |
if err != nil { |
| 456 |
return nil, b[i+1+n:], Undefined, err |
| 457 |
} |
| 458 |
i += n |
| 459 |
default: |
| 460 |
return nil, b, Undefined, syntaxError(b, "invalid character '%c' in string escape code", b[i]) |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
case '"': |
| 465 |
return b[:i+1], b[i+1:], String, nil |
| 466 |
|
| 467 |
default: |
| 468 |
if b[i] < 0x20 { |
| 469 |
return nil, b, Undefined, syntaxError(b, "invalid character '%c' in string escape code", b[i]) |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
return nil, b[len(b):], Undefined, syntaxError(b, "missing '\"' at the end of a string value") |
| 475 |
} |
| 476 |
|
| 477 |
func (d decoder) parseStringUnquote(b []byte, r []byte) ([]byte, []byte, bool, error) { |
| 478 |
s, b, k, err := d.parseString(b) |
| 479 |
if err != nil { |
| 480 |
return s, b, false, err |
| 481 |
} |
| 482 |
|
| 483 |
s = s[1 : len(s)-1] // trim the quotes |
| 484 |
|
| 485 |
if k == Unescaped { |
| 486 |
return s, b, false, nil |
| 487 |
} |
| 488 |
|
| 489 |
if r == nil { |
| 490 |
r = make([]byte, 0, len(s)) |
| 491 |
} |
| 492 |
|
| 493 |
for len(s) != 0 { |
| 494 |
i := bytes.IndexByte(s, '\\') |
| 495 |
|
| 496 |
if i < 0 { |
| 497 |
r = appendCoerceInvalidUTF8(r, s) |
| 498 |
break |
| 499 |
} |
| 500 |
|
| 501 |
r = appendCoerceInvalidUTF8(r, s[:i]) |
| 502 |
s = s[i+1:] |
| 503 |
|
| 504 |
c := s[0] |
| 505 |
switch c { |
| 506 |
case '"', '\\', '/': |
| 507 |
// simple escaped character |
| 508 |
case 'n': |
| 509 |
c = '\n' |
| 510 |
|
| 511 |
case 'r': |
| 512 |
c = '\r' |
| 513 |
|
| 514 |
case 't': |
| 515 |
c = '\t' |
| 516 |
|
| 517 |
case 'b': |
| 518 |
c = '\b' |
| 519 |
|
| 520 |
case 'f': |
| 521 |
c = '\f' |
| 522 |
|
| 523 |
case 'u': |
| 524 |
s = s[1:] |
| 525 |
|
| 526 |
r1, n1, err := d.parseUnicode(s) |
| 527 |
if err != nil { |
| 528 |
return r, b, true, err |
| 529 |
} |
| 530 |
s = s[n1:] |
| 531 |
|
| 532 |
if utf16.IsSurrogate(r1) { |
| 533 |
if !hasPrefix(s, `\u`) { |
| 534 |
r1 = unicode.ReplacementChar |
| 535 |
} else { |
| 536 |
r2, n2, err := d.parseUnicode(s[2:]) |
| 537 |
if err != nil { |
| 538 |
return r, b, true, err |
| 539 |
} |
| 540 |
if r1 = utf16.DecodeRune(r1, r2); r1 != unicode.ReplacementChar { |
| 541 |
s = s[2+n2:] |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
r = appendRune(r, r1) |
| 547 |
continue |
| 548 |
|
| 549 |
default: // not sure what this escape sequence is |
| 550 |
return r, b, false, syntaxError(s, "invalid character '%c' in string escape code", c) |
| 551 |
} |
| 552 |
|
| 553 |
r = append(r, c) |
| 554 |
s = s[1:] |
| 555 |
} |
| 556 |
|
| 557 |
return r, b, true, nil |
| 558 |
} |
| 559 |
|
| 560 |
func appendRune(b []byte, r rune) []byte { |
| 561 |
n := len(b) |
| 562 |
b = append(b, 0, 0, 0, 0) |
| 563 |
return b[:n+utf8.EncodeRune(b[n:], r)] |
| 564 |
} |
| 565 |
|
| 566 |
func appendCoerceInvalidUTF8(b []byte, s []byte) []byte { |
| 567 |
c := [4]byte{} |
| 568 |
|
| 569 |
for _, r := range string(s) { |
| 570 |
b = append(b, c[:utf8.EncodeRune(c[:], r)]...) |
| 571 |
} |
| 572 |
|
| 573 |
return b |
| 574 |
} |
| 575 |
|
| 576 |
func (d decoder) parseObject(b []byte) ([]byte, []byte, Kind, error) { |
| 577 |
if len(b) < 2 { |
| 578 |
return nil, b[len(b):], Undefined, unexpectedEOF(b) |
| 579 |
} |
| 580 |
|
| 581 |
if b[0] != '{' { |
| 582 |
return nil, b, Undefined, syntaxError(b, "expected '{' at the beginning of an object value") |
| 583 |
} |
| 584 |
|
| 585 |
var err error |
| 586 |
a := b |
| 587 |
n := len(b) |
| 588 |
i := 0 |
| 589 |
|
| 590 |
b = b[1:] |
| 591 |
for { |
| 592 |
b = skipSpaces(b) |
| 593 |
|
| 594 |
if len(b) == 0 { |
| 595 |
return nil, b, Undefined, syntaxError(b, "cannot decode object from empty input") |
| 596 |
} |
| 597 |
|
| 598 |
if b[0] == '}' { |
| 599 |
j := (n - len(b)) + 1 |
| 600 |
return a[:j], a[j:], Object, nil |
| 601 |
} |
| 602 |
|
| 603 |
if i != 0 { |
| 604 |
if len(b) == 0 { |
| 605 |
return nil, b, Undefined, syntaxError(b, "unexpected EOF after object field value") |
| 606 |
} |
| 607 |
if b[0] != ',' { |
| 608 |
return nil, b, Undefined, syntaxError(b, "expected ',' after object field value but found '%c'", b[0]) |
| 609 |
} |
| 610 |
b = skipSpaces(b[1:]) |
| 611 |
if len(b) == 0 { |
| 612 |
return nil, b, Undefined, unexpectedEOF(b) |
| 613 |
} |
| 614 |
if b[0] == '}' { |
| 615 |
return nil, b, Undefined, syntaxError(b, "unexpected trailing comma after object field") |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
_, b, _, err = d.parseString(b) |
| 620 |
if err != nil { |
| 621 |
return nil, b, Undefined, err |
| 622 |
} |
| 623 |
b = skipSpaces(b) |
| 624 |
|
| 625 |
if len(b) == 0 { |
| 626 |
return nil, b, Undefined, syntaxError(b, "unexpected EOF after object field key") |
| 627 |
} |
| 628 |
if b[0] != ':' { |
| 629 |
return nil, b, Undefined, syntaxError(b, "expected ':' after object field key but found '%c'", b[0]) |
| 630 |
} |
| 631 |
b = skipSpaces(b[1:]) |
| 632 |
|
| 633 |
_, b, _, err = d.parseValue(b) |
| 634 |
if err != nil { |
| 635 |
return nil, b, Undefined, err |
| 636 |
} |
| 637 |
|
| 638 |
i++ |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
func (d decoder) parseArray(b []byte) ([]byte, []byte, Kind, error) { |
| 643 |
if len(b) < 2 { |
| 644 |
return nil, b[len(b):], Undefined, unexpectedEOF(b) |
| 645 |
} |
| 646 |
|
| 647 |
if b[0] != '[' { |
| 648 |
return nil, b, Undefined, syntaxError(b, "expected '[' at the beginning of array value") |
| 649 |
} |
| 650 |
|
| 651 |
var err error |
| 652 |
a := b |
| 653 |
n := len(b) |
| 654 |
i := 0 |
| 655 |
|
| 656 |
b = b[1:] |
| 657 |
for { |
| 658 |
b = skipSpaces(b) |
| 659 |
|
| 660 |
if len(b) == 0 { |
| 661 |
return nil, b, Undefined, syntaxError(b, "missing closing ']' after array value") |
| 662 |
} |
| 663 |
|
| 664 |
if b[0] == ']' { |
| 665 |
j := (n - len(b)) + 1 |
| 666 |
return a[:j], a[j:], Array, nil |
| 667 |
} |
| 668 |
|
| 669 |
if i != 0 { |
| 670 |
if len(b) == 0 { |
| 671 |
return nil, b, Undefined, syntaxError(b, "unexpected EOF after array element") |
| 672 |
} |
| 673 |
if b[0] != ',' { |
| 674 |
return nil, b, Undefined, syntaxError(b, "expected ',' after array element but found '%c'", b[0]) |
| 675 |
} |
| 676 |
b = skipSpaces(b[1:]) |
| 677 |
if len(b) == 0 { |
| 678 |
return nil, b, Undefined, unexpectedEOF(b) |
| 679 |
} |
| 680 |
if b[0] == ']' { |
| 681 |
return nil, b, Undefined, syntaxError(b, "unexpected trailing comma after object field") |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
_, b, _, err = d.parseValue(b) |
| 686 |
if err != nil { |
| 687 |
return nil, b, Undefined, err |
| 688 |
} |
| 689 |
|
| 690 |
i++ |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
func (d decoder) parseValue(b []byte) ([]byte, []byte, Kind, error) { |
| 695 |
if len(b) == 0 { |
| 696 |
return nil, b, Undefined, syntaxError(b, "unexpected end of JSON input") |
| 697 |
} |
| 698 |
|
| 699 |
var v []byte |
| 700 |
var k Kind |
| 701 |
var err error |
| 702 |
|
| 703 |
switch b[0] { |
| 704 |
case '{': |
| 705 |
v, b, k, err = d.parseObject(b) |
| 706 |
case '[': |
| 707 |
v, b, k, err = d.parseArray(b) |
| 708 |
case '"': |
| 709 |
v, b, k, err = d.parseString(b) |
| 710 |
case 'n': |
| 711 |
v, b, k, err = d.parseNull(b) |
| 712 |
case 't': |
| 713 |
v, b, k, err = d.parseTrue(b) |
| 714 |
case 'f': |
| 715 |
v, b, k, err = d.parseFalse(b) |
| 716 |
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 717 |
v, b, k, err = d.parseNumber(b) |
| 718 |
default: |
| 719 |
err = syntaxError(b, "invalid character '%c' looking for beginning of value", b[0]) |
| 720 |
} |
| 721 |
|
| 722 |
return v, b, k, err |
| 723 |
} |
| 724 |
|
| 725 |
func hasNullPrefix(b []byte) bool { |
| 726 |
return len(b) >= 4 && string(b[:4]) == "null" |
| 727 |
} |
| 728 |
|
| 729 |
func hasTruePrefix(b []byte) bool { |
| 730 |
return len(b) >= 4 && string(b[:4]) == "true" |
| 731 |
} |
| 732 |
|
| 733 |
func hasFalsePrefix(b []byte) bool { |
| 734 |
return len(b) >= 5 && string(b[:5]) == "false" |
| 735 |
} |
| 736 |
|
| 737 |
func hasPrefix(b []byte, s string) bool { |
| 738 |
return len(b) >= len(s) && s == string(b[:len(s)]) |
| 739 |
} |
| 740 |
|
| 741 |
func hasLeadingSign(b []byte) bool { |
| 742 |
return len(b) > 0 && (b[0] == '+' || b[0] == '-') |
| 743 |
} |
| 744 |
|
| 745 |
func hasLeadingZeroes(b []byte) bool { |
| 746 |
if hasLeadingSign(b) { |
| 747 |
b = b[1:] |
| 748 |
} |
| 749 |
return len(b) > 1 && b[0] == '0' && '0' <= b[1] && b[1] <= '9' |
| 750 |
} |
| 751 |
|
| 752 |
func appendToLower(b, s []byte) []byte { |
| 753 |
if ascii.Valid(s) { // fast path for ascii strings |
| 754 |
i := 0 |
| 755 |
|
| 756 |
for j := range s { |
| 757 |
c := s[j] |
| 758 |
|
| 759 |
if 'A' <= c && c <= 'Z' { |
| 760 |
b = append(b, s[i:j]...) |
| 761 |
b = append(b, c+('a'-'A')) |
| 762 |
i = j + 1 |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
return append(b, s[i:]...) |
| 767 |
} |
| 768 |
|
| 769 |
for _, r := range string(s) { |
| 770 |
b = appendRune(b, foldRune(r)) |
| 771 |
} |
| 772 |
|
| 773 |
return b |
| 774 |
} |
| 775 |
|
| 776 |
func foldRune(r rune) rune { |
| 777 |
if r = unicode.SimpleFold(r); 'A' <= r && r <= 'Z' { |
| 778 |
r = r + ('a' - 'A') |
| 779 |
} |
| 780 |
return r |
| 781 |
} |