reflect_native.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "reflect"
  6. "strconv"
  7. "unsafe"
  8. "github.com/modern-go/reflect2"
  9. )
  10. const ptrSize = 32 << uintptr(^uintptr(0)>>63)
  11. func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
  12. if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
  13. sliceDecoder := decoderOfSlice(ctx, typ)
  14. return &base64Codec{sliceDecoder: sliceDecoder}
  15. }
  16. typeName := typ.String()
  17. kind := typ.Kind()
  18. switch kind {
  19. case reflect.String:
  20. if typeName != "string" {
  21. return encoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
  22. }
  23. return &stringCodec{}
  24. case reflect.Int:
  25. if typeName != "int" {
  26. return encoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
  27. }
  28. if strconv.IntSize == 32 {
  29. return &int32Codec{}
  30. }
  31. return &int64Codec{}
  32. case reflect.Int8:
  33. if typeName != "int8" {
  34. return encoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
  35. }
  36. return &int8Codec{}
  37. case reflect.Int16:
  38. if typeName != "int16" {
  39. return encoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
  40. }
  41. return &int16Codec{}
  42. case reflect.Int32:
  43. if typeName != "int32" {
  44. return encoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
  45. }
  46. return &int32Codec{}
  47. case reflect.Int64:
  48. if typeName != "int64" {
  49. return encoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
  50. }
  51. return &int64Codec{}
  52. case reflect.Uint:
  53. if typeName != "uint" {
  54. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
  55. }
  56. if strconv.IntSize == 32 {
  57. return &uint32Codec{}
  58. }
  59. return &uint64Codec{}
  60. case reflect.Uint8:
  61. if typeName != "uint8" {
  62. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
  63. }
  64. return &uint8Codec{}
  65. case reflect.Uint16:
  66. if typeName != "uint16" {
  67. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
  68. }
  69. return &uint16Codec{}
  70. case reflect.Uint32:
  71. if typeName != "uint32" {
  72. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
  73. }
  74. return &uint32Codec{}
  75. case reflect.Uintptr:
  76. if typeName != "uintptr" {
  77. return encoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
  78. }
  79. if ptrSize == 32 {
  80. return &uint32Codec{}
  81. }
  82. return &uint64Codec{}
  83. case reflect.Uint64:
  84. if typeName != "uint64" {
  85. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
  86. }
  87. return &uint64Codec{}
  88. case reflect.Float32:
  89. if typeName != "float32" {
  90. return encoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
  91. }
  92. return &float32Codec{}
  93. case reflect.Float64:
  94. if typeName != "float64" {
  95. return encoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
  96. }
  97. return &float64Codec{}
  98. case reflect.Bool:
  99. if typeName != "bool" {
  100. return encoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
  101. }
  102. return &boolCodec{}
  103. }
  104. return nil
  105. }
  106. func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
  107. if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
  108. sliceDecoder := decoderOfSlice(ctx, typ)
  109. return &base64Codec{sliceDecoder: sliceDecoder}
  110. }
  111. typeName := typ.String()
  112. switch typ.Kind() {
  113. case reflect.String:
  114. if typeName != "string" {
  115. return decoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
  116. }
  117. return &stringCodec{}
  118. case reflect.Int:
  119. if typeName != "int" {
  120. return decoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
  121. }
  122. if strconv.IntSize == 32 {
  123. return &int32Codec{}
  124. }
  125. return &int64Codec{}
  126. case reflect.Int8:
  127. if typeName != "int8" {
  128. return decoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
  129. }
  130. return &int8Codec{}
  131. case reflect.Int16:
  132. if typeName != "int16" {
  133. return decoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
  134. }
  135. return &int16Codec{}
  136. case reflect.Int32:
  137. if typeName != "int32" {
  138. return decoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
  139. }
  140. return &int32Codec{}
  141. case reflect.Int64:
  142. if typeName != "int64" {
  143. return decoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
  144. }
  145. return &int64Codec{}
  146. case reflect.Uint:
  147. if typeName != "uint" {
  148. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
  149. }
  150. if strconv.IntSize == 32 {
  151. return &uint32Codec{}
  152. }
  153. return &uint64Codec{}
  154. case reflect.Uint8:
  155. if typeName != "uint8" {
  156. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
  157. }
  158. return &uint8Codec{}
  159. case reflect.Uint16:
  160. if typeName != "uint16" {
  161. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
  162. }
  163. return &uint16Codec{}
  164. case reflect.Uint32:
  165. if typeName != "uint32" {
  166. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
  167. }
  168. return &uint32Codec{}
  169. case reflect.Uintptr:
  170. if typeName != "uintptr" {
  171. return decoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
  172. }
  173. if ptrSize == 32 {
  174. return &uint32Codec{}
  175. }
  176. return &uint64Codec{}
  177. case reflect.Uint64:
  178. if typeName != "uint64" {
  179. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
  180. }
  181. return &uint64Codec{}
  182. case reflect.Float32:
  183. if typeName != "float32" {
  184. return decoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
  185. }
  186. return &float32Codec{}
  187. case reflect.Float64:
  188. if typeName != "float64" {
  189. return decoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
  190. }
  191. return &float64Codec{}
  192. case reflect.Bool:
  193. if typeName != "bool" {
  194. return decoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
  195. }
  196. return &boolCodec{}
  197. }
  198. return nil
  199. }
  200. type stringCodec struct {
  201. }
  202. func (codec *stringCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  203. *((*string)(ptr)) = iter.ReadString()
  204. }
  205. func (codec *stringCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  206. str := *((*string)(ptr))
  207. stream.WriteString(str)
  208. }
  209. func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
  210. return *((*string)(ptr)) == ""
  211. }
  212. type int8Codec struct {
  213. }
  214. func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  215. if !iter.ReadNil() {
  216. *((*int8)(ptr)) = iter.ReadInt8()
  217. }
  218. }
  219. func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  220. stream.WriteInt8(*((*int8)(ptr)))
  221. }
  222. func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
  223. return *((*int8)(ptr)) == 0
  224. }
  225. type int16Codec struct {
  226. }
  227. func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  228. if !iter.ReadNil() {
  229. *((*int16)(ptr)) = iter.ReadInt16()
  230. }
  231. }
  232. func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  233. stream.WriteInt16(*((*int16)(ptr)))
  234. }
  235. func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
  236. return *((*int16)(ptr)) == 0
  237. }
  238. type int32Codec struct {
  239. }
  240. func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  241. if !iter.ReadNil() {
  242. *((*int32)(ptr)) = iter.ReadInt32()
  243. }
  244. }
  245. func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  246. stream.WriteInt32(*((*int32)(ptr)))
  247. }
  248. func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  249. return *((*int32)(ptr)) == 0
  250. }
  251. type int64Codec struct {
  252. }
  253. func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  254. if !iter.ReadNil() {
  255. *((*int64)(ptr)) = iter.ReadInt64()
  256. }
  257. }
  258. func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  259. stream.WriteInt64(*((*int64)(ptr)))
  260. }
  261. func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  262. return *((*int64)(ptr)) == 0
  263. }
  264. type uint8Codec struct {
  265. }
  266. func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  267. if !iter.ReadNil() {
  268. *((*uint8)(ptr)) = iter.ReadUint8()
  269. }
  270. }
  271. func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  272. stream.WriteUint8(*((*uint8)(ptr)))
  273. }
  274. func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
  275. return *((*uint8)(ptr)) == 0
  276. }
  277. type uint16Codec struct {
  278. }
  279. func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  280. if !iter.ReadNil() {
  281. *((*uint16)(ptr)) = iter.ReadUint16()
  282. }
  283. }
  284. func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  285. stream.WriteUint16(*((*uint16)(ptr)))
  286. }
  287. func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
  288. return *((*uint16)(ptr)) == 0
  289. }
  290. type uint32Codec struct {
  291. }
  292. func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  293. if !iter.ReadNil() {
  294. *((*uint32)(ptr)) = iter.ReadUint32()
  295. }
  296. }
  297. func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  298. stream.WriteUint32(*((*uint32)(ptr)))
  299. }
  300. func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  301. return *((*uint32)(ptr)) == 0
  302. }
  303. type uint64Codec struct {
  304. }
  305. func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  306. if !iter.ReadNil() {
  307. *((*uint64)(ptr)) = iter.ReadUint64()
  308. }
  309. }
  310. func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  311. stream.WriteUint64(*((*uint64)(ptr)))
  312. }
  313. func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  314. return *((*uint64)(ptr)) == 0
  315. }
  316. type float32Codec struct {
  317. }
  318. func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  319. if !iter.ReadNil() {
  320. *((*float32)(ptr)) = iter.ReadFloat32()
  321. }
  322. }
  323. func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  324. stream.WriteFloat32(*((*float32)(ptr)))
  325. }
  326. func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  327. return *((*float32)(ptr)) == 0
  328. }
  329. type float64Codec struct {
  330. }
  331. func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  332. if !iter.ReadNil() {
  333. *((*float64)(ptr)) = iter.ReadFloat64()
  334. }
  335. }
  336. func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  337. stream.WriteFloat64(*((*float64)(ptr)))
  338. }
  339. func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  340. return *((*float64)(ptr)) == 0
  341. }
  342. type boolCodec struct {
  343. }
  344. func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  345. if !iter.ReadNil() {
  346. *((*bool)(ptr)) = iter.ReadBool()
  347. }
  348. }
  349. func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  350. stream.WriteBool(*((*bool)(ptr)))
  351. }
  352. func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
  353. return !(*((*bool)(ptr)))
  354. }
  355. type base64Codec struct {
  356. sliceType *reflect2.UnsafeSliceType
  357. sliceDecoder ValDecoder
  358. }
  359. func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  360. if iter.ReadNil() {
  361. codec.sliceType.UnsafeSetNil(ptr)
  362. return
  363. }
  364. switch iter.WhatIsNext() {
  365. case StringValue:
  366. encoding := base64.StdEncoding
  367. src := iter.SkipAndReturnBytes()
  368. // New line characters (\r and \n) are ignored.
  369. // Refer to https://golang.org/pkg/encoding/base64/#Encoding.Decode
  370. src = bytes.Replace(src, []byte(`\r`), []byte{}, -1)
  371. src = bytes.Replace(src, []byte(`\n`), []byte{}, -1)
  372. src = src[1 : len(src)-1]
  373. decodedLen := encoding.DecodedLen(len(src))
  374. dst := make([]byte, decodedLen)
  375. len, err := encoding.Decode(dst, src)
  376. if err != nil {
  377. iter.ReportError("decode base64", err.Error())
  378. } else {
  379. dst = dst[:len]
  380. codec.sliceType.UnsafeSet(ptr, unsafe.Pointer(&dst))
  381. }
  382. case ArrayValue:
  383. codec.sliceDecoder.Decode(ptr, iter)
  384. default:
  385. iter.ReportError("base64Codec", "invalid input")
  386. }
  387. }
  388. func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  389. src := *((*[]byte)(ptr))
  390. if len(src) == 0 {
  391. stream.WriteNil()
  392. return
  393. }
  394. encoding := base64.StdEncoding
  395. stream.writeByte('"')
  396. size := encoding.EncodedLen(len(src))
  397. buf := make([]byte, size)
  398. encoding.Encode(buf, src)
  399. stream.buf = append(stream.buf, buf...)
  400. stream.writeByte('"')
  401. }
  402. func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  403. return len(*((*[]byte)(ptr))) == 0
  404. }