1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-23 21:09:11 +02:00

write map with int key

This commit is contained in:
Tao Wen 2017-06-05 23:01:00 +08:00
parent 5b27aaa62c
commit 29dc1d407d
2 changed files with 50 additions and 2 deletions

View File

@ -3,6 +3,8 @@ package jsoniter
import (
"unsafe"
"reflect"
"encoding/json"
"encoding"
)
type mapDecoder struct {
@ -47,13 +49,45 @@ func (encoder *mapEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
if i != 0 {
stream.WriteMore()
}
stream.WriteObjectField(key.String())
encodeMapKey(key, stream)
stream.writeByte(':')
val := realVal.MapIndex(key).Interface()
encoder.elemEncoder.encodeInterface(val, stream)
}
stream.WriteObjectEnd()
}
func encodeMapKey(key reflect.Value, stream *Stream) {
if key.Kind() == reflect.String {
stream.WriteString(key.String())
return
}
if tm, ok := key.Interface().(encoding.TextMarshaler); ok {
buf, err := tm.MarshalText()
if err != nil {
stream.Error = err
return
}
stream.writeByte('"')
stream.Write(buf)
stream.writeByte('"')
return
}
switch key.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
stream.writeByte('"')
stream.WriteInt64(key.Int())
stream.writeByte('"')
return
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
stream.writeByte('"')
stream.WriteUint64(key.Uint())
stream.writeByte('"')
return
}
stream.Error = &json.UnsupportedTypeError{key.Type()}
}
func (encoder *mapEncoder) encodeInterface(val interface{}, stream *Stream) {
writeToStream(val, stream, encoder)
}
@ -84,7 +118,13 @@ func (encoder *mapInterfaceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
if i != 0 {
stream.WriteMore()
}
stream.WriteObjectField(key.String())
switch key.Interface().(type) {
case string:
stream.WriteObjectField(key.String())
default:
stream.Error = &json.UnsupportedTypeError{key.Type()}
return
}
val := realVal.MapIndex(key).Interface()
encoder.elemEncoder.encode(unsafe.Pointer(&val), stream)
}

View File

@ -66,4 +66,12 @@ func Test_slice_of_map(t *testing.T) {
val = []map[string]string{}
should.Nil(UnmarshalFromString(str, &val))
should.Equal("2", val[0]["1"])
}
func Test_write_int_key_map(t *testing.T) {
should := require.New(t)
val := map[int]string{1: "2"}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"1":"2"}`, str)
}