mirror of
https://github.com/golang/go.git
synced 2025-11-10 21:51:05 +00:00
encoding/json: marshal maps using reflect.Value.MapRange
Map serialization using reflect.Value.MapIndex cannot retrieve map keys that contain a NaN, resulting in a panic. Switch the implementation to use the reflect.Value.MapRange method instead, which iterates over all map entries regardless of whether they are directly retrievable. Note that according to RFC 8259, section 4, a JSON object should have unique names, but does not forbid the occurrence of duplicate names. Fixes #43207 Change-Id: If4bc55229b1f64b8ca4b0fed37549725efdace39 Reviewed-on: https://go-review.googlesource.com/c/go/+/278632 Trust: Meng Zhuo <mzh@golangcn.org> Trust: Joe Tsai <thebrokentoaster@gmail.com> Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Meng Zhuo <mzh@golangcn.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
parent
96a96a9058
commit
a7526bbf72
2 changed files with 40 additions and 17 deletions
|
|
@ -794,23 +794,24 @@ func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
|
|||
e.WriteByte('{')
|
||||
|
||||
// Extract and sort the keys.
|
||||
keys := v.MapKeys()
|
||||
sv := make([]reflectWithString, len(keys))
|
||||
for i, v := range keys {
|
||||
sv[i].v = v
|
||||
sv := make([]reflectWithString, v.Len())
|
||||
mi := v.MapRange()
|
||||
for i := 0; mi.Next(); i++ {
|
||||
sv[i].k = mi.Key()
|
||||
sv[i].v = mi.Value()
|
||||
if err := sv[i].resolve(); err != nil {
|
||||
e.error(fmt.Errorf("json: encoding error for type %q: %q", v.Type().String(), err.Error()))
|
||||
}
|
||||
}
|
||||
sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s })
|
||||
sort.Slice(sv, func(i, j int) bool { return sv[i].ks < sv[j].ks })
|
||||
|
||||
for i, kv := range sv {
|
||||
if i > 0 {
|
||||
e.WriteByte(',')
|
||||
}
|
||||
e.string(kv.s, opts.escapeHTML)
|
||||
e.string(kv.ks, opts.escapeHTML)
|
||||
e.WriteByte(':')
|
||||
me.elemEnc(e, v.MapIndex(kv.v), opts)
|
||||
me.elemEnc(e, kv.v, opts)
|
||||
}
|
||||
e.WriteByte('}')
|
||||
e.ptrLevel--
|
||||
|
|
@ -997,29 +998,30 @@ func typeByIndex(t reflect.Type, index []int) reflect.Type {
|
|||
}
|
||||
|
||||
type reflectWithString struct {
|
||||
v reflect.Value
|
||||
s string
|
||||
k reflect.Value
|
||||
v reflect.Value
|
||||
ks string
|
||||
}
|
||||
|
||||
func (w *reflectWithString) resolve() error {
|
||||
if w.v.Kind() == reflect.String {
|
||||
w.s = w.v.String()
|
||||
if w.k.Kind() == reflect.String {
|
||||
w.ks = w.k.String()
|
||||
return nil
|
||||
}
|
||||
if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok {
|
||||
if w.v.Kind() == reflect.Ptr && w.v.IsNil() {
|
||||
if tm, ok := w.k.Interface().(encoding.TextMarshaler); ok {
|
||||
if w.k.Kind() == reflect.Ptr && w.k.IsNil() {
|
||||
return nil
|
||||
}
|
||||
buf, err := tm.MarshalText()
|
||||
w.s = string(buf)
|
||||
w.ks = string(buf)
|
||||
return err
|
||||
}
|
||||
switch w.v.Kind() {
|
||||
switch w.k.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
w.s = strconv.FormatInt(w.v.Int(), 10)
|
||||
w.ks = strconv.FormatInt(w.k.Int(), 10)
|
||||
return nil
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
w.s = strconv.FormatUint(w.v.Uint(), 10)
|
||||
w.ks = strconv.FormatUint(w.k.Uint(), 10)
|
||||
return nil
|
||||
}
|
||||
panic("unexpected map key type")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue