reflect_native.go 12 KB

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