2017-05-24 08:34:00 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-06-05 17:01:00 +02:00
|
|
|
"encoding"
|
2017-06-06 17:27:00 +02:00
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
2017-06-17 15:11:23 +02:00
|
|
|
"sort"
|
2017-06-05 17:53:48 +02:00
|
|
|
"strconv"
|
2017-06-06 17:27:00 +02:00
|
|
|
"unsafe"
|
2017-05-24 08:34:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type mapDecoder struct {
|
|
|
|
mapType reflect.Type
|
2017-06-05 17:53:48 +02:00
|
|
|
keyType reflect.Type
|
2017-05-24 08:34:00 +02:00
|
|
|
elemType reflect.Type
|
|
|
|
elemDecoder Decoder
|
|
|
|
mapInterface emptyInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (decoder *mapDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
|
|
|
// dark magic to cast unsafe.Pointer back to interface{} using reflect.Type
|
|
|
|
mapInterface := decoder.mapInterface
|
|
|
|
mapInterface.word = ptr
|
|
|
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
|
|
|
realVal := reflect.ValueOf(*realInterface).Elem()
|
|
|
|
if realVal.IsNil() {
|
|
|
|
realVal.Set(reflect.MakeMap(realVal.Type()))
|
|
|
|
}
|
2017-06-12 04:13:13 +02:00
|
|
|
iter.ReadMapCB(func(iter *Iterator, keyStr string) bool {
|
2017-05-24 08:34:00 +02:00
|
|
|
elem := reflect.New(decoder.elemType)
|
|
|
|
decoder.elemDecoder.decode(unsafe.Pointer(elem.Pointer()), iter)
|
|
|
|
// to put into map, we have to use reflection
|
2017-06-05 18:09:33 +02:00
|
|
|
keyType := decoder.keyType
|
|
|
|
switch {
|
|
|
|
case keyType.Kind() == reflect.String:
|
|
|
|
realVal.SetMapIndex(reflect.ValueOf(keyStr), elem.Elem())
|
|
|
|
return true
|
|
|
|
case keyType.Implements(textUnmarshalerType):
|
|
|
|
textUnmarshaler := reflect.New(keyType.Elem()).Interface().(encoding.TextUnmarshaler)
|
|
|
|
err := textUnmarshaler.UnmarshalText([]byte(keyStr))
|
|
|
|
if err != nil {
|
|
|
|
iter.reportError("read map key as TextUnmarshaler", err.Error())
|
|
|
|
return false
|
2017-06-05 17:53:48 +02:00
|
|
|
}
|
2017-06-05 18:09:33 +02:00
|
|
|
realVal.SetMapIndex(reflect.ValueOf(textUnmarshaler), elem.Elem())
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
switch keyType.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
n, err := strconv.ParseInt(keyStr, 10, 64)
|
|
|
|
if err != nil || reflect.Zero(keyType).OverflowInt(n) {
|
|
|
|
iter.reportError("read map key as int64", "read int64 failed")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
realVal.SetMapIndex(reflect.ValueOf(n).Convert(keyType), elem.Elem())
|
|
|
|
return true
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
n, err := strconv.ParseUint(keyStr, 10, 64)
|
|
|
|
if err != nil || reflect.Zero(keyType).OverflowUint(n) {
|
|
|
|
iter.reportError("read map key as uint64", "read uint64 failed")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
realVal.SetMapIndex(reflect.ValueOf(n).Convert(keyType), elem.Elem())
|
|
|
|
return true
|
2017-06-05 17:53:48 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-05 18:09:33 +02:00
|
|
|
iter.reportError("read map key", "unexpected map key type "+keyType.String())
|
|
|
|
return true
|
|
|
|
})
|
2017-05-24 08:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type mapEncoder struct {
|
|
|
|
mapType reflect.Type
|
|
|
|
elemType reflect.Type
|
|
|
|
elemEncoder Encoder
|
|
|
|
mapInterface emptyInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *mapEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
mapInterface := encoder.mapInterface
|
|
|
|
mapInterface.word = ptr
|
|
|
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
|
|
|
realVal := reflect.ValueOf(*realInterface)
|
|
|
|
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
for i, key := range realVal.MapKeys() {
|
|
|
|
if i != 0 {
|
|
|
|
stream.WriteMore()
|
|
|
|
}
|
2017-06-05 17:01:00 +02:00
|
|
|
encodeMapKey(key, stream)
|
|
|
|
stream.writeByte(':')
|
2017-05-24 08:34:00 +02:00
|
|
|
val := realVal.MapIndex(key).Interface()
|
|
|
|
encoder.elemEncoder.encodeInterface(val, stream)
|
|
|
|
}
|
|
|
|
stream.WriteObjectEnd()
|
|
|
|
}
|
|
|
|
|
2017-06-05 17:01:00 +02:00
|
|
|
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()}
|
|
|
|
}
|
|
|
|
|
2017-05-24 08:34:00 +02:00
|
|
|
func (encoder *mapEncoder) encodeInterface(val interface{}, stream *Stream) {
|
|
|
|
writeToStream(val, stream, encoder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *mapEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
mapInterface := encoder.mapInterface
|
|
|
|
mapInterface.word = ptr
|
|
|
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
|
|
|
realVal := reflect.ValueOf(*realInterface)
|
|
|
|
return realVal.Len() == 0
|
2017-06-05 18:09:33 +02:00
|
|
|
}
|
2017-06-16 10:46:30 +02:00
|
|
|
|
|
|
|
type sortKeysMapEncoder struct {
|
|
|
|
mapType reflect.Type
|
|
|
|
elemType reflect.Type
|
|
|
|
elemEncoder Encoder
|
|
|
|
mapInterface emptyInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *sortKeysMapEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
mapInterface := encoder.mapInterface
|
|
|
|
mapInterface.word = ptr
|
|
|
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
|
|
|
realVal := reflect.ValueOf(*realInterface)
|
|
|
|
|
|
|
|
// Extract and sort the keys.
|
2017-06-17 10:27:19 +02:00
|
|
|
var sv stringValues = realVal.MapKeys()
|
|
|
|
sort.Sort(sv)
|
2017-06-16 10:46:30 +02:00
|
|
|
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
for i, key := range sv {
|
|
|
|
if i != 0 {
|
|
|
|
stream.WriteMore()
|
|
|
|
}
|
2017-06-17 10:27:19 +02:00
|
|
|
encodeMapKey(key, stream)
|
2017-06-16 10:46:30 +02:00
|
|
|
stream.writeByte(':')
|
2017-06-17 10:27:19 +02:00
|
|
|
val := realVal.MapIndex(key).Interface()
|
2017-06-16 10:46:30 +02:00
|
|
|
encoder.elemEncoder.encodeInterface(val, stream)
|
|
|
|
}
|
|
|
|
stream.WriteObjectEnd()
|
|
|
|
}
|
|
|
|
|
2017-06-17 10:27:19 +02:00
|
|
|
// stringValues is a slice of reflect.Value holding *reflect.StringValue.
|
|
|
|
// It implements the methods to sort by string.
|
|
|
|
type stringValues []reflect.Value
|
2017-06-16 10:46:30 +02:00
|
|
|
|
2017-06-17 10:27:19 +02:00
|
|
|
func (sv stringValues) Len() int { return len(sv) }
|
|
|
|
func (sv stringValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
|
|
|
|
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() }
|
2017-06-16 10:46:30 +02:00
|
|
|
|
|
|
|
func (encoder *sortKeysMapEncoder) encodeInterface(val interface{}, stream *Stream) {
|
|
|
|
writeToStream(val, stream, encoder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *sortKeysMapEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
mapInterface := encoder.mapInterface
|
|
|
|
mapInterface.word = ptr
|
|
|
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
|
|
|
realVal := reflect.ValueOf(*realInterface)
|
|
|
|
return realVal.Len() == 0
|
|
|
|
}
|