mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +02:00
downgrade to lower golang version
This commit is contained in:
parent
3c8bd9ef54
commit
f29fe7407e
@ -153,56 +153,30 @@ func (encoder *sortKeysMapEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
|||||||
|
|
||||||
|
|
||||||
// Extract and sort the keys.
|
// Extract and sort the keys.
|
||||||
keys := realVal.MapKeys()
|
var sv stringValues = realVal.MapKeys()
|
||||||
sv := make([]reflectWithString, len(keys))
|
sort.Sort(sv)
|
||||||
for i, v := range keys {
|
|
||||||
sv[i].v = v
|
|
||||||
if err := sv[i].resolve(); err != nil {
|
|
||||||
stream.Error = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s })
|
|
||||||
|
|
||||||
stream.WriteObjectStart()
|
stream.WriteObjectStart()
|
||||||
for i, key := range sv {
|
for i, key := range sv {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
stream.WriteMore()
|
stream.WriteMore()
|
||||||
}
|
}
|
||||||
encodeMapKey(key.v, stream)
|
encodeMapKey(key, stream)
|
||||||
stream.writeByte(':')
|
stream.writeByte(':')
|
||||||
val := realVal.MapIndex(key.v).Interface()
|
val := realVal.MapIndex(key).Interface()
|
||||||
encoder.elemEncoder.encodeInterface(val, stream)
|
encoder.elemEncoder.encodeInterface(val, stream)
|
||||||
}
|
}
|
||||||
stream.WriteObjectEnd()
|
stream.WriteObjectEnd()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stringValues is a slice of reflect.Value holding *reflect.StringValue.
|
||||||
|
// It implements the methods to sort by string.
|
||||||
|
type stringValues []reflect.Value
|
||||||
|
|
||||||
type reflectWithString struct {
|
func (sv stringValues) Len() int { return len(sv) }
|
||||||
v reflect.Value
|
func (sv stringValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
|
||||||
s string
|
func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
|
||||||
}
|
func (sv stringValues) get(i int) string { return sv[i].String() }
|
||||||
|
|
||||||
func (w *reflectWithString) resolve() error {
|
|
||||||
if w.v.Kind() == reflect.String {
|
|
||||||
w.s = w.v.String()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok {
|
|
||||||
buf, err := tm.MarshalText()
|
|
||||||
w.s = string(buf)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
switch w.v.Kind() {
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
||||||
w.s = strconv.FormatInt(w.v.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)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
panic("unexpected map key type")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (encoder *sortKeysMapEncoder) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *sortKeysMapEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||||
writeToStream(val, stream, encoder)
|
writeToStream(val, stream, encoder)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user