Commit 8c86f1f3 authored by Rémy Oudompheng's avatar Rémy Oudompheng

encoding/json: cleanup leftover redundant variables.

Those variables come from ancient times when reflect.Value was
an interface.

R=dave, rsc
CC=golang-dev
https://golang.org/cl/6946059
parent 475dee90
......@@ -125,13 +125,12 @@ func (d *decodeState) unmarshal(v interface{}) (err error) {
}()
rv := reflect.ValueOf(v)
pv := rv
if pv.Kind() != reflect.Ptr || pv.IsNil() {
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &InvalidUnmarshalError{reflect.TypeOf(v)}
}
d.scan.reset()
// We decode rv not pv.Elem because the Unmarshaler interface
// We decode rv not rv.Elem because the Unmarshaler interface
// test must be applied at the top level of the value.
d.value(rv)
return d.savedError
......@@ -423,17 +422,12 @@ func (d *decodeState) object(v reflect.Value) {
v = pv
// Decoding into nil interface? Switch to non-reflect code.
iv := v
if iv.Kind() == reflect.Interface {
iv.Set(reflect.ValueOf(d.objectInterface()))
if v.Kind() == reflect.Interface {
v.Set(reflect.ValueOf(d.objectInterface()))
return
}
// Check type of target: struct or map[string]T
var (
mv reflect.Value
sv reflect.Value
)
switch v.Kind() {
case reflect.Map:
// map must have string type
......@@ -442,17 +436,15 @@ func (d *decodeState) object(v reflect.Value) {
d.saveError(&UnmarshalTypeError{"object", v.Type()})
break
}
mv = v
if mv.IsNil() {
mv.Set(reflect.MakeMap(t))
if v.IsNil() {
v.Set(reflect.MakeMap(t))
}
case reflect.Struct:
sv = v
default:
d.saveError(&UnmarshalTypeError{"object", v.Type()})
}
if !mv.IsValid() && !sv.IsValid() {
if !v.IsValid() {
d.off--
d.next() // skip over { } in input
return
......@@ -484,8 +476,8 @@ func (d *decodeState) object(v reflect.Value) {
var subv reflect.Value
destring := false // whether the value is wrapped in a string to be decoded first
if mv.IsValid() {
elemType := mv.Type().Elem()
if v.Kind() == reflect.Map {
elemType := v.Type().Elem()
if !mapElem.IsValid() {
mapElem = reflect.New(elemType).Elem()
} else {
......@@ -494,7 +486,7 @@ func (d *decodeState) object(v reflect.Value) {
subv = mapElem
} else {
var f *field
fields := cachedTypeFields(sv.Type())
fields := cachedTypeFields(v.Type())
for i := range fields {
ff := &fields[i]
if ff.name == key {
......@@ -506,7 +498,7 @@ func (d *decodeState) object(v reflect.Value) {
}
}
if f != nil {
subv = sv
subv = v
destring = f.quoted
for _, i := range f.index {
if subv.Kind() == reflect.Ptr {
......@@ -519,7 +511,7 @@ func (d *decodeState) object(v reflect.Value) {
}
} else {
// To give a good error, a quick scan for unexported fields in top level.
st := sv.Type()
st := v.Type()
for i := 0; i < st.NumField(); i++ {
f := st.Field(i)
if f.PkgPath != "" && strings.EqualFold(f.Name, key) {
......@@ -546,8 +538,8 @@ func (d *decodeState) object(v reflect.Value) {
}
// Write value back to map;
// if using struct, subv points into struct already.
if mv.IsValid() {
mv.SetMapIndex(reflect.ValueOf(key), subv)
if v.Kind() == reflect.Map {
v.SetMapIndex(reflect.ValueOf(key), subv)
}
// Next token must be , or }.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment