1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00
json-iterator/reflect_native.go

421 lines
9.4 KiB
Go
Raw Normal View History

2017-01-06 14:17:47 +02:00
package jsoniter
2017-01-25 16:43:57 +02:00
import (
2017-06-06 03:44:56 +02:00
"encoding/base64"
2017-06-06 17:27:00 +02:00
"encoding/json"
2017-09-09 02:45:57 +02:00
"reflect"
"unsafe"
2018-02-16 09:42:37 +02:00
"github.com/v2pro/plz/reflect2"
2017-01-25 16:43:57 +02:00
)
2017-01-06 14:17:47 +02:00
2017-01-09 11:47:21 +02:00
type stringCodec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *stringCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
2017-01-06 14:17:47 +02:00
*((*string)(ptr)) = iter.ReadString()
}
2017-06-20 09:11:01 +02:00
func (codec *stringCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
str := *((*string)(ptr))
stream.WriteString(str)
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*string)(ptr)) == ""
}
2017-01-09 11:47:21 +02:00
type intCodec struct {
}
2017-06-20 09:11:01 +02:00
func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int)(ptr)) = iter.ReadInt()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *intCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 11:47:21 +02:00
stream.WriteInt(*((*int)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *intCodec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*int)(ptr)) == 0
}
2017-07-02 05:03:13 +02:00
type uintptrCodec struct {
}
func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
2017-09-05 07:00:03 +02:00
}
2017-07-02 05:03:13 +02:00
}
func (codec *uintptrCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteUint64(uint64(*((*uintptr)(ptr))))
}
func (codec *uintptrCodec) IsEmpty(ptr unsafe.Pointer) bool {
return *((*uintptr)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type int8Codec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int8)(ptr)) = iter.ReadInt8()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteInt8(*((*int8)(ptr)))
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*int8)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type int16Codec struct {
}
2017-06-20 09:11:01 +02:00
func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int16)(ptr)) = iter.ReadInt16()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteInt16(*((*int16)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*int16)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type int32Codec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int32)(ptr)) = iter.ReadInt32()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteInt32(*((*int32)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*int32)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type int64Codec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int64)(ptr)) = iter.ReadInt64()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteInt64(*((*int64)(ptr)))
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*int64)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type uintCodec struct {
}
2017-06-20 09:11:01 +02:00
func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint)(ptr)) = iter.ReadUint()
2017-09-05 07:00:03 +02:00
return
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *uintCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteUint(*((*uint)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *uintCodec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*uint)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type uint8Codec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint8)(ptr)) = iter.ReadUint8()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteUint8(*((*uint8)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*uint8)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type uint16Codec struct {
2017-01-06 14:17:47 +02:00
}
2017-07-09 09:11:24 +02:00
func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint16)(ptr)) = iter.ReadUint16()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteUint16(*((*uint16)(ptr)))
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*uint16)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type uint32Codec struct {
}
2017-06-20 09:11:01 +02:00
func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint32)(ptr)) = iter.ReadUint32()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteUint32(*((*uint32)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*uint32)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type uint64Codec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint64)(ptr)) = iter.ReadUint64()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteUint64(*((*uint64)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*uint64)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type float32Codec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*float32)(ptr)) = iter.ReadFloat32()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteFloat32(*((*float32)(ptr)))
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*float32)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type float64Codec struct {
}
2017-06-20 09:11:01 +02:00
func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*float64)(ptr)) = iter.ReadFloat64()
2017-09-05 07:00:03 +02:00
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteFloat64(*((*float64)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return *((*float64)(ptr)) == 0
}
2017-01-09 13:19:48 +02:00
type boolCodec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*bool)(ptr)) = iter.ReadBool()
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 13:19:48 +02:00
stream.WriteBool(*((*bool)(ptr)))
}
2017-06-20 09:11:01 +02:00
func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 17:38:25 +02:00
return !(*((*bool)(ptr)))
}
2017-05-26 18:36:21 +02:00
type emptyInterfaceCodec struct {
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *emptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
existing := *((*interface{})(ptr))
// Checking for both typed and untyped nil pointers.
if existing != nil &&
reflect.TypeOf(existing).Kind() == reflect.Ptr &&
!reflect.ValueOf(existing).IsNil() {
var ptrToExisting interface{}
for {
elem := reflect.ValueOf(existing).Elem()
if elem.Kind() != reflect.Ptr || elem.IsNil() {
break
}
ptrToExisting = existing
existing = elem.Interface()
}
if iter.ReadNil() {
if ptrToExisting != nil {
nilPtr := reflect.Zero(reflect.TypeOf(ptrToExisting).Elem())
reflect.ValueOf(ptrToExisting).Elem().Set(nilPtr)
} else {
*((*interface{})(ptr)) = nil
}
} else {
iter.ReadVal(existing)
}
return
}
if iter.ReadNil() {
*((*interface{})(ptr)) = nil
2017-09-09 02:45:57 +02:00
} else {
*((*interface{})(ptr)) = iter.Read()
}
2017-01-06 14:17:47 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *emptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
2018-02-14 09:04:23 +02:00
obj := *((*interface{})(ptr))
stream.WriteVal(obj)
2017-01-25 18:25:17 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *emptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
2017-09-15 06:32:42 +02:00
emptyInterface := (*emptyInterface)(ptr)
return emptyInterface.typ == nil
2017-03-08 17:38:25 +02:00
}
2017-05-26 18:36:21 +02:00
type nonEmptyInterfaceCodec struct {
}
2017-06-20 09:11:01 +02:00
func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.WhatIsNext() == NilValue {
iter.skipFourBytes('n', 'u', 'l', 'l')
2018-01-04 11:18:16 +02:00
*((*interface{})(ptr)) = nil
return
}
2017-05-26 18:36:21 +02:00
nonEmptyInterface := (*nonEmptyInterface)(ptr)
if nonEmptyInterface.itab == nil {
2017-06-20 09:11:01 +02:00
iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")
return
}
2017-05-26 18:36:21 +02:00
var i interface{}
e := (*emptyInterface)(unsafe.Pointer(&i))
e.typ = nonEmptyInterface.itab.typ
e.word = nonEmptyInterface.word
iter.ReadVal(&i)
if e.word == nil {
nonEmptyInterface.itab = nil
}
2017-05-26 18:36:21 +02:00
nonEmptyInterface.word = e.word
}
2017-06-20 09:11:01 +02:00
func (codec *nonEmptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-05-26 18:36:21 +02:00
nonEmptyInterface := (*nonEmptyInterface)(ptr)
var i interface{}
if nonEmptyInterface.itab != nil {
e := (*emptyInterface)(unsafe.Pointer(&i))
e.typ = nonEmptyInterface.itab.typ
e.word = nonEmptyInterface.word
}
2017-05-26 18:36:21 +02:00
stream.WriteVal(i)
}
2017-06-20 09:11:01 +02:00
func (codec *nonEmptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
2017-05-26 18:36:21 +02:00
nonEmptyInterface := (*nonEmptyInterface)(ptr)
return nonEmptyInterface.word == nil
}
type dynamicEncoder struct {
valType reflect2.Type
}
func (encoder *dynamicEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
obj := encoder.valType.UnsafeIndirect(ptr)
stream.WriteVal(obj)
}
func (encoder *dynamicEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return encoder.valType.UnsafeIndirect(ptr) == nil
}
2017-06-06 03:44:56 +02:00
type base64Codec struct {
2018-02-19 08:39:57 +02:00
sliceType *reflect2.UnsafeSliceType
2017-07-19 06:04:22 +02:00
sliceDecoder ValDecoder
2017-06-06 03:44:56 +02:00
}
2017-06-20 09:11:01 +02:00
func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
2017-06-26 08:25:56 +02:00
if iter.ReadNil() {
2018-02-19 08:39:57 +02:00
codec.sliceType.UnsafeSetNil(ptr)
2017-06-26 08:25:56 +02:00
return
}
2017-07-19 06:04:22 +02:00
switch iter.WhatIsNext() {
case StringValue:
2017-07-19 06:04:22 +02:00
encoding := base64.StdEncoding
src := iter.SkipAndReturnBytes()
2018-02-16 09:42:37 +02:00
src = src[1: len(src)-1]
2017-07-19 06:04:22 +02:00
decodedLen := encoding.DecodedLen(len(src))
dst := make([]byte, decodedLen)
len, err := encoding.Decode(dst, src)
if err != nil {
iter.ReportError("decode base64", err.Error())
} else {
dst = dst[:len]
2018-02-19 08:39:57 +02:00
codec.sliceType.UnsafeSet(ptr, unsafe.Pointer(&dst))
2017-07-19 06:04:22 +02:00
}
case ArrayValue:
2017-07-19 06:04:22 +02:00
codec.sliceDecoder.Decode(ptr, iter)
default:
iter.ReportError("base64Codec", "invalid input")
2017-06-06 03:44:56 +02:00
}
}
2017-06-20 09:11:01 +02:00
func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-06-26 08:25:56 +02:00
src := *((*[]byte)(ptr))
if len(src) == 0 {
stream.WriteNil()
return
}
2017-06-06 03:44:56 +02:00
encoding := base64.StdEncoding
stream.writeByte('"')
2018-02-14 07:58:51 +02:00
size := encoding.EncodedLen(len(src))
buf := make([]byte, size)
encoding.Encode(buf, src)
stream.buf = append(stream.buf, buf...)
2017-06-06 03:44:56 +02:00
stream.writeByte('"')
}
2017-07-09 09:11:24 +02:00
func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-06-06 03:44:56 +02:00
return len(*((*[]byte)(ptr))) == 0
}