git.lukeshu.com/go/lowmemjson@v0.3.9-0.20230723050957-72f6d13f6fb2/decode.go (about) 1 // Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> 2 // 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 // 5 // Some doc comments are 6 // copyright 2010 The Go Authors. All rights reserved. 7 // Use of this source code is governed by a BSD-style 8 // license that can be found in the LICENSE file. 9 // 10 // SPDX-License-Identifier: BSD-3-Clause 11 12 package lowmemjson 13 14 import ( 15 "bytes" 16 "encoding" 17 "fmt" 18 "io" 19 "reflect" 20 "strconv" 21 "strings" 22 "unicode/utf16" 23 "unicode/utf8" 24 25 "git.lukeshu.com/go/lowmemjson/internal/base64dec" 26 "git.lukeshu.com/go/lowmemjson/internal/fastio" 27 "git.lukeshu.com/go/lowmemjson/internal/jsonparse" 28 "git.lukeshu.com/go/lowmemjson/internal/jsonstruct" 29 ) 30 31 // Decodable is the interface implemented by types that can decode a 32 // JSON representation of themselves. Decodable is a 33 // low-memory-overhead replacement for the json.Unmarshaler interface. 34 // 35 // On the io.RuneScanner passed to DecodeJSON: 36 // 37 // - .UnreadRune() will return ErrInvalidUnreadRune if the last 38 // operation was not a successful .ReadRune() call. 39 // 40 // - .ReadRune() will return io.EOF at the end of the JSON value; it 41 // is not possible for .ReadRune() to read past the end of the 42 // value in to another value. 43 // 44 // - .ReadRune() will never return invalid JSON; if invalid JSON is 45 // encountered, it will use a panic-based mechanism to transfer 46 // control back to the Decoder. 47 // 48 // - .ReadRune() never return an error other than io.EOF; if an I/O 49 // error is encountered, it will use a panic-based mechanism to 50 // transfer control back to the Decoder. 51 // 52 // DecodeJSON is expected to consume the entire scanner until io.EOF 53 // or another is encountered; if it does not, then the parent Decode 54 // call will return a *DecodeTypeError. 55 // 56 // DecodeJSON should return nil (not io.EOF) on success. 57 // 58 // Implementor's note: "withLimitingScanner" is the thing to search 59 // for in decode.go if you want to read up on that io.RuneScanner. 60 type Decodable interface { 61 DecodeJSON(io.RuneScanner) error 62 } 63 64 type decodeStackItem struct { 65 par reflect.Type 66 idx any 67 } 68 69 // A Decoder reads and decodes values from an input stream of JSON 70 // elements. 71 // 72 // Decoder is analogous to, and has a similar API to the standard 73 // library's encoding/json.Decoder. Differences are: 74 // 75 // - lowmemjson.NewDecoder takes an io.RuneScanner, while 76 // json.NewDecoder takes an io.Reader. 77 // 78 // - lowmemjson.Decoder does not have a .Buffered() method, while 79 // json.Decoder does. 80 // 81 // - lowmemjson.Decoder does not have a .Token() method, while 82 // json.Decoder does. 83 // 84 // If something more similar to a json.Decoder is desired, 85 // lowmemjson/compat/json.NewDecoder takes an io.Reader (and turns it 86 // into an io.RuneScanner by wrapping it in a bufio.Reader), and 87 // lowmemjson/compat/json.Decoder has a .Buffered() method; though 88 // lowmemjson/compat/json.Decoder also lacks the .Token() method. 89 type Decoder struct { 90 io runeTypeScanner 91 92 // config 93 disallowUnknownFields bool 94 useNumber bool 95 96 // state 97 posStack []int64 98 structStack []decodeStackItem 99 typeErr *DecodeError 100 } 101 102 const maxNestingDepth = 10000 103 104 // NewDecoder returns a new Decoder that reads from r. 105 // 106 // NewDecoder is analogous to the standard library's 107 // encoding/json.NewDecoder, but takes an io.RuneScanner rather than 108 // an io.Reader. 109 func NewDecoder(r io.RuneScanner) *Decoder { 110 return &Decoder{ 111 io: runeTypeScanner{ 112 inner: r, 113 parser: jsonparse.Parser{ 114 MaxDepth: maxNestingDepth, 115 }, 116 }, 117 } 118 } 119 120 // DisallowUnknownFields causes the Decoder to return an error when 121 // the destination is a struct and the input contains object keys 122 // which do not match any non-ignored, exported fields in the 123 // destination. 124 // 125 // This is identical to the standard library's 126 // encoding/json.Decoder.DisallowUnknownFields. 127 func (dec *Decoder) DisallowUnknownFields() { dec.disallowUnknownFields = true } 128 129 // UseNumber causes the Decoder to unmarshal a number into an 130 // interface{} as a Number instead of as a float64. 131 // 132 // This is identical to the standard library's 133 // encoding/json.Decoder.UseNumber. 134 func (dec *Decoder) UseNumber() { dec.useNumber = true } 135 136 // InputOffset returns the input stream byte offset of the current 137 // decoder position. The offset gives the location of the rune that 138 // will be returned from the next call to .ReadRune(). 139 // 140 // This is identical to the standard library's 141 // encoding/json.Decoder.InputOffset. 142 func (dec *Decoder) InputOffset() int64 { return dec.io.InputOffset() } 143 144 // More reports whether there is more to the stream of JSON elements, 145 // or if the Decoder has reached EOF or an error. 146 // 147 // More is identical to the standard library's 148 // encoding/json.Decoder.More. 149 func (dec *Decoder) More() bool { 150 dec.io.Reset() 151 _, _, t, e := dec.io.ReadRuneType() 152 _ = dec.io.UnreadRune() // best effort 153 return e == nil && t != jsonparse.RuneTypeEOF 154 } 155 156 func (dec *Decoder) posStackPush() { 157 dec.posStack = append(dec.posStack, dec.InputOffset()) 158 } 159 160 func (dec *Decoder) posStackPop() { 161 dec.posStack = dec.posStack[:len(dec.posStack)-1] 162 } 163 164 func (dec *Decoder) structStackPush(par reflect.Type, idx any) { 165 dec.structStack = append(dec.structStack, decodeStackItem{par, idx}) 166 } 167 168 func (dec *Decoder) structStackPop() { 169 dec.structStack = dec.structStack[:len(dec.structStack)-1] 170 } 171 172 func (dec *Decoder) structStackStr() string { 173 var buf strings.Builder 174 buf.WriteString("v") 175 for _, item := range dec.structStack { 176 fmt.Fprintf(&buf, "[%#v]", item.idx) 177 } 178 return buf.String() 179 } 180 181 func (dec *Decoder) structStackParent() string { 182 last := len(dec.structStack) - 1 183 if last > 0 && dec.structStack[last].par.Kind() != reflect.Struct && dec.structStack[last-1].par.Kind() == reflect.Struct { 184 last-- 185 } 186 if last >= 0 && dec.structStack[last].par.Kind() == reflect.Struct { 187 return dec.structStack[last].par.Name() 188 } 189 return "" 190 } 191 192 func (dec *Decoder) structStackName() string { 193 if dec.structStackParent() == "" { 194 return "" 195 } 196 var fields []string 197 for _, elem := range dec.structStack { 198 if elem.par.Kind() == reflect.Struct { 199 fields = append(fields, elem.idx.(string)) 200 } 201 } 202 return strings.Join(fields, ".") 203 } 204 205 // DecodeThenEOF is like Decode, but emits an error if there is extra 206 // data after the JSON. A JSON document is specified to be a single 207 // JSON element; repeated calls to Decoder.Decode will happily decode 208 // a stream of multiple JSON elements. 209 func (dec *Decoder) DecodeThenEOF(ptr any) (err error) { 210 if err := dec.Decode(ptr); err != nil { 211 return err 212 } 213 _, _, t, err := dec.io.ReadRuneType() 214 switch t { 215 case jsonparse.RuneTypeError: 216 return &DecodeError{ 217 Err: err, 218 } 219 case jsonparse.RuneTypeEOF: 220 return nil 221 default: 222 panic(fmt.Errorf("should not happen: .ReadRuneType returned non-error non-EOF after decode without .Reset being called: '%v'", t)) 223 } 224 } 225 226 // Decode reads the next JSON element from the Decoder's input stream 227 // and stores it in the value pointed to by ptr. 228 // 229 // See the [documentation for encoding/json.Unmarshal] for details 230 // about the conversion of JSON into a Go value; Decode behaves 231 // identically to that, with the exception that in addition to the 232 // json.Unmarshaler interface it also checks for the Decodable 233 // interface. 234 // 235 // [documentation for encoding/json.Unmarshal]: https://pkg.go.dev/encoding/json@go1.20#Unmarshal 236 func (dec *Decoder) Decode(ptr any) (err error) { 237 ptrVal := reflect.ValueOf(ptr) 238 if ptrVal.Kind() != reflect.Pointer || ptrVal.IsNil() || !ptrVal.Elem().CanSet() { 239 return &DecodeArgumentError{ 240 // don't use ptrVal.Type() because ptrVal might be invalid if ptr==nil 241 Type: reflect.TypeOf(ptr), 242 } 243 } 244 245 dec.typeErr = nil 246 dec.io.Reset() 247 dec.io.PushReadBarrier() 248 if err := dec.decode(ptrVal.Elem(), false); err != nil { 249 return err 250 } 251 dec.io.PopReadBarrier() 252 if dec.typeErr != nil { 253 return dec.typeErr 254 } 255 return nil 256 } 257 258 // io helpers ////////////////////////////////////////////////////////////////////////////////////// 259 260 func (dec *Decoder) newTypeError(jTyp string, gTyp reflect.Type, err error) { 261 if dec.typeErr != nil { 262 return 263 } 264 dec.typeErr = &DecodeError{ 265 Field: dec.structStackStr(), 266 FieldParent: dec.structStackParent(), 267 FieldName: dec.structStackName(), 268 Err: &DecodeTypeError{ 269 GoType: gTyp, 270 JSONType: jTyp, 271 Err: err, 272 Offset: dec.posStack[len(dec.posStack)-1], 273 }, 274 } 275 } 276 277 func (dec *Decoder) readRune() (rune, jsonparse.RuneType, *DecodeError) { 278 c, _, t, e := dec.io.ReadRuneType() 279 if e != nil { 280 return 0, 0, &DecodeError{ 281 Field: dec.structStackStr(), 282 FieldParent: dec.structStackParent(), 283 FieldName: dec.structStackName(), 284 Err: e, 285 } 286 } 287 return c, t, nil 288 } 289 290 func (dec *Decoder) unreadRune() { 291 if err := dec.io.UnreadRune(); err != nil { 292 // .UnreadRune() must succeed if the previous call was 293 // .ReadRune(), which it always is for this code. 294 panic(fmt.Errorf("should not happen: UnreadRune: %w", err)) 295 } 296 } 297 298 func (dec *Decoder) peekRuneType() (jsonparse.RuneType, *DecodeError) { 299 _, t, err := dec.readRune() 300 if err != nil { 301 return 0, err 302 } 303 dec.unreadRune() 304 return t, nil 305 } 306 307 // expectRuneOrPanic is for when you *know* what the next 308 // non-whitespace rune is going to be; for it to be anything else 309 // would be a syntax error. It will return an error for I/O errors 310 // and syntax errors, but panic if the result is not what was 311 // expected; as that would indicate a bug in the agreement between the 312 // parser and the decoder. 313 func (dec *Decoder) expectRuneOrPanic(ec rune, et jsonparse.RuneType) *DecodeError { 314 ac, at, err := dec.readRune() 315 if err != nil { 316 return err 317 } 318 if ac != ec || at != et { 319 panic(fmt.Errorf("should not happen: expected r=%q t=%#v but got r=%q t=%#v", ec, et, ac, at)) 320 } 321 return nil 322 } 323 324 type decRuneScanner struct { 325 dec *Decoder 326 eof bool 327 } 328 329 type scannerPanic *DecodeError 330 331 func (sc *decRuneScanner) ReadRune() (rune, int, error) { 332 if sc.eof { 333 return 0, 0, io.EOF 334 } 335 c, s, t, e := sc.dec.io.ReadRuneType() 336 if t == jsonparse.RuneTypeEOF { 337 sc.eof = true 338 sc.dec.io.PopReadBarrier() 339 return 0, 0, io.EOF 340 } 341 if e != nil { 342 panic(scannerPanic(&DecodeError{ 343 Field: sc.dec.structStackStr(), 344 FieldParent: sc.dec.structStackParent(), 345 FieldName: sc.dec.structStackName(), 346 Err: e, 347 })) 348 } 349 return c, s, nil 350 } 351 352 func (sc *decRuneScanner) UnreadRune() error { 353 return sc.dec.io.UnreadRune() 354 } 355 356 func (dec *Decoder) withLimitingScanner(gTyp reflect.Type, fn func(io.RuneScanner) error) (err *DecodeError) { 357 t, err := dec.peekRuneType() 358 if err != nil { 359 return err 360 } 361 dec.io.PushReadBarrier() 362 defer func() { 363 if r := recover(); r != nil { 364 if sp, ok := r.(scannerPanic); ok { 365 err = (*DecodeError)(sp) 366 } else { 367 panic(r) 368 } 369 } 370 }() 371 l := &decRuneScanner{dec: dec} 372 if err := fn(l); err != nil { 373 dec.newTypeError(t.JSONType(), gTyp, err) 374 } 375 if _, _, err := l.ReadRune(); err != io.EOF { 376 dec.newTypeError(t.JSONType(), gTyp, fmt.Errorf("did not consume entire %s", t.JSONType())) 377 for err != io.EOF { 378 _, _, err = l.ReadRune() 379 } 380 } 381 return nil 382 } 383 384 // decoder main //////////////////////////////////////////////////////////////////////////////////// 385 386 var kind2bits = map[reflect.Kind]int{ 387 reflect.Int: int(32 << (^uint(0) >> 63)), 388 reflect.Int8: 8, 389 reflect.Int16: 16, 390 reflect.Int32: 32, 391 reflect.Int64: 64, 392 393 reflect.Uint: int(32 << (^uint(0) >> 63)), 394 reflect.Uint8: 8, 395 reflect.Uint16: 16, 396 reflect.Uint32: 32, 397 reflect.Uint64: 64, 398 399 reflect.Uintptr: int(32 << (^uintptr(0) >> 63)), 400 401 reflect.Float32: 32, 402 reflect.Float64: 64, 403 } 404 405 func (dec *Decoder) decode(val reflect.Value, nullOK bool) *DecodeError { 406 dec.posStackPush() 407 defer dec.posStackPop() 408 typ := val.Type() 409 switch { 410 case val.CanAddr() && reflect.PointerTo(typ) == rawMessagePtrType: 411 t, err := dec.peekRuneType() 412 if err != nil { 413 return err 414 } 415 var buf bytes.Buffer 416 if err := dec.scan(&buf); err != nil { 417 return err 418 } 419 if err := val.Addr().Interface().(*RawMessage).UnmarshalJSON(buf.Bytes()); err != nil { 420 dec.newTypeError(t.JSONType(), reflect.PointerTo(typ), err) 421 } 422 case val.CanAddr() && reflect.PointerTo(typ).Implements(decodableType): 423 obj := val.Addr().Interface().(Decodable) 424 return dec.withLimitingScanner(reflect.PointerTo(typ), obj.DecodeJSON) 425 case val.CanAddr() && reflect.PointerTo(typ).Implements(jsonUnmarshalerType): 426 t, err := dec.peekRuneType() 427 if err != nil { 428 return err 429 } 430 var buf bytes.Buffer 431 if err := dec.scan(&buf); err != nil { 432 return err 433 } 434 obj := val.Addr().Interface().(jsonUnmarshaler) 435 if err := obj.UnmarshalJSON(buf.Bytes()); err != nil { 436 dec.newTypeError(t.JSONType(), reflect.PointerTo(typ), err) 437 } 438 case val.CanAddr() && reflect.PointerTo(typ).Implements(textUnmarshalerType): 439 if ok, err := dec.maybeDecodeNull(nullOK); ok { 440 return err 441 } 442 var buf bytes.Buffer 443 if err := dec.decodeString(reflect.PointerTo(typ), &buf); err != nil { 444 return err 445 } 446 obj := val.Addr().Interface().(encoding.TextUnmarshaler) 447 if err := obj.UnmarshalText(buf.Bytes()); err != nil { 448 dec.newTypeError("string", reflect.PointerTo(typ), err) 449 } 450 default: 451 switch kind := typ.Kind(); kind { 452 case reflect.Bool: 453 if ok, err := dec.maybeDecodeNull(nullOK); ok { 454 return err 455 } 456 b, err := dec.decodeBool(typ) 457 if err != nil { 458 return err 459 } 460 val.SetBool(b) 461 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 462 if ok, err := dec.maybeDecodeNull(nullOK); ok { 463 return err 464 } 465 if t, err := dec.peekRuneType(); err != nil { 466 return err 467 } else if !t.IsNumber() { 468 dec.newTypeError(t.JSONType(), typ, nil) 469 return dec.scan(fastio.Discard) 470 } 471 var buf strings.Builder 472 if err := dec.scan(&buf); err != nil { 473 return err 474 } 475 n, err := strconv.ParseInt(buf.String(), 10, kind2bits[kind]) 476 if err != nil { 477 dec.newTypeError("number "+buf.String(), typ, err) 478 return nil 479 } 480 val.SetInt(n) 481 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 482 if ok, err := dec.maybeDecodeNull(nullOK); ok { 483 return err 484 } 485 if t, err := dec.peekRuneType(); err != nil { 486 return err 487 } else if !t.IsNumber() { 488 dec.newTypeError(t.JSONType(), typ, nil) 489 return dec.scan(fastio.Discard) 490 } 491 var buf strings.Builder 492 if err := dec.scan(&buf); err != nil { 493 return err 494 } 495 n, err := strconv.ParseUint(buf.String(), 10, kind2bits[kind]) 496 if err != nil { 497 dec.newTypeError("number "+buf.String(), typ, err) 498 return nil 499 } 500 val.SetUint(n) 501 case reflect.Float32, reflect.Float64: 502 if ok, err := dec.maybeDecodeNull(nullOK); ok { 503 return err 504 } 505 if t, err := dec.peekRuneType(); err != nil { 506 return err 507 } else if !t.IsNumber() { 508 dec.newTypeError(t.JSONType(), typ, nil) 509 return dec.scan(fastio.Discard) 510 } 511 var buf strings.Builder 512 if err := dec.scan(&buf); err != nil { 513 return err 514 } 515 n, err := strconv.ParseFloat(buf.String(), kind2bits[kind]) 516 if err != nil { 517 dec.newTypeError("number "+buf.String(), typ, err) 518 return nil 519 } 520 val.SetFloat(n) 521 case reflect.String: 522 if ok, err := dec.maybeDecodeNull(nullOK); ok { 523 return err 524 } 525 var buf strings.Builder 526 if typ == numberType { 527 t, err := dec.peekRuneType() 528 if err != nil { 529 return err 530 } 531 if err := dec.scan(&buf); err != nil { 532 return err 533 } 534 if !t.IsNumber() { 535 dec.newTypeError(t.JSONType(), typ, 536 fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", 537 buf.String())) 538 return nil 539 } 540 val.SetString(buf.String()) 541 } else { 542 if err := dec.decodeString(typ, &buf); err != nil { 543 return err 544 } 545 val.SetString(buf.String()) 546 } 547 case reflect.Interface: 548 t, err := dec.peekRuneType() 549 if err != nil { 550 return err 551 } 552 if typ.NumMethod() > 0 { 553 dec.newTypeError(t.JSONType(), typ, ErrDecodeNonEmptyInterface) 554 return dec.scan(fastio.Discard) 555 } 556 // If the interface stores a pointer, try to use the type information of the pointer. 557 if !val.IsNil() && val.Elem().Kind() == reflect.Pointer { 558 // Follow a chain of pointers until we find the first settable 559 // pointer (if any). 560 ptr := val.Elem() 561 for { 562 if ptr.CanSet() || ptr.IsNil() || ptr.Elem().Kind() != reflect.Pointer { 563 // We've reached the end of the line, good or bad. 564 break 565 } 566 ptr = ptr.Elem() 567 } 568 // ptr.Elem() != val 569 // 570 // Avoid the loop of an interface storing a pointer to its own 571 // address. We only need to worry about this at the leaf (and not 572 // in the loop) because the only way it's possible is if there's 573 // an interface in there, which'd break from the loop on its own. 574 // 575 // ptr.CanSet() || t != jsonparse.RuneTypeNullN 576 // 577 // We only need the pointer itself to be settable if we're 578 // decoding null. 579 if ptr.Elem() != val && (ptr.CanSet() || t != jsonparse.RuneTypeNullN) { 580 if err := dec.decode(ptr, false); err != nil { 581 return err 582 } 583 break 584 } 585 } 586 // Couldn't get type information from a pointer; fall back to untyped mode. 587 switch t { 588 case jsonparse.RuneTypeNullN: 589 if err := dec.decodeNull(); err != nil { 590 return err 591 } 592 val.Set(reflect.Zero(typ)) 593 default: 594 v, err := dec.decodeAny() 595 if err != nil { 596 return err 597 } 598 if v != nil { 599 val.Set(reflect.ValueOf(v)) 600 } 601 } 602 case reflect.Struct: 603 if ok, err := dec.maybeDecodeNull(nullOK); ok { 604 return err 605 } 606 index := jsonstruct.IndexStruct(typ) 607 var nameBuf strings.Builder 608 return dec.decodeObject(typ, func() *DecodeError { 609 dec.posStackPush() 610 defer dec.posStackPop() 611 nameBuf.Reset() 612 return dec.decodeString(nil, &nameBuf) 613 }, func() *DecodeError { 614 dec.posStackPush() 615 defer dec.posStackPop() 616 name := nameBuf.String() 617 dec.structStackPush(typ, name) 618 defer dec.structStackPop() 619 idx, ok := index.ByName[name] 620 if !ok { 621 for oidx := range index.ByPos { 622 if strings.EqualFold(name, index.ByPos[oidx].Name) { 623 idx = oidx 624 ok = true 625 break 626 } 627 } 628 } 629 if !ok { 630 if dec.disallowUnknownFields { 631 dec.newTypeError("", typ, fmt.Errorf("json: unknown field %q", name)) 632 } 633 return dec.scan(fastio.Discard) 634 } 635 field := index.ByPos[idx] 636 fVal := val 637 for _, idx := range field.Path { 638 if fVal.Kind() == reflect.Pointer { 639 if fVal.IsNil() && !fVal.CanSet() { // https://golang.org/issue/21357 640 dec.newTypeError("", fVal.Type().Elem(), 641 fmt.Errorf("json: cannot set embedded pointer to unexported struct: %v", 642 fVal.Type().Elem())) 643 return dec.scan(fastio.Discard) 644 } 645 t, err := dec.peekRuneType() 646 if err != nil { 647 return err 648 } 649 if t != jsonparse.RuneTypeNullN { 650 if fVal.IsNil() { 651 fVal.Set(reflect.New(fVal.Type().Elem())) 652 } 653 fVal = fVal.Elem() 654 } 655 } 656 fVal = fVal.Field(idx) 657 } 658 if field.Quote { 659 t, err := dec.peekRuneType() 660 if err != nil { 661 return err 662 } 663 switch t { 664 case jsonparse.RuneTypeNullN: 665 if err := dec.decodeNull(); err != nil { 666 return err 667 } 668 switch fVal.Kind() { 669 // XXX: I can't justify this list, other than "it's what encoding/json 670 // does, but I don't understand their rationale". 671 case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice: 672 fVal.Set(reflect.Zero(fVal.Type())) 673 default: 674 // do nothing??? 675 } 676 case jsonparse.RuneTypeStringBeg: 677 // TODO: Figure out how to do this without buffering, have correct offsets. 678 var buf bytes.Buffer 679 if err := dec.decodeString(nil, &buf); err != nil { 680 return err 681 } 682 if err := NewDecoder(bytes.NewReader(buf.Bytes())).Decode(fVal.Addr().Interface()); err != nil { 683 if str := buf.String(); str != "null" { 684 dec.newTypeError("", fVal.Type(), 685 fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", 686 str, fVal.Type())) 687 } 688 } 689 default: 690 dec.newTypeError(t.JSONType(), fVal.Type(), 691 fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", 692 fVal.Type())) 693 return dec.scan(fastio.Discard) 694 } 695 return nil 696 } else { 697 return dec.decode(fVal, true) 698 } 699 }) 700 case reflect.Map: 701 t, err := dec.peekRuneType() 702 if err != nil { 703 return err 704 } 705 switch t { 706 case jsonparse.RuneTypeNullN: 707 if err := dec.decodeNull(); err != nil { 708 return err 709 } 710 val.Set(reflect.Zero(typ)) 711 case jsonparse.RuneTypeObjectBeg: 712 if val.IsNil() { 713 val.Set(reflect.MakeMap(typ)) 714 } 715 var nameBuf bytes.Buffer 716 var nameValPtr reflect.Value 717 return dec.decodeObject(typ, func() *DecodeError { 718 dec.posStackPush() 719 defer dec.posStackPop() 720 nameBuf.Reset() 721 if err := dec.decodeString(nil, &nameBuf); err != nil { 722 return err 723 } 724 nameValTyp := typ.Key() 725 nameValPtr = reflect.New(nameValTyp) 726 switch { 727 case reflect.PointerTo(nameValTyp).Implements(textUnmarshalerType): 728 obj := nameValPtr.Interface().(encoding.TextUnmarshaler) 729 if err := obj.UnmarshalText(nameBuf.Bytes()); err != nil { 730 dec.newTypeError("string", reflect.PointerTo(nameValTyp), err) 731 } 732 default: 733 switch nameValTyp.Kind() { 734 case reflect.String: 735 nameValPtr.Elem().SetString(nameBuf.String()) 736 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 737 n, err := strconv.ParseInt(nameBuf.String(), 10, kind2bits[nameValTyp.Kind()]) 738 if err != nil { 739 dec.newTypeError("number "+nameBuf.String(), nameValTyp, err) 740 return nil 741 } 742 nameValPtr.Elem().SetInt(n) 743 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 744 n, err := strconv.ParseUint(nameBuf.String(), 10, kind2bits[nameValTyp.Kind()]) 745 if err != nil { 746 dec.newTypeError("number "+nameBuf.String(), nameValTyp, err) 747 return nil 748 } 749 nameValPtr.Elem().SetUint(n) 750 default: 751 dec.newTypeError("object", typ, &DecodeArgumentError{Type: nameValTyp}) 752 } 753 } 754 return nil 755 }, func() *DecodeError { 756 dec.posStackPush() 757 defer dec.posStackPop() 758 dec.structStackPush(typ, nameValPtr.Elem()) 759 defer dec.structStackPop() 760 761 fValPtr := reflect.New(typ.Elem()) 762 if err := dec.decode(fValPtr.Elem(), false); err != nil { 763 return err 764 } 765 766 val.SetMapIndex(nameValPtr.Elem(), fValPtr.Elem()) 767 return nil 768 }) 769 default: 770 dec.newTypeError(t.JSONType(), typ, nil) 771 } 772 case reflect.Slice: 773 t, err := dec.peekRuneType() 774 if err != nil { 775 return err 776 } 777 switch { 778 case typ.Elem().Kind() == reflect.Uint8 && !(t == jsonparse.RuneTypeArrayBeg && (false || 779 reflect.PointerTo(typ.Elem()).Implements(decodableType) || 780 reflect.PointerTo(typ.Elem()).Implements(jsonUnmarshalerType) || 781 reflect.PointerTo(typ.Elem()).Implements(textUnmarshalerType))): 782 switch t { 783 case jsonparse.RuneTypeNullN: 784 if err := dec.decodeNull(); err != nil { 785 return err 786 } 787 val.Set(reflect.Zero(typ)) 788 case jsonparse.RuneTypeStringBeg: 789 if typ.Elem() == byteType { 790 var buf bytes.Buffer 791 if err := dec.decodeString(typ, base64dec.NewBase64Decoder(&buf)); err != nil { 792 return err 793 } 794 val.Set(reflect.ValueOf(buf.Bytes())) 795 } else { 796 // TODO: Surely there's a better way. At the very least, we should 797 // avoid buffering. 798 var buf bytes.Buffer 799 if err := dec.decodeString(typ, base64dec.NewBase64Decoder(&buf)); err != nil { 800 return err 801 } 802 bs := buf.Bytes() 803 val.Set(reflect.MakeSlice(typ, len(bs), len(bs))) 804 for i := 0; i < len(bs); i++ { 805 val.Index(i).Set(reflect.ValueOf(bs[i]).Convert(typ.Elem())) 806 } 807 } 808 default: 809 dec.newTypeError(t.JSONType(), typ, nil) 810 } 811 default: 812 switch t { 813 case jsonparse.RuneTypeNullN: 814 if err := dec.decodeNull(); err != nil { 815 return err 816 } 817 val.Set(reflect.Zero(typ)) 818 case jsonparse.RuneTypeArrayBeg: 819 if val.IsNil() { 820 val.Set(reflect.MakeSlice(typ, 0, 0)) 821 } 822 if val.Len() > 0 { 823 val.Set(val.Slice(0, 0)) 824 } 825 i := 0 826 return dec.decodeArray(typ, func() *DecodeError { 827 dec.posStackPush() 828 defer dec.posStackPop() 829 dec.structStackPush(typ, i) 830 defer dec.structStackPop() 831 mValPtr := reflect.New(typ.Elem()) 832 if err := dec.decode(mValPtr.Elem(), false); err != nil { 833 return err 834 } 835 val.Set(reflect.Append(val, mValPtr.Elem())) 836 i++ 837 return nil 838 }) 839 default: 840 dec.newTypeError(t.JSONType(), typ, nil) 841 } 842 } 843 case reflect.Array: 844 if ok, err := dec.maybeDecodeNull(nullOK); ok { 845 return err 846 } 847 i := 0 848 n := val.Len() 849 if err := dec.decodeArray(typ, func() *DecodeError { 850 dec.posStackPush() 851 defer dec.posStackPop() 852 dec.structStackPush(typ, i) 853 defer dec.structStackPop() 854 if i < n { 855 mValPtr := reflect.New(typ.Elem()) 856 if err := dec.decode(mValPtr.Elem(), false); err != nil { 857 return err 858 } 859 val.Index(i).Set(mValPtr.Elem()) 860 } else { 861 if err := dec.scan(fastio.Discard); err != nil { 862 return err 863 } 864 } 865 i++ 866 return nil 867 }); err != nil { 868 return err 869 } 870 for ; i < n; i++ { 871 val.Index(i).Set(reflect.Zero(typ.Elem())) 872 } 873 case reflect.Pointer: 874 t, err := dec.peekRuneType() 875 if err != nil { 876 return err 877 } 878 switch t { 879 case jsonparse.RuneTypeNullN: 880 if err := dec.decodeNull(); err != nil { 881 return err 882 } 883 val.Set(reflect.Zero(typ)) 884 default: 885 if val.IsNil() { 886 val.Set(reflect.New(typ.Elem())) 887 } 888 return dec.decode(val.Elem(), false) 889 } 890 default: 891 dec.newTypeError("", typ, fmt.Errorf("unsupported type (kind=%v)", typ.Kind())) 892 } 893 } 894 return nil 895 } 896 897 func (dec *Decoder) scan(out fastio.RuneWriter) *DecodeError { 898 dec.io.PushReadBarrier() 899 for { 900 c, t, err := dec.readRune() 901 if err != nil { 902 return err 903 } 904 if t == jsonparse.RuneTypeEOF { 905 break 906 } 907 _, _ = out.WriteRune(c) 908 } 909 dec.io.PopReadBarrier() 910 return nil 911 } 912 913 func (dec *Decoder) decodeAny() (any, *DecodeError) { 914 t, err := dec.peekRuneType() 915 if err != nil { 916 return nil, err 917 } 918 switch t { 919 case jsonparse.RuneTypeObjectBeg: 920 ret := make(map[string]any) 921 typ := reflect.TypeOf(ret) 922 var nameBuf strings.Builder 923 if err := dec.decodeObject(typ, func() *DecodeError { 924 dec.posStackPush() 925 defer dec.posStackPop() 926 nameBuf.Reset() 927 return dec.decodeString(nil, &nameBuf) 928 }, func() *DecodeError { 929 dec.posStackPush() 930 defer dec.posStackPop() 931 name := nameBuf.String() 932 dec.structStackPush(typ, name) 933 defer dec.structStackPop() 934 v, err := dec.decodeAny() 935 if err != nil { 936 return err 937 } 938 ret[name] = v 939 return nil 940 }); err != nil { 941 return nil, err 942 } 943 return ret, nil 944 case jsonparse.RuneTypeArrayBeg: 945 ret := []any{} 946 typ := reflect.TypeOf(ret) 947 if err := dec.decodeArray(typ, func() *DecodeError { 948 dec.posStackPush() 949 defer dec.posStackPop() 950 dec.structStackPush(typ, len(ret)) 951 defer dec.structStackPop() 952 v, err := dec.decodeAny() 953 if err != nil { 954 return err 955 } 956 ret = append(ret, v) 957 return nil 958 }); err != nil { 959 return nil, err 960 } 961 return ret, nil 962 case jsonparse.RuneTypeStringBeg: 963 var buf strings.Builder 964 if err := dec.decodeString(nil, &buf); err != nil { 965 return nil, err 966 } 967 return buf.String(), nil 968 case jsonparse.RuneTypeNumberIntNeg, jsonparse.RuneTypeNumberIntZero, jsonparse.RuneTypeNumberIntDig: 969 var buf strings.Builder 970 if err := dec.scan(&buf); err != nil { 971 return nil, err 972 } 973 num := Number(buf.String()) 974 if dec.useNumber { 975 return num, nil 976 } 977 f64, err := num.Float64() 978 if err != nil { 979 dec.newTypeError("number "+buf.String(), float64Type, err) 980 return nil, nil 981 } 982 return f64, nil 983 case jsonparse.RuneTypeTrueT, jsonparse.RuneTypeFalseF: 984 return dec.decodeBool(nil) 985 case jsonparse.RuneTypeNullN: 986 return nil, dec.decodeNull() 987 default: 988 panic(fmt.Errorf("should not happen: unexpected runeType at beginning of value: %v", t)) 989 } 990 } 991 992 // DecodeObject is a helper function to ease implementing the 993 // Decodable interface; allowing the lowmemjson package to handle 994 // decoding the object syntax, while the Decodable only needs to 995 // handle decoding the keys and values within the object. 996 // 997 // Outside of implementing Decodable.DecodeJSON methods, callers 998 // should instead simply use NewDecoder(r).Decode(&val) rather than 999 // attempting to call DecodeObject directly. 1000 func DecodeObject(r io.RuneScanner, decodeKey, decodeVal func(io.RuneScanner) error) error { 1001 var dec *Decoder 1002 if dr, ok := r.(*decRuneScanner); ok { 1003 dec = dr.dec 1004 } else { 1005 dec = NewDecoder(r) 1006 } 1007 if dec.typeErr != nil { 1008 oldTypeErr := dec.typeErr 1009 dec.typeErr = nil 1010 defer func() { dec.typeErr = oldTypeErr }() 1011 } 1012 dec.posStackPush() 1013 defer dec.posStackPop() 1014 // TODO Find a better Go type to use than `nil`. 1015 if err := dec.decodeObject(nil, 1016 func() *DecodeError { 1017 dec.posStackPush() 1018 defer dec.posStackPop() 1019 // TODO: Find a better Go type to use than `nil`. 1020 return dec.withLimitingScanner(nil, decodeKey) 1021 }, 1022 func() *DecodeError { 1023 dec.posStackPush() 1024 defer dec.posStackPop() 1025 // TODO: Find a better Go type to use than `nil`. 1026 return dec.withLimitingScanner(nil, decodeVal) 1027 }); err != nil { 1028 return err 1029 } 1030 if dec.typeErr != nil { 1031 return dec.typeErr 1032 } 1033 return nil 1034 } 1035 1036 func (dec *Decoder) decodeObject(gTyp reflect.Type, decodeKey, decodeVal func() *DecodeError) *DecodeError { 1037 if _, t, err := dec.readRune(); err != nil { 1038 return err 1039 } else if t != jsonparse.RuneTypeObjectBeg { 1040 dec.newTypeError(t.JSONType(), gTyp, nil) 1041 dec.unreadRune() 1042 return dec.scan(fastio.Discard) 1043 } 1044 _, t, err := dec.readRune() 1045 if err != nil { 1046 return err 1047 } 1048 switch t { 1049 case jsonparse.RuneTypeObjectEnd: 1050 return nil 1051 case jsonparse.RuneTypeStringBeg: 1052 decodeMember: 1053 dec.unreadRune() 1054 if err := decodeKey(); err != nil { 1055 return err 1056 } 1057 if err := dec.expectRuneOrPanic(':', jsonparse.RuneTypeObjectColon); err != nil { 1058 return err 1059 } 1060 if err := decodeVal(); err != nil { 1061 return err 1062 } 1063 _, t, err := dec.readRune() 1064 if err != nil { 1065 return err 1066 } 1067 switch t { 1068 case jsonparse.RuneTypeObjectComma: 1069 if err := dec.expectRuneOrPanic('"', jsonparse.RuneTypeStringBeg); err != nil { 1070 return err 1071 } 1072 goto decodeMember 1073 case jsonparse.RuneTypeObjectEnd: 1074 return nil 1075 default: 1076 panic(fmt.Errorf("should not happen: unexpected runeType after k/v pair in object: %v", t)) 1077 } 1078 default: 1079 panic(fmt.Errorf("should not happen: unexpected runeType after opening '{' of object: %v", t)) 1080 } 1081 } 1082 1083 // DecodeArray is a helper function to ease implementing the Decoder 1084 // interface; allowing the lowmemjson package to handle decoding the 1085 // array syntax, while the Decodable only needs to handle decoding 1086 // members within the array. 1087 // 1088 // Outside of implementing Decodable.DecodeJSON methods, callers 1089 // should instead simply use NewDecoder(r).Decode(&val) rather than 1090 // attempting to call DecodeArray directly. 1091 func DecodeArray(r io.RuneScanner, decodeMember func(r io.RuneScanner) error) error { 1092 var dec *Decoder 1093 if dr, ok := r.(*decRuneScanner); ok { 1094 dec = dr.dec 1095 } else { 1096 dec = NewDecoder(r) 1097 } 1098 if dec.typeErr != nil { 1099 oldTypeErr := dec.typeErr 1100 dec.typeErr = nil 1101 defer func() { dec.typeErr = oldTypeErr }() 1102 } 1103 dec.posStackPush() 1104 defer dec.posStackPop() 1105 // TODO Find a better Go type to use than `nil`. 1106 if err := dec.decodeArray(nil, func() *DecodeError { 1107 dec.posStackPush() 1108 defer dec.posStackPop() 1109 // TODO: Find a better Go type to use than `nil`. 1110 return dec.withLimitingScanner(nil, decodeMember) 1111 }); err != nil { 1112 return err 1113 } 1114 if dec.typeErr != nil { 1115 return dec.typeErr 1116 } 1117 return nil 1118 } 1119 1120 func (dec *Decoder) decodeArray(gTyp reflect.Type, decodeMember func() *DecodeError) *DecodeError { 1121 if _, t, err := dec.readRune(); err != nil { 1122 return err 1123 } else if t != jsonparse.RuneTypeArrayBeg { 1124 dec.newTypeError(t.JSONType(), gTyp, nil) 1125 dec.unreadRune() 1126 return dec.scan(fastio.Discard) 1127 } 1128 _, t, err := dec.readRune() 1129 if err != nil { 1130 return err 1131 } 1132 switch t { 1133 case jsonparse.RuneTypeArrayEnd: 1134 return nil 1135 default: 1136 dec.unreadRune() 1137 decodeNextMember: 1138 if err := decodeMember(); err != nil { 1139 return err 1140 } 1141 _, t, err := dec.readRune() 1142 if err != nil { 1143 return err 1144 } 1145 switch t { 1146 case jsonparse.RuneTypeArrayComma: 1147 goto decodeNextMember 1148 case jsonparse.RuneTypeArrayEnd: 1149 return nil 1150 default: 1151 panic(fmt.Errorf("should not happen: unexpected runeType after array item: %v", t)) 1152 } 1153 } 1154 } 1155 1156 // DecodeString is a helper function to ease implementing the 1157 // Decodable interface; allowing the lowmemjson package to handle 1158 // decoding character escapes and such, while the Decodable only needs 1159 // to handle what to do with the decoded runes. 1160 // 1161 // Outside of implementing Decodable.DecodeJSON methods, callers 1162 // should instead simply use NewDecoder(r).Decode(&val) rather than 1163 // attempting to call DecodeString directly. 1164 func DecodeString(in io.RuneScanner, out fastio.RuneWriter) error { 1165 var dec *Decoder 1166 if dr, ok := in.(*decRuneScanner); ok { 1167 dec = dr.dec 1168 } else { 1169 dec = NewDecoder(in) 1170 } 1171 if dec.typeErr != nil { 1172 oldTypeErr := dec.typeErr 1173 dec.typeErr = nil 1174 defer func() { dec.typeErr = oldTypeErr }() 1175 } 1176 dec.posStackPush() 1177 defer dec.posStackPop() 1178 // TODO Find a better Go type to use than `nil`. 1179 if err := dec.decodeString(nil, out); err != nil { 1180 return err 1181 } 1182 if dec.typeErr != nil { 1183 return dec.typeErr 1184 } 1185 return nil 1186 } 1187 1188 func (dec *Decoder) decodeString(gTyp reflect.Type, out fastio.RuneWriter) *DecodeError { 1189 if _, t, err := dec.readRune(); err != nil { 1190 return err 1191 } else if t != jsonparse.RuneTypeStringBeg { 1192 dec.newTypeError(t.JSONType(), gTyp, nil) 1193 dec.unreadRune() 1194 return dec.scan(fastio.Discard) 1195 } 1196 var uhex [3]byte 1197 for { 1198 c, t, err := dec.readRune() 1199 if err != nil { 1200 return err 1201 } 1202 switch t { 1203 case jsonparse.RuneTypeStringChar: 1204 if _, err := out.WriteRune(c); err != nil { 1205 dec.newTypeError("string", gTyp, err) 1206 } 1207 case jsonparse.RuneTypeStringEsc, jsonparse.RuneTypeStringEscU: 1208 // do nothing 1209 case jsonparse.RuneTypeStringEsc1: 1210 switch c { 1211 case '"', '\\', '/': 1212 // self 1213 case 'b': 1214 c = '\b' 1215 case 'f': 1216 c = '\f' 1217 case 'n': 1218 c = '\n' 1219 case 'r': 1220 c = '\r' 1221 case 't': 1222 c = '\t' 1223 default: 1224 panic(fmt.Errorf("should not happen: rune %q is not a RuneTypeStringEsc1", c)) 1225 } 1226 if _, err := out.WriteRune(c); err != nil { 1227 dec.newTypeError("string", gTyp, err) 1228 } 1229 case jsonparse.RuneTypeStringEscUA: 1230 uhex[0] = byte(c) 1231 case jsonparse.RuneTypeStringEscUB: 1232 uhex[1] = byte(c) 1233 case jsonparse.RuneTypeStringEscUC: 1234 uhex[2] = byte(c) 1235 case jsonparse.RuneTypeStringEscUD: 1236 c = hexToRune(uhex[0], uhex[1], uhex[2], byte(c)) 1237 handleUnicode: 1238 if utf16.IsSurrogate(c) { 1239 t, err := dec.peekRuneType() 1240 if err != nil { 1241 return err 1242 } 1243 if t != jsonparse.RuneTypeStringEsc { 1244 if _, err := out.WriteRune(utf8.RuneError); err != nil { 1245 dec.newTypeError("string", gTyp, err) 1246 } 1247 break 1248 } 1249 if err := dec.expectRuneOrPanic('\\', jsonparse.RuneTypeStringEsc); err != nil { 1250 return err 1251 } 1252 t, err = dec.peekRuneType() 1253 if err != nil { 1254 return err 1255 } 1256 if t != jsonparse.RuneTypeStringEscU { 1257 if _, err := out.WriteRune(utf8.RuneError); err != nil { 1258 dec.newTypeError("string", gTyp, err) 1259 } 1260 break 1261 } 1262 if err := dec.expectRuneOrPanic('u', jsonparse.RuneTypeStringEscU); err != nil { 1263 return err 1264 } 1265 1266 b, _, err := dec.readRune() 1267 if err != nil { 1268 return err 1269 } 1270 uhex[0] = byte(b) 1271 b, _, err = dec.readRune() 1272 if err != nil { 1273 return err 1274 } 1275 uhex[1] = byte(b) 1276 b, _, err = dec.readRune() 1277 if err != nil { 1278 return err 1279 } 1280 uhex[2] = byte(b) 1281 b, _, err = dec.readRune() 1282 if err != nil { 1283 return err 1284 } 1285 c2 := hexToRune(uhex[0], uhex[1], uhex[2], byte(b)) 1286 d := utf16.DecodeRune(c, c2) 1287 if d == utf8.RuneError { 1288 if _, err := out.WriteRune(utf8.RuneError); err != nil { 1289 dec.newTypeError("string", gTyp, err) 1290 } 1291 c = c2 1292 goto handleUnicode 1293 } 1294 if _, err := out.WriteRune(d); err != nil { 1295 dec.newTypeError("string", gTyp, err) 1296 } 1297 } else { 1298 if _, err := out.WriteRune(c); err != nil { 1299 dec.newTypeError("string", gTyp, err) 1300 } 1301 } 1302 case jsonparse.RuneTypeStringEnd: 1303 return nil 1304 default: 1305 panic(fmt.Errorf("should not happen: unexpected runeType in string: %v", t)) 1306 } 1307 } 1308 } 1309 1310 func (dec *Decoder) decodeBool(gTyp reflect.Type) (bool, *DecodeError) { 1311 c, t, err := dec.readRune() 1312 if err != nil { 1313 return false, err 1314 } 1315 switch c { 1316 case 't': 1317 if err := dec.expectRuneOrPanic('r', jsonparse.RuneTypeTrueR); err != nil { 1318 return false, err 1319 } 1320 if err := dec.expectRuneOrPanic('u', jsonparse.RuneTypeTrueU); err != nil { 1321 return false, err 1322 } 1323 if err := dec.expectRuneOrPanic('e', jsonparse.RuneTypeTrueE); err != nil { 1324 return false, err 1325 } 1326 return true, nil 1327 case 'f': 1328 if err := dec.expectRuneOrPanic('a', jsonparse.RuneTypeFalseA); err != nil { 1329 return false, err 1330 } 1331 if err := dec.expectRuneOrPanic('l', jsonparse.RuneTypeFalseL); err != nil { 1332 return false, err 1333 } 1334 if err := dec.expectRuneOrPanic('s', jsonparse.RuneTypeFalseS); err != nil { 1335 return false, err 1336 } 1337 if err := dec.expectRuneOrPanic('e', jsonparse.RuneTypeFalseE); err != nil { 1338 return false, err 1339 } 1340 return false, nil 1341 default: 1342 dec.newTypeError(t.JSONType(), gTyp, nil) 1343 return false, nil 1344 } 1345 } 1346 1347 func (dec *Decoder) decodeNull() *DecodeError { 1348 if err := dec.expectRuneOrPanic('n', jsonparse.RuneTypeNullN); err != nil { 1349 return err 1350 } 1351 if err := dec.expectRuneOrPanic('u', jsonparse.RuneTypeNullU); err != nil { 1352 return err 1353 } 1354 if err := dec.expectRuneOrPanic('l', jsonparse.RuneTypeNullL1); err != nil { 1355 return err 1356 } 1357 if err := dec.expectRuneOrPanic('l', jsonparse.RuneTypeNullL2); err != nil { 1358 return err 1359 } 1360 return nil 1361 } 1362 1363 func (dec *Decoder) maybeDecodeNull(nullOK bool) (ok bool, err *DecodeError) { 1364 if nullOK { 1365 t, err := dec.peekRuneType() 1366 if err != nil { 1367 return true, err 1368 } 1369 if t == jsonparse.RuneTypeNullN { 1370 return true, dec.decodeNull() 1371 } 1372 } 1373 return false, nil 1374 }