1
0
mirror of https://github.com/json-iterator/go.git synced 2025-02-19 19:59:49 +02:00
json-iterator/reflect_native.go

456 lines
11 KiB
Go
Raw Normal View History

2017-01-06 20:17:47 +08:00
package jsoniter
2017-01-25 22:43:57 +08:00
import (
2017-06-06 09:44:56 +08:00
"encoding/base64"
2018-02-16 15:42:37 +08:00
"github.com/v2pro/plz/reflect2"
2018-02-24 22:04:41 +08:00
"reflect"
2018-02-23 08:12:45 +08:00
"strconv"
2018-02-24 22:04:41 +08:00
"unsafe"
2017-01-25 22:43:57 +08:00
)
2017-01-06 20:17:47 +08:00
2018-02-23 08:12:45 +08:00
const ptrSize = 32 << uintptr(^uintptr(0)>>63)
2018-02-22 10:12:08 +08:00
func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
2018-02-21 17:59:41 +08:00
sliceDecoder := decoderOfSlice(ctx, typ)
2018-02-20 22:55:31 +08:00
return &base64Codec{sliceDecoder: sliceDecoder}
}
typeName := typ.String()
kind := typ.Kind()
switch kind {
case reflect.String:
if typeName != "string" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &stringCodec{}
case reflect.Int:
if typeName != "int" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
2018-02-23 08:12:45 +08:00
if strconv.IntSize == 32 {
return &int32Codec{}
}
return &int64Codec{}
2018-02-20 22:55:31 +08:00
case reflect.Int8:
if typeName != "int8" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &int8Codec{}
case reflect.Int16:
if typeName != "int16" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &int16Codec{}
case reflect.Int32:
if typeName != "int32" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &int32Codec{}
case reflect.Int64:
if typeName != "int64" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &int64Codec{}
case reflect.Uint:
if typeName != "uint" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
2018-02-23 08:12:45 +08:00
if strconv.IntSize == 32 {
return &uint32Codec{}
}
return &uint64Codec{}
2018-02-20 22:55:31 +08:00
case reflect.Uint8:
if typeName != "uint8" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &uint8Codec{}
case reflect.Uint16:
if typeName != "uint16" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &uint16Codec{}
case reflect.Uint32:
if typeName != "uint32" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &uint32Codec{}
case reflect.Uintptr:
if typeName != "uintptr" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
2018-02-23 08:12:45 +08:00
if ptrSize == 32 {
return &uint32Codec{}
}
return &uint64Codec{}
2018-02-20 22:55:31 +08:00
case reflect.Uint64:
if typeName != "uint64" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &uint64Codec{}
case reflect.Float32:
if typeName != "float32" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &float32Codec{}
case reflect.Float64:
if typeName != "float64" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &float64Codec{}
case reflect.Bool:
if typeName != "bool" {
2018-02-22 10:12:08 +08:00
return encoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &boolCodec{}
}
return nil
}
2018-02-22 10:12:08 +08:00
func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
2018-02-21 17:59:41 +08:00
sliceDecoder := decoderOfSlice(ctx, typ)
2018-02-20 22:55:31 +08:00
return &base64Codec{sliceDecoder: sliceDecoder}
}
typeName := typ.String()
switch typ.Kind() {
case reflect.String:
if typeName != "string" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &stringCodec{}
case reflect.Int:
if typeName != "int" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
2018-02-23 08:12:45 +08:00
if strconv.IntSize == 32 {
return &int32Codec{}
}
return &int64Codec{}
2018-02-20 22:55:31 +08:00
case reflect.Int8:
if typeName != "int8" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &int8Codec{}
case reflect.Int16:
if typeName != "int16" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &int16Codec{}
case reflect.Int32:
if typeName != "int32" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &int32Codec{}
case reflect.Int64:
if typeName != "int64" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &int64Codec{}
case reflect.Uint:
if typeName != "uint" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
2018-02-23 08:12:45 +08:00
if strconv.IntSize == 32 {
return &uint32Codec{}
}
return &uint64Codec{}
2018-02-20 22:55:31 +08:00
case reflect.Uint8:
if typeName != "uint8" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &uint8Codec{}
case reflect.Uint16:
if typeName != "uint16" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &uint16Codec{}
case reflect.Uint32:
if typeName != "uint32" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &uint32Codec{}
case reflect.Uintptr:
if typeName != "uintptr" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
2018-02-23 08:12:45 +08:00
if ptrSize == 32 {
return &uint32Codec{}
}
return &uint64Codec{}
2018-02-20 22:55:31 +08:00
case reflect.Uint64:
if typeName != "uint64" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &uint64Codec{}
case reflect.Float32:
if typeName != "float32" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &float32Codec{}
case reflect.Float64:
if typeName != "float64" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &float64Codec{}
case reflect.Bool:
if typeName != "bool" {
2018-02-22 10:12:08 +08:00
return decoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
2018-02-20 22:55:31 +08:00
}
return &boolCodec{}
}
return nil
}
2017-01-09 17:47:21 +08:00
type stringCodec struct {
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *stringCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
2017-01-06 20:17:47 +08:00
*((*string)(ptr)) = iter.ReadString()
}
2017-06-20 15:11:01 +08:00
func (codec *stringCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
str := *((*string)(ptr))
stream.WriteString(str)
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*string)(ptr)) == ""
}
2017-01-09 19:19:48 +08:00
type int8Codec struct {
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int8)(ptr)) = iter.ReadInt8()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteInt8(*((*int8)(ptr)))
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*int8)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type int16Codec struct {
}
2017-06-20 15:11:01 +08:00
func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int16)(ptr)) = iter.ReadInt16()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteInt16(*((*int16)(ptr)))
}
2017-06-20 15:11:01 +08:00
func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*int16)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type int32Codec struct {
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int32)(ptr)) = iter.ReadInt32()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteInt32(*((*int32)(ptr)))
}
2017-06-20 15:11:01 +08:00
func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*int32)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type int64Codec struct {
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*int64)(ptr)) = iter.ReadInt64()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteInt64(*((*int64)(ptr)))
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*int64)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type uint8Codec struct {
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint8)(ptr)) = iter.ReadUint8()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteUint8(*((*uint8)(ptr)))
}
2017-06-20 15:11:01 +08:00
func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*uint8)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type uint16Codec struct {
2017-01-06 20:17:47 +08:00
}
2017-07-09 15:11:24 +08:00
func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint16)(ptr)) = iter.ReadUint16()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteUint16(*((*uint16)(ptr)))
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*uint16)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type uint32Codec struct {
}
2017-06-20 15:11:01 +08:00
func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint32)(ptr)) = iter.ReadUint32()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteUint32(*((*uint32)(ptr)))
}
2017-06-20 15:11:01 +08:00
func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*uint32)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type uint64Codec struct {
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*uint64)(ptr)) = iter.ReadUint64()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteUint64(*((*uint64)(ptr)))
}
2017-06-20 15:11:01 +08:00
func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*uint64)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type float32Codec struct {
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*float32)(ptr)) = iter.ReadFloat32()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteFloat32(*((*float32)(ptr)))
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*float32)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type float64Codec struct {
}
2017-06-20 15:11:01 +08:00
func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*float64)(ptr)) = iter.ReadFloat64()
2017-09-05 13:00:03 +08:00
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteFloat64(*((*float64)(ptr)))
}
2017-06-20 15:11:01 +08:00
func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return *((*float64)(ptr)) == 0
}
2017-01-09 19:19:48 +08:00
type boolCodec struct {
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if !iter.ReadNil() {
*((*bool)(ptr)) = iter.ReadBool()
}
2017-01-06 20:17:47 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:19:48 +08:00
stream.WriteBool(*((*bool)(ptr)))
}
2017-06-20 15:11:01 +08:00
func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
2017-03-08 07:38:25 -08:00
return !(*((*bool)(ptr)))
}
2017-06-06 09:44:56 +08:00
type base64Codec struct {
2018-02-24 22:04:41 +08:00
sliceType *reflect2.UnsafeSliceType
2017-07-19 12:04:22 +08:00
sliceDecoder ValDecoder
2017-06-06 09:44:56 +08:00
}
2017-06-20 15:11:01 +08:00
func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
2017-06-26 14:25:56 +08:00
if iter.ReadNil() {
2018-02-19 14:39:57 +08:00
codec.sliceType.UnsafeSetNil(ptr)
2017-06-26 14:25:56 +08:00
return
}
2017-07-19 12:04:22 +08:00
switch iter.WhatIsNext() {
case StringValue:
2017-07-19 12:04:22 +08:00
encoding := base64.StdEncoding
src := iter.SkipAndReturnBytes()
2018-02-24 22:04:41 +08:00
src = src[1 : len(src)-1]
2017-07-19 12:04:22 +08: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 14:39:57 +08:00
codec.sliceType.UnsafeSet(ptr, unsafe.Pointer(&dst))
2017-07-19 12:04:22 +08:00
}
case ArrayValue:
2017-07-19 12:04:22 +08:00
codec.sliceDecoder.Decode(ptr, iter)
default:
iter.ReportError("base64Codec", "invalid input")
2017-06-06 09:44:56 +08:00
}
}
2017-06-20 15:11:01 +08:00
func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-06-26 14:25:56 +08:00
src := *((*[]byte)(ptr))
if len(src) == 0 {
stream.WriteNil()
return
}
2017-06-06 09:44:56 +08:00
encoding := base64.StdEncoding
stream.writeByte('"')
2018-02-14 13:58:51 +08:00
size := encoding.EncodedLen(len(src))
buf := make([]byte, size)
encoding.Encode(buf, src)
stream.buf = append(stream.buf, buf...)
2017-06-06 09:44:56 +08:00
stream.writeByte('"')
}
2017-07-09 15:11:24 +08:00
func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
2017-06-06 09:44:56 +08:00
return len(*((*[]byte)(ptr))) == 0
}