MCPcopy Create free account
hub / github.com/blues/note / array

Method array

jsonxt/decode.go:498–597  ·  view source on GitHub ↗

array consumes an array from d.data[d.off-1:], decoding into v. The first byte of the array ('[') has been read already.

(v reflect.Value)

Source from the content-addressed store, hash-verified

496// array consumes an array from d.data[d.off-1:], decoding into v.
497// The first byte of the array ('[') has been read already.
498func (d *decodeState) array(v reflect.Value) error {
499 // Check for unmarshaler.
500 u, ut, pv := indirect(v, false)
501 if u != nil {
502 start := d.readIndex()
503 d.skip()
504 return u.UnmarshalJSON(d.data[start:d.off])
505 }
506 if ut != nil {
507 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
508 d.skip()
509 return nil
510 }
511 v = pv
512
513 // Check type of target.
514 switch v.Kind() {
515 case reflect.Interface:
516 if v.NumMethod() == 0 {
517 // Decoding into nil interface? Switch to non-reflect code.
518 ai := d.arrayInterface()
519 v.Set(reflect.ValueOf(ai))
520 return nil
521 }
522 // Otherwise it's invalid.
523 fallthrough
524 default:
525 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
526 d.skip()
527 return nil
528 case reflect.Array, reflect.Slice:
529 break
530 }
531
532 i := 0
533 for {
534 // Look ahead for ] - can only happen on first iteration.
535 d.scanWhile(scanSkipSpace)
536 if d.opcode == scanEndArray {
537 break
538 }
539
540 // Get element of array, growing if necessary.
541 if v.Kind() == reflect.Slice {
542 // Grow slice if necessary
543 if i >= v.Cap() {
544 newcap := v.Cap() + v.Cap()/2
545 if newcap < 4 {
546 newcap = 4
547 }
548 newv := reflect.MakeSlice(v.Type(), v.Len(), newcap)
549 reflect.Copy(newv, v)
550 v.Set(newv)
551 }
552 if i >= v.Len() {
553 v.SetLen(i + 1)
554 }
555 }

Callers 1

valueMethod · 0.95

Calls 9

readIndexMethod · 0.95
skipMethod · 0.95
saveErrorMethod · 0.95
arrayInterfaceMethod · 0.95
scanWhileMethod · 0.95
valueMethod · 0.95
indirectFunction · 0.85
LenMethod · 0.80
UnmarshalJSONMethod · 0.65

Tested by

no test coverage detected