}
d.scan.reset()
+ d.scanWhile(scanSkipSpace)
// We decode rv not rv.Elem because the Unmarshaler interface
// test must be applied at the top level of the value.
d.value(rv)
// decodeState represents the state while decoding a JSON value.
type decodeState struct {
data []byte
- off int // read offset in data
+ off int // next read offset in data
+ opcode int // last read result
scan scanner
- nextscan scanner // for calls to nextValue
errorContext struct { // provides context for type errors
Struct string
Field string
disallowUnknownFields bool
}
+// readIndex returns the position of the last byte read.
+func (d *decodeState) readIndex() int {
+ return d.off - 1
+}
+
// errPhase is used for errors that should not happen unless
// there is a bug in the JSON decoder or something is editing
// the data slice while the decoder executes.
return err
}
-// next cuts off and returns the next full JSON value in d.data[d.off:].
-// The next value is known to be an object or array, not a literal.
-func (d *decodeState) next() []byte {
- c := d.data[d.off]
- item, rest, err := nextValue(d.data[d.off:], &d.nextscan)
- if err != nil {
- d.error(err)
+// skip scans to the end of what was started.
+func (d *decodeState) skip() {
+ s, data, i := &d.scan, d.data, d.off
+ depth := len(s.parseState)
+ for {
+ op := s.step(s, data[i])
+ i++
+ if len(s.parseState) < depth {
+ d.off = i
+ d.opcode = op
+ return
+ }
}
- d.off = len(d.data) - len(rest)
+}
- // Our scanner has seen the opening brace/bracket
- // and thinks we're still in the middle of the object.
- // invent a closing brace/bracket to get it out.
- if c == '{' {
- d.scan.step(&d.scan, '}')
+// scanNext processes the byte at d.data[d.off].
+func (d *decodeState) scanNext() {
+ s, data, i := &d.scan, d.data, d.off
+ if i < len(data) {
+ d.opcode = s.step(s, data[i])
+ d.off = i + 1
} else {
- d.scan.step(&d.scan, ']')
+ d.opcode = s.eof()
+ d.off = len(data) + 1 // mark processed EOF with len+1
}
-
- return item
}
// scanWhile processes bytes in d.data[d.off:] until it
// receives a scan code not equal to op.
-// It updates d.off and returns the new scan code.
-func (d *decodeState) scanWhile(op int) int {
- var newOp int
- for {
- if d.off >= len(d.data) {
- newOp = d.scan.eof()
- d.off = len(d.data) + 1 // mark processed EOF with len+1
- } else {
- c := d.data[d.off]
- d.off++
- newOp = d.scan.step(&d.scan, c)
- }
+func (d *decodeState) scanWhile(op int) {
+ s, data, i := &d.scan, d.data, d.off
+ for i < len(d.data) {
+ newOp := s.step(s, data[i])
+ i++
if newOp != op {
- break
+ d.opcode = newOp
+ d.off = i
+ return
}
}
- return newOp
+
+ d.off = len(d.data) + 1 // mark processed EOF with len+1
+ d.opcode = d.scan.eof()
}
-// value decodes a JSON value from d.data[d.off:] into the value.
-// it updates d.off to point past the decoded value.
+// value consumes a JSON value from d.data[d.off-1:], decoding into v, and
+// reads the following byte ahead. If v is invalid, the value is discarded.
+// The first byte of the value has been read already.
func (d *decodeState) value(v reflect.Value) {
- if !v.IsValid() {
- _, rest, err := nextValue(d.data[d.off:], &d.nextscan)
- if err != nil {
- d.error(err)
- }
- d.off = len(d.data) - len(rest)
-
- // d.scan thinks we're still at the beginning of the item.
- // Feed in an empty string - the shortest, simplest value -
- // so that it knows we got to the end of the value.
- if d.scan.redo {
- // rewind.
- d.scan.redo = false
- d.scan.step = stateBeginValue
- }
- d.scan.step(&d.scan, '"')
- d.scan.step(&d.scan, '"')
-
- n := len(d.scan.parseState)
- if n > 0 && d.scan.parseState[n-1] == parseObjectKey {
- // d.scan thinks we just read an object key; finish the object
- d.scan.step(&d.scan, ':')
- d.scan.step(&d.scan, '"')
- d.scan.step(&d.scan, '"')
- d.scan.step(&d.scan, '}')
- }
-
- return
- }
-
- switch op := d.scanWhile(scanSkipSpace); op {
+ switch d.opcode {
default:
d.error(errPhase)
case scanBeginArray:
- d.array(v)
+ if v.IsValid() {
+ d.array(v)
+ } else {
+ d.skip()
+ }
+ d.scanNext()
case scanBeginObject:
- d.object(v)
+ if v.IsValid() {
+ d.object(v)
+ } else {
+ d.skip()
+ }
+ d.scanNext()
case scanBeginLiteral:
- d.literal(v)
+ // All bytes inside literal return scanContinue op code.
+ start := d.readIndex()
+ d.scanWhile(scanContinue)
+
+ if v.IsValid() {
+ d.literalStore(d.data[start:d.readIndex()], v, false)
+ }
}
}
// If it finds anything other than a quoted string literal or null,
// valueQuoted returns unquotedValue{}.
func (d *decodeState) valueQuoted() interface{} {
- switch op := d.scanWhile(scanSkipSpace); op {
+ switch d.opcode {
default:
d.error(errPhase)
case scanBeginArray:
- d.array(reflect.Value{})
+ d.skip()
+ d.scanNext()
case scanBeginObject:
- d.object(reflect.Value{})
+ d.skip()
+ d.scanNext()
case scanBeginLiteral:
switch v := d.literalInterface().(type) {
// until it gets to a non-pointer.
// if it encounters an Unmarshaler, indirect stops and returns that.
// if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
-func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
+func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
// Issue #24153 indicates that it is generally not a guaranteed property
// that you may round-trip a reflect.Value by calling Value.Addr().Elem()
// and expect the value to still be settable for values derived from
return nil, nil, v
}
-// array consumes an array from d.data[d.off-1:], decoding into the value v.
-// the first byte of the array ('[') has been read already.
+// array consumes an array from d.data[d.off-1:], decoding into v.
+// The first byte of the array ('[') has been read already.
func (d *decodeState) array(v reflect.Value) {
// Check for unmarshaler.
- u, ut, pv := d.indirect(v, false)
+ u, ut, pv := indirect(v, false)
if u != nil {
- d.off--
- err := u.UnmarshalJSON(d.next())
+ start := d.readIndex()
+ d.skip()
+ err := u.UnmarshalJSON(d.data[start:d.off])
if err != nil {
d.error(err)
}
}
if ut != nil {
d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
- d.off--
- d.next()
+ d.skip()
return
}
-
v = pv
// Check type of target.
fallthrough
default:
d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)})
- d.off--
- d.next()
+ d.skip()
return
case reflect.Array:
case reflect.Slice:
i := 0
for {
// Look ahead for ] - can only happen on first iteration.
- op := d.scanWhile(scanSkipSpace)
- if op == scanEndArray {
+ d.scanWhile(scanSkipSpace)
+ if d.opcode == scanEndArray {
break
}
- // Back up so d.value can have the byte we just read.
- d.off--
- d.scan.undo(op)
-
// Get element of array, growing if necessary.
if v.Kind() == reflect.Slice {
// Grow slice if necessary
i++
// Next token must be , or ].
- op = d.scanWhile(scanSkipSpace)
- if op == scanEndArray {
+ if d.opcode == scanSkipSpace {
+ d.scanWhile(scanSkipSpace)
+ }
+ if d.opcode == scanEndArray {
break
}
- if op != scanArrayValue {
+ if d.opcode != scanArrayValue {
d.error(errPhase)
}
}
var nullLiteral = []byte("null")
var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
-// object consumes an object from d.data[d.off-1:], decoding into the value v.
-// the first byte ('{') of the object has been read already.
+// object consumes an object from d.data[d.off-1:], decoding into v.
+// The first byte ('{') of the object has been read already.
func (d *decodeState) object(v reflect.Value) {
// Check for unmarshaler.
- u, ut, pv := d.indirect(v, false)
+ u, ut, pv := indirect(v, false)
if u != nil {
- d.off--
- err := u.UnmarshalJSON(d.next())
+ start := d.readIndex()
+ d.skip()
+ err := u.UnmarshalJSON(d.data[start:d.off])
if err != nil {
d.error(err)
}
}
if ut != nil {
d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)})
- d.off--
- d.next() // skip over { } in input
+ d.skip()
return
}
v = pv
default:
if !reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) {
d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)})
- d.off--
- d.next() // skip over { } in input
+ d.skip()
return
}
}
// ok
default:
d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)})
- d.off--
- d.next() // skip over { } in input
+ d.skip()
return
}
for {
// Read opening " of string key or closing }.
- op := d.scanWhile(scanSkipSpace)
- if op == scanEndObject {
+ d.scanWhile(scanSkipSpace)
+ if d.opcode == scanEndObject {
// closing } - can only happen on first iteration.
break
}
- if op != scanBeginLiteral {
+ if d.opcode != scanBeginLiteral {
d.error(errPhase)
}
// Read key.
- start := d.off - 1
- op = d.scanWhile(scanContinue)
- item := d.data[start : d.off-1]
+ start := d.readIndex()
+ d.scanWhile(scanContinue)
+ item := d.data[start:d.readIndex()]
key, ok := unquoteBytes(item)
if !ok {
d.error(errPhase)
}
// Read : before value.
- if op == scanSkipSpace {
- op = d.scanWhile(scanSkipSpace)
+ if d.opcode == scanSkipSpace {
+ d.scanWhile(scanSkipSpace)
}
- if op != scanObjectKey {
+ if d.opcode != scanObjectKey {
d.error(errPhase)
}
+ d.scanWhile(scanSkipSpace)
if destring {
switch qv := d.valueQuoted().(type) {
}
// Next token must be , or }.
- op = d.scanWhile(scanSkipSpace)
- if op == scanEndObject {
+ if d.opcode == scanSkipSpace {
+ d.scanWhile(scanSkipSpace)
+ }
+ if d.opcode == scanEndObject {
break
}
- if op != scanObjectValue {
+ if d.opcode != scanObjectValue {
d.error(errPhase)
}
}
}
-// literal consumes a literal from d.data[d.off-1:], decoding into the value v.
-// The first byte of the literal has been read already
-// (that's how the caller knows it's a literal).
-func (d *decodeState) literal(v reflect.Value) {
- // All bytes inside literal return scanContinue op code.
- start := d.off - 1
- op := d.scanWhile(scanContinue)
-
- // Scan read one byte too far; back up.
- d.off--
- d.scan.undo(op)
-
- d.literalStore(d.data[start:d.off], v, false)
-}
-
// convertNumber converts the number literal s to a float64 or a Number
// depending on the setting of d.useNumber.
func (d *decodeState) convertNumber(s string) (interface{}, error) {
return
}
isNull := item[0] == 'n' // null
- u, ut, pv := d.indirect(v, isNull)
+ u, ut, pv := indirect(v, isNull)
if u != nil {
err := u.UnmarshalJSON(item)
if err != nil {
default:
val = "number"
}
- d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.readIndex())})
}
return
}
if fromQuoted {
d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
} else {
- d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())})
}
case reflect.Bool:
v.SetBool(value)
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(value))
} else {
- d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())})
}
}
}
switch v.Kind() {
default:
- d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
case reflect.Slice:
if v.Type().Elem().Kind() != reflect.Uint8 {
- d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
break
}
b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(string(s)))
} else {
- d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())})
}
}
if fromQuoted {
d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
} else {
- d.error(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.off)})
+ d.error(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())})
}
case reflect.Interface:
n, err := d.convertNumber(s)
break
}
if v.NumMethod() != 0 {
- d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())})
break
}
v.Set(reflect.ValueOf(n))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(s, 10, 64)
if err != nil || v.OverflowInt(n) {
- d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())})
break
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(s, 10, 64)
if err != nil || v.OverflowUint(n) {
- d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())})
break
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
- d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.off)})
+ d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())})
break
}
v.SetFloat(n)
// but they avoid the weight of reflection in this common case.
// valueInterface is like value but returns interface{}
-func (d *decodeState) valueInterface() interface{} {
- switch d.scanWhile(scanSkipSpace) {
+func (d *decodeState) valueInterface() (val interface{}) {
+ switch d.opcode {
default:
d.error(errPhase)
panic("unreachable")
case scanBeginArray:
- return d.arrayInterface()
+ val = d.arrayInterface()
+ d.scanNext()
case scanBeginObject:
- return d.objectInterface()
+ val = d.objectInterface()
+ d.scanNext()
case scanBeginLiteral:
- return d.literalInterface()
+ val = d.literalInterface()
}
+ return
}
// arrayInterface is like array but returns []interface{}.
var v = make([]interface{}, 0)
for {
// Look ahead for ] - can only happen on first iteration.
- op := d.scanWhile(scanSkipSpace)
- if op == scanEndArray {
+ d.scanWhile(scanSkipSpace)
+ if d.opcode == scanEndArray {
break
}
- // Back up so d.value can have the byte we just read.
- d.off--
- d.scan.undo(op)
-
v = append(v, d.valueInterface())
// Next token must be , or ].
- op = d.scanWhile(scanSkipSpace)
- if op == scanEndArray {
+ if d.opcode == scanSkipSpace {
+ d.scanWhile(scanSkipSpace)
+ }
+ if d.opcode == scanEndArray {
break
}
- if op != scanArrayValue {
+ if d.opcode != scanArrayValue {
d.error(errPhase)
}
}
m := make(map[string]interface{})
for {
// Read opening " of string key or closing }.
- op := d.scanWhile(scanSkipSpace)
- if op == scanEndObject {
+ d.scanWhile(scanSkipSpace)
+ if d.opcode == scanEndObject {
// closing } - can only happen on first iteration.
break
}
- if op != scanBeginLiteral {
+ if d.opcode != scanBeginLiteral {
d.error(errPhase)
}
// Read string key.
- start := d.off - 1
- op = d.scanWhile(scanContinue)
- item := d.data[start : d.off-1]
+ start := d.readIndex()
+ d.scanWhile(scanContinue)
+ item := d.data[start:d.readIndex()]
key, ok := unquote(item)
if !ok {
d.error(errPhase)
}
// Read : before value.
- if op == scanSkipSpace {
- op = d.scanWhile(scanSkipSpace)
+ if d.opcode == scanSkipSpace {
+ d.scanWhile(scanSkipSpace)
}
- if op != scanObjectKey {
+ if d.opcode != scanObjectKey {
d.error(errPhase)
}
+ d.scanWhile(scanSkipSpace)
// Read value.
m[key] = d.valueInterface()
// Next token must be , or }.
- op = d.scanWhile(scanSkipSpace)
- if op == scanEndObject {
+ if d.opcode == scanSkipSpace {
+ d.scanWhile(scanSkipSpace)
+ }
+ if d.opcode == scanEndObject {
break
}
- if op != scanObjectValue {
+ if d.opcode != scanObjectValue {
d.error(errPhase)
}
}
return m
}
-// literalInterface is like literal but returns an interface value.
+// literalInterface consumes and returns a literal from d.data[d.off-1:] and
+// it reads the following byte ahead. The first byte of the literal has been
+// read already (that's how the caller knows it's a literal).
func (d *decodeState) literalInterface() interface{} {
// All bytes inside literal return scanContinue op code.
- start := d.off - 1
- op := d.scanWhile(scanContinue)
+ start := d.readIndex()
+ d.scanWhile(scanContinue)
- // Scan read one byte too far; back up.
- d.off--
- d.scan.undo(op)
- item := d.data[start:d.off]
+ item := d.data[start:d.readIndex()]
switch c := item[0]; c {
case 'n': // null