fuzzy_decoder.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package extra
  2. import (
  3. "encoding/json"
  4. "io"
  5. "math"
  6. "reflect"
  7. "strings"
  8. "unsafe"
  9. "github.com/json-iterator/go"
  10. "github.com/v2pro/plz/reflect2"
  11. )
  12. const maxUint = ^uint(0)
  13. const maxInt = int(maxUint >> 1)
  14. const minInt = -maxInt - 1
  15. // RegisterFuzzyDecoders decode input from PHP with tolerance.
  16. // It will handle string/number auto conversation, and treat empty [] as empty struct.
  17. func RegisterFuzzyDecoders() {
  18. jsoniter.RegisterExtension(&tolerateEmptyArrayExtension{})
  19. jsoniter.RegisterTypeDecoder("string", &fuzzyStringDecoder{})
  20. jsoniter.RegisterTypeDecoder("float32", &fuzzyFloat32Decoder{})
  21. jsoniter.RegisterTypeDecoder("float64", &fuzzyFloat64Decoder{})
  22. jsoniter.RegisterTypeDecoder("int", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  23. if isFloat {
  24. val := iter.ReadFloat64()
  25. if val > float64(maxInt) || val < float64(minInt) {
  26. iter.ReportError("fuzzy decode int", "exceed range")
  27. return
  28. }
  29. *((*int)(ptr)) = int(val)
  30. } else {
  31. *((*int)(ptr)) = iter.ReadInt()
  32. }
  33. }})
  34. jsoniter.RegisterTypeDecoder("uint", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  35. if isFloat {
  36. val := iter.ReadFloat64()
  37. if val > float64(maxUint) || val < 0 {
  38. iter.ReportError("fuzzy decode uint", "exceed range")
  39. return
  40. }
  41. *((*uint)(ptr)) = uint(val)
  42. } else {
  43. *((*uint)(ptr)) = iter.ReadUint()
  44. }
  45. }})
  46. jsoniter.RegisterTypeDecoder("int8", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  47. if isFloat {
  48. val := iter.ReadFloat64()
  49. if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
  50. iter.ReportError("fuzzy decode int8", "exceed range")
  51. return
  52. }
  53. *((*int8)(ptr)) = int8(val)
  54. } else {
  55. *((*int8)(ptr)) = iter.ReadInt8()
  56. }
  57. }})
  58. jsoniter.RegisterTypeDecoder("uint8", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  59. if isFloat {
  60. val := iter.ReadFloat64()
  61. if val > float64(math.MaxUint8) || val < 0 {
  62. iter.ReportError("fuzzy decode uint8", "exceed range")
  63. return
  64. }
  65. *((*uint8)(ptr)) = uint8(val)
  66. } else {
  67. *((*uint8)(ptr)) = iter.ReadUint8()
  68. }
  69. }})
  70. jsoniter.RegisterTypeDecoder("int16", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  71. if isFloat {
  72. val := iter.ReadFloat64()
  73. if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
  74. iter.ReportError("fuzzy decode int16", "exceed range")
  75. return
  76. }
  77. *((*int16)(ptr)) = int16(val)
  78. } else {
  79. *((*int16)(ptr)) = iter.ReadInt16()
  80. }
  81. }})
  82. jsoniter.RegisterTypeDecoder("uint16", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  83. if isFloat {
  84. val := iter.ReadFloat64()
  85. if val > float64(math.MaxUint16) || val < 0 {
  86. iter.ReportError("fuzzy decode uint16", "exceed range")
  87. return
  88. }
  89. *((*uint16)(ptr)) = uint16(val)
  90. } else {
  91. *((*uint16)(ptr)) = iter.ReadUint16()
  92. }
  93. }})
  94. jsoniter.RegisterTypeDecoder("int32", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  95. if isFloat {
  96. val := iter.ReadFloat64()
  97. if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
  98. iter.ReportError("fuzzy decode int32", "exceed range")
  99. return
  100. }
  101. *((*int32)(ptr)) = int32(val)
  102. } else {
  103. *((*int32)(ptr)) = iter.ReadInt32()
  104. }
  105. }})
  106. jsoniter.RegisterTypeDecoder("uint32", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  107. if isFloat {
  108. val := iter.ReadFloat64()
  109. if val > float64(math.MaxUint32) || val < 0 {
  110. iter.ReportError("fuzzy decode uint32", "exceed range")
  111. return
  112. }
  113. *((*uint32)(ptr)) = uint32(val)
  114. } else {
  115. *((*uint32)(ptr)) = iter.ReadUint32()
  116. }
  117. }})
  118. jsoniter.RegisterTypeDecoder("int64", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  119. if isFloat {
  120. val := iter.ReadFloat64()
  121. if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
  122. iter.ReportError("fuzzy decode int64", "exceed range")
  123. return
  124. }
  125. *((*int64)(ptr)) = int64(val)
  126. } else {
  127. *((*int64)(ptr)) = iter.ReadInt64()
  128. }
  129. }})
  130. jsoniter.RegisterTypeDecoder("uint64", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  131. if isFloat {
  132. val := iter.ReadFloat64()
  133. if val > float64(math.MaxUint64) || val < 0 {
  134. iter.ReportError("fuzzy decode uint64", "exceed range")
  135. return
  136. }
  137. *((*uint64)(ptr)) = uint64(val)
  138. } else {
  139. *((*uint64)(ptr)) = iter.ReadUint64()
  140. }
  141. }})
  142. }
  143. type tolerateEmptyArrayExtension struct {
  144. jsoniter.DummyExtension
  145. }
  146. func (extension *tolerateEmptyArrayExtension) DecorateDecoder(typ reflect2.Type, decoder jsoniter.ValDecoder) jsoniter.ValDecoder {
  147. if typ.Kind() == reflect.Struct || typ.Kind() == reflect.Map {
  148. return &tolerateEmptyArrayDecoder{decoder}
  149. }
  150. return decoder
  151. }
  152. type tolerateEmptyArrayDecoder struct {
  153. valDecoder jsoniter.ValDecoder
  154. }
  155. func (decoder *tolerateEmptyArrayDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  156. if iter.WhatIsNext() == jsoniter.ArrayValue {
  157. iter.Skip()
  158. newIter := iter.Pool().BorrowIterator([]byte("{}"))
  159. defer iter.Pool().ReturnIterator(newIter)
  160. decoder.valDecoder.Decode(ptr, newIter)
  161. } else {
  162. decoder.valDecoder.Decode(ptr, iter)
  163. }
  164. }
  165. type fuzzyStringDecoder struct {
  166. }
  167. func (decoder *fuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  168. valueType := iter.WhatIsNext()
  169. switch valueType {
  170. case jsoniter.NumberValue:
  171. var number json.Number
  172. iter.ReadVal(&number)
  173. *((*string)(ptr)) = string(number)
  174. case jsoniter.StringValue:
  175. *((*string)(ptr)) = iter.ReadString()
  176. case jsoniter.NilValue:
  177. iter.Skip()
  178. *((*string)(ptr)) = ""
  179. default:
  180. iter.ReportError("fuzzyStringDecoder", "not number or string")
  181. }
  182. }
  183. type fuzzyIntegerDecoder struct {
  184. fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
  185. }
  186. func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  187. valueType := iter.WhatIsNext()
  188. var str string
  189. switch valueType {
  190. case jsoniter.NumberValue:
  191. var number json.Number
  192. iter.ReadVal(&number)
  193. str = string(number)
  194. case jsoniter.StringValue:
  195. str = iter.ReadString()
  196. case jsoniter.BoolValue:
  197. if iter.ReadBool() {
  198. str = "1"
  199. } else {
  200. str = "0"
  201. }
  202. case jsoniter.NilValue:
  203. iter.Skip()
  204. str = "0"
  205. default:
  206. iter.ReportError("fuzzyIntegerDecoder", "not number or string")
  207. }
  208. newIter := iter.Pool().BorrowIterator([]byte(str))
  209. defer iter.Pool().ReturnIterator(newIter)
  210. isFloat := strings.IndexByte(str, '.') != -1
  211. decoder.fun(isFloat, ptr, newIter)
  212. if newIter.Error != nil && newIter.Error != io.EOF {
  213. iter.Error = newIter.Error
  214. }
  215. }
  216. type fuzzyFloat32Decoder struct {
  217. }
  218. func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  219. valueType := iter.WhatIsNext()
  220. var str string
  221. switch valueType {
  222. case jsoniter.NumberValue:
  223. *((*float32)(ptr)) = iter.ReadFloat32()
  224. case jsoniter.StringValue:
  225. str = iter.ReadString()
  226. newIter := iter.Pool().BorrowIterator([]byte(str))
  227. defer iter.Pool().ReturnIterator(newIter)
  228. *((*float32)(ptr)) = newIter.ReadFloat32()
  229. if newIter.Error != nil && newIter.Error != io.EOF {
  230. iter.Error = newIter.Error
  231. }
  232. case jsoniter.BoolValue:
  233. // support bool to float32
  234. if iter.ReadBool() {
  235. *((*float32)(ptr)) = 1
  236. } else {
  237. *((*float32)(ptr)) = 0
  238. }
  239. case jsoniter.NilValue:
  240. iter.Skip()
  241. *((*float32)(ptr)) = 0
  242. default:
  243. iter.ReportError("fuzzyFloat32Decoder", "not number or string")
  244. }
  245. }
  246. type fuzzyFloat64Decoder struct {
  247. }
  248. func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  249. valueType := iter.WhatIsNext()
  250. var str string
  251. switch valueType {
  252. case jsoniter.NumberValue:
  253. *((*float64)(ptr)) = iter.ReadFloat64()
  254. case jsoniter.StringValue:
  255. str = iter.ReadString()
  256. newIter := iter.Pool().BorrowIterator([]byte(str))
  257. defer iter.Pool().ReturnIterator(newIter)
  258. *((*float64)(ptr)) = newIter.ReadFloat64()
  259. if newIter.Error != nil && newIter.Error != io.EOF {
  260. iter.Error = newIter.Error
  261. }
  262. case jsoniter.BoolValue:
  263. // support bool to float64
  264. if iter.ReadBool() {
  265. *((*float64)(ptr)) = 1
  266. } else {
  267. *((*float64)(ptr)) = 0
  268. }
  269. case jsoniter.NilValue:
  270. iter.Skip()
  271. *((*float64)(ptr)) = 0
  272. default:
  273. iter.ReportError("fuzzyFloat64Decoder", "not number or string")
  274. }
  275. }