fuzzy_decoder.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package extra
  2. import (
  3. "github.com/json-iterator/go"
  4. "unsafe"
  5. "encoding/json"
  6. "strings"
  7. "math"
  8. )
  9. const MaxUint = ^uint(0)
  10. const MaxInt = int(MaxUint >> 1)
  11. const MinInt = -MaxInt - 1
  12. func RegisterFuzzyDecoders() {
  13. jsoniter.RegisterTypeDecoder("string", &FuzzyStringDecoder{})
  14. jsoniter.RegisterTypeDecoder("float32", &FuzzyFloat32Decoder{})
  15. jsoniter.RegisterTypeDecoder("float64", &FuzzyFloat64Decoder{})
  16. jsoniter.RegisterTypeDecoder("int", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  17. if isFloat {
  18. val := iter.ReadFloat64()
  19. if val > float64(MaxInt) || val < float64(MinInt) {
  20. iter.ReportError("fuzzy decode int", "exceed range")
  21. return
  22. }
  23. *((*int)(ptr)) = int(val)
  24. } else {
  25. *((*int)(ptr)) = iter.ReadInt()
  26. }
  27. }})
  28. jsoniter.RegisterTypeDecoder("uint", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  29. if isFloat {
  30. val := iter.ReadFloat64()
  31. if val > float64(MaxUint) || val < 0 {
  32. iter.ReportError("fuzzy decode uint", "exceed range")
  33. return
  34. }
  35. *((*uint)(ptr)) = uint(val)
  36. } else {
  37. *((*uint)(ptr)) = iter.ReadUint()
  38. }
  39. }})
  40. jsoniter.RegisterTypeDecoder("int8", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  41. if isFloat {
  42. val := iter.ReadFloat64()
  43. if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
  44. iter.ReportError("fuzzy decode int8", "exceed range")
  45. return
  46. }
  47. *((*int8)(ptr)) = int8(val)
  48. } else {
  49. *((*int8)(ptr)) = iter.ReadInt8()
  50. }
  51. }})
  52. jsoniter.RegisterTypeDecoder("uint8", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  53. if isFloat {
  54. val := iter.ReadFloat64()
  55. if val > float64(math.MaxUint8) || val < 0 {
  56. iter.ReportError("fuzzy decode uint8", "exceed range")
  57. return
  58. }
  59. *((*uint8)(ptr)) = uint8(val)
  60. } else {
  61. *((*uint8)(ptr)) = iter.ReadUint8()
  62. }
  63. }})
  64. jsoniter.RegisterTypeDecoder("int16", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  65. if isFloat {
  66. val := iter.ReadFloat64()
  67. if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
  68. iter.ReportError("fuzzy decode int16", "exceed range")
  69. return
  70. }
  71. *((*int16)(ptr)) = int16(val)
  72. } else {
  73. *((*int16)(ptr)) = iter.ReadInt16()
  74. }
  75. }})
  76. jsoniter.RegisterTypeDecoder("uint16", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  77. if isFloat {
  78. val := iter.ReadFloat64()
  79. if val > float64(math.MaxUint16) || val < 0 {
  80. iter.ReportError("fuzzy decode uint16", "exceed range")
  81. return
  82. }
  83. *((*uint16)(ptr)) = uint16(val)
  84. } else {
  85. *((*uint16)(ptr)) = iter.ReadUint16()
  86. }
  87. }})
  88. jsoniter.RegisterTypeDecoder("int32", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  89. if isFloat {
  90. val := iter.ReadFloat64()
  91. if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
  92. iter.ReportError("fuzzy decode int32", "exceed range")
  93. return
  94. }
  95. *((*int32)(ptr)) = int32(val)
  96. } else {
  97. *((*int32)(ptr)) = iter.ReadInt32()
  98. }
  99. }})
  100. jsoniter.RegisterTypeDecoder("uint32", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  101. if isFloat {
  102. val := iter.ReadFloat64()
  103. if val > float64(math.MaxUint32) || val < 0 {
  104. iter.ReportError("fuzzy decode uint32", "exceed range")
  105. return
  106. }
  107. *((*uint32)(ptr)) = uint32(val)
  108. } else {
  109. *((*uint32)(ptr)) = iter.ReadUint32()
  110. }
  111. }})
  112. jsoniter.RegisterTypeDecoder("int64", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  113. if isFloat {
  114. val := iter.ReadFloat64()
  115. if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
  116. iter.ReportError("fuzzy decode int64", "exceed range")
  117. return
  118. }
  119. *((*int64)(ptr)) = int64(val)
  120. } else {
  121. *((*int64)(ptr)) = iter.ReadInt64()
  122. }
  123. }})
  124. jsoniter.RegisterTypeDecoder("uint64", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  125. if isFloat {
  126. val := iter.ReadFloat64()
  127. if val > float64(math.MaxUint64) || val < 0 {
  128. iter.ReportError("fuzzy decode uint64", "exceed range")
  129. return
  130. }
  131. *((*uint64)(ptr)) = uint64(val)
  132. } else {
  133. *((*uint64)(ptr)) = iter.ReadUint64()
  134. }
  135. }})
  136. }
  137. type FuzzyStringDecoder struct {
  138. }
  139. func (decoder *FuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  140. valueType := iter.WhatIsNext()
  141. switch valueType {
  142. case jsoniter.Number:
  143. var number json.Number
  144. iter.ReadVal(&number)
  145. *((*string)(ptr)) = string(number)
  146. case jsoniter.String:
  147. *((*string)(ptr)) = iter.ReadString()
  148. default:
  149. iter.ReportError("FuzzyStringDecoder", "not number or string")
  150. }
  151. }
  152. type FuzzyIntegerDecoder struct {
  153. fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
  154. }
  155. func (decoder *FuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  156. valueType := iter.WhatIsNext()
  157. var str string
  158. switch valueType {
  159. case jsoniter.Number:
  160. var number json.Number
  161. iter.ReadVal(&number)
  162. str = string(number)
  163. case jsoniter.String:
  164. str = iter.ReadString()
  165. default:
  166. iter.ReportError("FuzzyIntegerDecoder", "not number or string")
  167. }
  168. newIter := iter.Config().BorrowIterator([]byte(str))
  169. defer iter.Config().ReturnIterator(newIter)
  170. isFloat := strings.IndexByte(str, '.') != -1
  171. decoder.fun(isFloat, ptr, newIter)
  172. if newIter.Error != nil {
  173. iter.Error = newIter.Error
  174. }
  175. }
  176. type FuzzyFloat32Decoder struct {
  177. }
  178. func (decoder *FuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  179. valueType := iter.WhatIsNext()
  180. var str string
  181. switch valueType {
  182. case jsoniter.Number:
  183. *((*float32)(ptr)) = iter.ReadFloat32()
  184. case jsoniter.String:
  185. str = iter.ReadString()
  186. newIter := iter.Config().BorrowIterator([]byte(str))
  187. defer iter.Config().ReturnIterator(newIter)
  188. *((*float32)(ptr)) = newIter.ReadFloat32()
  189. if newIter.Error != nil {
  190. iter.Error = newIter.Error
  191. }
  192. default:
  193. iter.ReportError("FuzzyFloat32Decoder", "not number or string")
  194. }
  195. }
  196. type FuzzyFloat64Decoder struct {
  197. }
  198. func (decoder *FuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  199. valueType := iter.WhatIsNext()
  200. var str string
  201. switch valueType {
  202. case jsoniter.Number:
  203. *((*float64)(ptr)) = iter.ReadFloat64()
  204. case jsoniter.String:
  205. str = iter.ReadString()
  206. newIter := iter.Config().BorrowIterator([]byte(str))
  207. defer iter.Config().ReturnIterator(newIter)
  208. *((*float64)(ptr)) = newIter.ReadFloat64()
  209. if newIter.Error != nil {
  210. iter.Error = newIter.Error
  211. }
  212. default:
  213. iter.ReportError("FuzzyFloat32Decoder", "not number or string")
  214. }
  215. }